-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
77 lines (56 loc) · 1.7 KB
/
day10.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
# this queue is synchronized, which is pretty pointless...
from queue import Queue
with open("../inputs/day10-real.txt", "r") as f:
m = [l.strip() for l in f]
n = {
"|": [ ( 0,-1), ( 0, 1) ],
"-": [ (-1, 0), ( 1, 0) ],
"L": [ ( 0,-1), ( 1, 0) ],
"J": [ ( 0,-1), (-1, 0) ],
"7": [ (-1, 0), ( 0, 1) ],
"F": [ ( 1, 0), ( 0, 1) ],
}
x,y = None, None
for yi,line in enumerate(m):
for xi,c in enumerate(line):
if c == "S":
x,y = xi,yi
break
assert(x != None)
assert(y != None)
q = Queue()
for dx,dy in [(-1,0),(1,0),(0,-1),(0,1)]:
c = m[y+dy][x+dx]
if c in n:
for dx2,dy2 in n[c]:
if x == x+dx+dx2 and y == y+dy+dy2:
q.put((1,(x+dx,y+dy)))
dists = { (x,y): 0 }
assert(q.qsize() == 2)
while not q.empty():
d,(x,y) = q.get()
if (x,y) in dists:
continue
#print(d,(x,y))
dists[(x,y)] = d
for dx,dy in n[m[y][x]]:
q.put((d+1,(x+dx,y+dy)))
print(f"Part 1: {max(dists.values())}")
w = len(m[0])
h = len(m)
inside_count = 0
for y,line in enumerate(m):
for x,c in enumerate(line):
if (x,y) in dists:
continue
crosses = 0
x2,y2 = x,y
while x2 < w and y2 < h:
c2 = m[y2][x2]
if (x2,y2) in dists and c2 != "L" and c2 != "7":
crosses += 1
x2 += 1
y2 += 1
if crosses % 2 == 1:
inside_count += 1
print(f"Part 2: {inside_count}")