-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201915.py
82 lines (72 loc) · 2.25 KB
/
201915.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
from collections import deque
from copy import copy
from enum import IntEnum
from intcode import OutputInterrupt, intcode_from_file
class Tile(IntEnum):
WALL = 0
EMPTY = 1
OXYGEN = 2
class Directon(IntEnum):
NORTH = 1
SOUTH = 2
WEST = 3
EAST = 4
def explore_grid():
oxygen = None
droid = intcode_from_file("201915.txt")
grid = {(0, 0): Tile.EMPTY}
x, y = 0, 0
s = deque([(x, y, droid)])
while s:
(x, y, d) = s.popleft()
for direction in Directon:
dx, dy = 0, 0
match direction:
case Directon.NORTH:
dx, dy = 0, 1
case Directon.SOUTH:
dx, dy = 0, -1
case Directon.WEST:
dx, dy = -1, 0
case Directon.EAST:
dx, dy = 1, 0
if (x + dx, y + dy) not in grid:
droid = copy(d)
droid.append_input(direction)
try:
droid.run()
except OutputInterrupt:
tile = droid.pop_output()
grid[(x + dx, y + dy)] = Tile(tile)
if tile != Tile.WALL:
s.append((x + dx, y + dy, droid))
if tile == Tile.OXYGEN:
oxygen = (x + dx, y + dy)
break
return (grid, oxygen)
grid, oxygen = explore_grid()
part_one = 0
q = deque([(0, 0, 0)])
visited = {(0, 0)}
while q:
(x, y, steps) = q.popleft()
if (x, y) == oxygen:
print(f"Part one: {steps}")
break
for dx, dy in ((0, 1), (0, -1), (-1, 0), (1, 0)):
if (x + dx, y + dy) not in visited:
visited.add((x + dx, y + dy))
if grid[(x + dx, y + dy)] != Tile.WALL:
q.append((x + dx, y + dy, steps + 1))
part_two = 0
q = deque([(*oxygen, 0)])
visited = {oxygen}
while q:
(x, y, steps) = q.popleft()
part_two = max(part_two, steps)
for dx, dy in ((0, 1), (0, -1), (-1, 0), (1, 0)):
if (x + dx, y + dy) not in visited:
visited.add((x + dx, y + dy))
if grid[(x + dx, y + dy)] != Tile.WALL:
q.append((x + dx, y + dy, steps + 1))
print(f"Part two: {part_two}")