-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24-2.py
79 lines (68 loc) · 1.59 KB
/
day24-2.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
from collections import deque
from copy import deepcopy
t = []
with open("day24-1.txt") as f:
t = [x.strip() for x in f.readlines()]
def grid(line):
pos = [0, 0]
l = deque(line)
while l:
p = l.popleft()
if p == "e":
pos[0] += 2
elif p == "w":
pos[0] -= 2
elif p == "s":
p = l.popleft()
pos[1] -= 1
if p == "e":
pos[0] += 1
elif p == "w":
pos[0] -= 1
elif p == "n":
p = l.popleft()
pos[1] += 1
if p == "e":
pos[0] += 1
elif p == "w":
pos[0] -= 1
return tuple(pos)
flipped = set()
for a in t:
p = grid(a)
if p in flipped:
flipped.remove(p)
else:
flipped.add(p)
print(len(flipped))
def black_neighbour(pos, f):
x, y = pos
n = [
(x + 2, y),
(x - 2, y),
(x + 1, y - 1),
(x - 1, y - 1),
(x + 1, y + 1),
(x - 1, y + 1),
]
return len([x for x in n if x in f])
def flip(f):
new_f = set()
ho = [x[0] for x in f]
x_lo = min(ho) - 2
x_hi = max(ho) + 3
ve = [x[1] for x in f]
y_lo = min(ve) - 1
y_hi = max(ve) + 2
for x in range(x_lo, x_hi):
for y in range(y_lo, y_hi):
bn = black_neighbour((x,y), f)
if (x, y) in f:
if 0 < bn <= 2:
new_f.add((x, y))
elif bn == 2:
new_f.add((x, y))
return new_f
for i in range(100):
flipped = flip(flipped)
print(len(flipped))