2024 - Day 18 (RAM Run)
View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
#Advent of Code
#2024 Day 18, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from collections import deque
def solve(puzzle_input: list[str], width: int = 71, byte_cutoff: int = 1024) -> int:
corrupted: set[tuple[int, int]] = set()
for corrupted_from, corrupted_loc_str in enumerate(puzzle_input[:byte_cutoff]):
corrupted_loc: tuple[int, int] = tuple(int(n) for n in corrupted_loc_str.split(","))
if corrupted_loc not in corrupted:
corrupted.add(corrupted_loc)
queue: deque[tuple[tuple[int, int], set[tuple[int, int]]]] = deque([((0, 0), set())])
seen: set[tuple[int, int]] = set([(0, 0)])
while queue:
current_loc: tuple[int, int]
current_visited: set[tuple[int, int]]
current_loc, current_visited = queue.popleft()
if current_loc == (width - 1, width - 1):
return len(current_visited)
else:
for neighbour_loc in [
(current_loc[0], current_loc[1] - 1),
(current_loc[0] - 1, current_loc[1]),
(current_loc[0], current_loc[1] + 1),
(current_loc[0] + 1, current_loc[1])
]:
if neighbour_loc[0] < 0 or neighbour_loc[0] >= width:
continue
elif neighbour_loc[1] < 0 or neighbour_loc[1] >= width:
continue
elif neighbour_loc in corrupted:
continue
elif neighbour_loc in seen:
continue
seen.add(neighbour_loc)
queue.append((neighbour_loc, current_visited | {current_loc}))
def main() -> tuple[str, int]:
puzzle_input = util.read.as_lines()
return "The minimum number of steps needed to reach the exit is {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["5,4",
"4,2",
"4,5",
"3,0",
"2,1",
"6,3",
"2,4",
"1,5",
"0,6",
"3,3",
"2,6",
"5,1",
"1,2",
"5,5",
"2,5",
"6,5",
"1,4",
"0,4",
"6,4",
"1,1",
"6,1",
"1,0",
"0,5",
"1,6",
"2,0"], 7, 12), 22)
if __name__ == "__main__":
run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
#Advent of Code
#2024 Day 18, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from collections import deque
def solve(puzzle_input: list[str], width: int = 71) -> str:
corrupted: dict[tuple[int, int], int] = {}
for corrupted_from, corrupted_loc_str in enumerate(puzzle_input):
corrupted_loc: tuple[int, int] = tuple(int(n) for n in corrupted_loc_str.split(","))
if corrupted_loc not in corrupted:
corrupted[corrupted_loc] = corrupted_from
def is_possible(byte_cutoff: int) -> bool:
queue: deque[tuple[tuple[int, int], set[tuple[int, int]]]] = deque([((0, 0), set())])
seen: set[tuple[int, int]] = set([(0, 0)])
while queue:
current_loc: tuple[int, int]
current_visited: set[tuple[int, int]]
current_loc, current_visited = queue.popleft()
if current_loc == (width - 1, width - 1):
return True
else:
for neighbour_loc in [
(current_loc[0], current_loc[1] - 1),
(current_loc[0] - 1, current_loc[1]),
(current_loc[0], current_loc[1] + 1),
(current_loc[0] + 1, current_loc[1])
]:
if neighbour_loc[0] < 0 or neighbour_loc[0] >= width:
continue
elif neighbour_loc[1] < 0 or neighbour_loc[1] >= width:
continue
elif neighbour_loc in corrupted and corrupted[neighbour_loc] <= byte_cutoff:
continue
elif neighbour_loc in seen:
continue
seen.add(neighbour_loc)
queue.append((neighbour_loc, current_visited | {current_loc}))
return False
for cutoff in range(len(puzzle_input) - 1, -1, -1):
if is_possible(cutoff):
return puzzle_input[cutoff + 1]
def main() -> tuple[str, str]:
puzzle_input = util.read.as_lines()
return "The coordinates of the first byte that prevents the exit from being reachable are {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["5,4",
"4,2",
"4,5",
"3,0",
"2,1",
"6,3",
"2,4",
"1,5",
"0,6",
"3,3",
"2,6",
"5,1",
"1,2",
"5,5",
"2,5",
"6,5",
"1,4",
"0,4",
"6,4",
"1,1",
"6,1",
"1,0",
"0,5",
"1,6",
"2,0"], 7), "6,1")
if __name__ == "__main__":
run(main)