-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_6.py
110 lines (84 loc) · 2.77 KB
/
day_6.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from utils import read_input
from collections import deque, namedtuple
class Token:
EMPTY = "."
GUARD = "^"
OBSTACLE = "#"
OUTSIDE = None
class Direction:
UP = "up"
DOWN = "down"
LEFT = "left"
RIGHT = "right"
Coordinate = namedtuple("Coordinate", ["x", "y"])
class Guard:
def __init__(self, start):
self.position = start
self.direction = Direction.UP
self.direction_order = deque(
[Direction.RIGHT, Direction.DOWN, Direction.LEFT, Direction.UP]
)
def next(self, grid):
new_coord = None
match self.direction:
case Direction.UP:
new_coord = Coordinate(self.position.x, self.position.y - 1)
case Direction.DOWN:
new_coord = Coordinate(self.position.x, self.position.y + 1)
case Direction.LEFT:
new_coord = Coordinate(self.position.x - 1, self.position.y)
case Direction.RIGHT:
new_coord = Coordinate(self.position.x + 1, self.position.y)
next_location = grid.get(new_coord)
if next_location == Token.EMPTY:
self.position = new_coord
return True
if next_location == Token.OBSTACLE:
self.direction = self.direction_order[0]
self.direction_order.rotate(-1)
return True
if next_location == Token.OUTSIDE:
return False
def make_grid(inputs):
grid = {}
for y, row in enumerate(inputs):
for x, cell in enumerate(row):
if cell == Token.GUARD:
guard = Guard(Coordinate(x, y))
cell = Token.EMPTY
grid[Coordinate(x, y)] = cell
return grid, guard
def part_1():
inputs = read_input(6, list)
grid, guard = make_grid(inputs)
visited = set([guard.position])
while guard.next(grid):
visited.add(guard.position)
result = len(visited)
print(f"Part 1: {result}")
assert result == 5086
return visited
def part_2(guard_path):
inputs = read_input(6, list)
grid, guard = make_grid(inputs)
start = guard.position
visited = set([(guard.position, Direction.UP)])
loop_locations = 0
for coordinate in guard_path:
n_guard = Guard(start)
n_grid = grid.copy()
n_visited = visited.copy()
if coordinate == start:
continue
if n_grid.get(coordinate) == Token.EMPTY:
n_grid[coordinate] = Token.OBSTACLE
while n_guard.next(n_grid):
spot = (n_guard.position, n_guard.direction)
if spot in n_visited:
loop_locations += 1
break
n_visited.add(spot)
print(f"Part 2: {loop_locations}")
assert loop_locations == 1770
visited = part_1()
part_2(visited)