-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.py
70 lines (59 loc) · 1.66 KB
/
day20.py
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
"""
Day 20: Race Condition
"""
from array import array
from itertools import chain
from aoc2024.day20c import solve as _solve
SAMPLE_INPUT = """
###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############
"""
def _getpath(data: str) -> array[int]:
data = data.splitlines()
y, x = next(
(y, x)
for y, line in enumerate(data)
for x, char in enumerate(line)
if char == "S"
)
path = [(y, x, 0)]
last = None
while True:
if data[y][x] == "E":
return array("i", chain.from_iterable(sorted(path)))
pos = y, x
for y, x in ((y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)):
if data[y][x] != "#" and (y, x) != last:
path.append((y, x, len(path)))
last = pos
break
else:
return None
def part1(data: str, time: int = 100) -> int:
"""
>>> counts = [part1(SAMPLE_INPUT, time) for time in (2, 4, 6, 8, 10, 12, 20, 36, 38, 40, 64)]
>>> [x - y for x, y in zip(counts, counts[1 :] + [0])]
[14, 14, 2, 4, 2, 3, 1, 1, 1, 1, 1]
"""
return _solve(_getpath(data), 2, time)
def part2(data: str, time: int = 100) -> int:
"""
>>> counts = [part2(SAMPLE_INPUT, time) for time in (50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76)]
>>> [x - y for x, y in zip(counts, counts[1 :] + [0])]
[32, 31, 29, 39, 25, 23, 20, 19, 12, 14, 12, 22, 4, 3]
"""
return _solve(_getpath(data), 20, time)
parts = (part1, part2)