-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.py
executable file
·106 lines (76 loc) · 2.36 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# [Day 24: Lobby Layout](https://adventofcode.com/2020/day/24)
import sys
from collections import defaultdict
from pathlib import Path
verbose = "-v" in sys.argv
if verbose:
sys.argv.remove("-v")
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
lines = data.splitlines()
# part 1
def walk(data, tiles):
x, y = 0, 0
i = 0
while i < len(data):
if data[i : i + 2] == "ne":
i += 2
x = x + 1
y = y + 1
elif data[i : i + 2] == "se":
i += 2
x = x + 1
y = y - 1
elif data[i : i + 1] == "w":
i += 1
x = x - 2
elif data[i : i + 1] == "e":
i += 1
x = x + 2
elif data[i : i + 2] == "nw":
i += 2
x = x - 1
y = y + 1
elif data[i : i + 2] == "sw":
i += 2
x = x - 1
y = y - 1
else:
raise ValueError(data[i:])
tiles[(x, y)] = not tiles[(x, y)]
tiles = defaultdict(lambda: False)
for line in lines:
walk(line, tiles)
# get only the black tiles (=True, white tiles=False)
# we need them for part 2
tiles = set((x, y) for (x, y), face in tiles.items() if face is True)
print(len(tiles))
# part 2
# the six neighbors of a tile
NEIGHBORS = ((1, 1), (1, -1), (-2, 0), (2, 0), (-1, 1), (-1, -1))
# Hexagonal Conway's game of life
for day in range(100):
new_tiles = set()
seen = set()
# for every visited tiles
for black_x, black_y in tiles:
# test the tile and its six neighbors
for dx, dy in ((0, 0),) + NEIGHBORS:
x, y = black_x + dx, black_y + dy
# but only once
if (x, y) in seen:
continue
seen.add((x, y))
is_black = (x, y) in tiles
black_neighbors = sum(1 for nx, ny in NEIGHBORS if (x + nx, y + ny) in tiles)
if is_black and (black_neighbors == 0 or black_neighbors > 2):
new_is_black = False
elif not is_black and (black_neighbors == 2):
new_is_black = True
else:
new_is_black = is_black
if new_is_black:
new_tiles.add((x, y))
tiles = new_tiles
print(len(tiles))