-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp18.py
63 lines (52 loc) · 1.7 KB
/
p18.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
from collections import deque
from dataclasses import dataclass
DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def read_input():
corruptions = []
fname = "p18.input.txt"
dims = (70, 70)
if "test" in fname:
dims = (6, 6)
with open(fname, "r") as f:
for line in f.readlines():
line = line.strip()
x, y = [int(i) for i in line.split(",")]
corruptions.append((x, y))
return corruptions, dims
if __name__ == "__main__":
corruptions, dims = read_input()
def find(n_corrupt):
cset = set(corruptions[:n_corrupt])
seen = set()
@dataclass
class State:
x: int
y: int
path: set
def __lt__(self, other):
return self.y < other.y and self.x < other.x
def pos(self):
return (self.x, self.y)
search = deque([State(0, 0, set())])
while len(search) > 0:
head = search.popleft()
if head.pos() == dims:
return len(head.path)
break
for d in DIRECTIONS:
dx, dy = d
nx, ny = head.x + dx, head.y + dy
if nx < 0 or ny < 0 or nx > dims[0] or ny > dims[1]:
continue
if (nx, ny) in seen:
continue
if (nx, ny) in cset:
continue
seen.add((nx, ny))
search.append(State(nx, ny, head.path | {(nx, ny)}))
return None
print(find(1024))
for i in range(1024, len(corruptions)):
if not find(i):
print(",".join([str(i) for i in corruptions[i - 1]]))
break