Skip to content

Commit

Permalink
AoC 2024 day 18 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 20, 2024
1 parent f37d3bb commit 2302659
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
25 changes: 22 additions & 3 deletions aoc2024/src/day18/python/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,35 @@
# limitations under the License.
from typing import Sequence
from common.python3.graph_utils import shortest_distance_bfs
from common.python3.types import Position

_CORRUPTED = '#'
_NOT_CORRUPTED = '.'


def _corrupt_grid(grid: list[list[str]], corrupted_positions: tuple[tuple[int, ...], ...]) -> None:
for i in range(len(corrupted_positions)):
pos = corrupted_positions[i]
grid[pos[1]][pos[0]] = _CORRUPTED


def min_steps_to_exit(input: Sequence[str], num_bytes: int = 1024,
width: int = 71, length: int = 71) -> int:
corrupted_positions = tuple(map(lambda entry: tuple(map(int, entry.split(','))), input))
grid = [[_NOT_CORRUPTED for _ in range(width)] for _ in range(length)]
for i in range(num_bytes):
pos = corrupted_positions[i]
grid[pos[1]][pos[0]] = _CORRUPTED
_corrupt_grid(grid, corrupted_positions[:num_bytes])
return shortest_distance_bfs(grid=grid, predicate=lambda x, y: grid[y][x] != _CORRUPTED)


def find_first_byte_preventing_exit(input: Sequence[str],
width: int = 71, length: int = 71) -> int:
corrupted_positions = tuple(map(lambda entry: tuple(map(int, entry.split(','))), input))
for i in range(len(corrupted_positions)):
try:
grid = [[_NOT_CORRUPTED for _ in range(width)] for _ in range(length)]
_corrupt_grid(grid, corrupted_positions[:i + 1])
_ = shortest_distance_bfs(grid=grid, predicate=lambda x, y: grid[y][x] != _CORRUPTED)
except ValueError:
# Found the byte that prevents the exit.
return corrupted_positions[i]
raise ValueError('There is no byte that prevents the exit.')
8 changes: 7 additions & 1 deletion aoc2024/test/day18/python/test_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import unittest

from common.python3.AdventOfCodeTestCase import AdventOfCodeTestCase
from aoc2024.src.day18.python.solution import min_steps_to_exit
from aoc2024.src.day18.python.solution import min_steps_to_exit, find_first_byte_preventing_exit


class TestSolution(AdventOfCodeTestCase):
Expand All @@ -28,6 +28,12 @@ def test_part1_withExample_success(self):
def test_part1_withPuzzleInput_success(self):
self.assertEqual(234, min_steps_to_exit(self.input, num_bytes=1024))

def test_part2_withExample_success(self):
self.assertEqual((6, 1), find_first_byte_preventing_exit(self.examples[0], width=7, length=7))

def test_part2_withPuzzleInput_success(self):
self.assertEqual((58, 19), find_first_byte_preventing_exit(self.input))


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions common/python3/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ def shortest_distance_bfs(grid: list[list[str]], visited: set[Position] = None,
and predicate(next_pos[0], next_pos[1])):
visited.add(next_pos)
queue.append((next_pos, level + 1))
raise ValueError('Could not reach the exit.')

0 comments on commit 2302659

Please sign in to comment.