-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.py
71 lines (53 loc) · 1.81 KB
/
day06.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
71
"""
Advent of Code 2024, Day 6: Guard Gallivant.
See: https://adventofcode.com/2024/day/6
"""
import sys
from typing import TextIO
from grid import Grid, NORTH, rotate_cw
def traverse(grid: Grid, start: complex) -> tuple[set[complex], bool]:
"""
Traverse the grid from the given starting position.
Returns when the end of the map is reached or a loop occurs.
:param grid: Grid to traverse
:param start: Position to start traversing from
:return: Tuple containing the positions visited and a value
indicating whether a loop has occurred.
"""
direction = NORTH
position = start
path = set()
while position in grid and (position, direction) not in path:
path.add((position, direction))
if grid.get(position + direction) == "#":
direction = rotate_cw(direction)
else:
position += direction
return {p for p, _ in path}, (position, direction) in path
def part_one(file: TextIO) -> int:
"""
Solve part one of the puzzle.
"""
grid = Grid.from_ascii_grid(file.read())
start = next(p for p in grid if grid[p] == "^")
path, _ = traverse(grid, start)
return len(path)
def part_two(file: TextIO) -> int:
"""
Solve part two of the puzzle.
"""
grid = Grid.from_ascii_grid(file.read())
start = next(p for p in grid if grid[p] == "^")
path, _ = traverse(grid, start)
return sum(traverse(grid | {p: "#"}, start)[1] for p in path if p != start)
def main():
"""
The main entrypoint for the script.
"""
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()