-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.py
86 lines (65 loc) · 2.01 KB
/
day24.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
from util import Day
from aocd import submit
from collections import Counter
# east, southeast, southwest, west, northwest, and northeast
# e, se, sw, w, nw, and ne
# pointy hex
def preprocess(row):
return row.replace("", " ").strip().replace("n ", "n").replace("s ", "s").split(" ")
directions = {
"ne": 1 + 0.5j,
"e": 1j,
"se": -1 + 0.5j,
"sw": -1 - 0.5j,
"w": -1j,
"nw": 1 - 0.5j,
}
def flip(row):
return sum((directions[char] for char in row))
def get_neighbours(tile):
return [tile + val for val in directions.values()]
def möve(state):
new_state = set()
around = Counter()
for tile in state:
neighbours = get_neighbours(tile)
count = 0
for n in neighbours:
if n in state:
count += 1 # neighbour black of this black tile
else:
around[n] += 1 # counts up one for the neighbouring tiles if white
# Any black tile with zero or more than 2 black tiles immediately adjacent to it is flipped to white.
if 0 < count < 3:
new_state.add(tile)
# Any white tile with exactly 2 black tiles immediately adjacent to it is flipped to black.
for k, v in around.items():
if v == 2:
new_state.add(k)
return new_state
def hex_of_lyfe(state, moves):
for i in range(moves):
state = möve(state)
print(f"Move {i+1}: {len(state)}")
return len(state)
def main(day, part=1):
day.apply(preprocess)
day.apply(flip)
count = Counter(day.data)
if part == 1:
out = sum((1 for _, v in count.items() if (v % 2)))
if part == 2:
start = set((k for k, v in count.items() if (v % 2)))
out = hex_of_lyfe(start, 100)
return out
if __name__ == "__main__":
day = Day(24)
day.download()
day.load(typing=str)
p1 = main(day)
print(p1)
submit(p1, part="a", day=24, year=2020)
day.load(typing=str)
p2 = main(day, part=2)
print(p2)
submit(p2, part="b", day=24, year=2020)