-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.py
64 lines (62 loc) · 1.88 KB
/
24.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
# Problem: https://adventofcode.com/2020/day/24
# Part 1
from sys import stdin
from collections import defaultdict
# representing the tiles with a dict mapping (x, y) coord tuples to a boolean
# (0, 0) is the reference tile; (-1, 0) is west of it, and (1, 0) is east of it
# (-0.5, -0.5) is southwest, while (0, -1) is SW then SE (or SE then SW)
# the boolean represents whether a tile is black
tiles = defaultdict(bool)
for line in stdin:
x, y = 0, 0
h_distance = 1
for char in line.strip():
if char in ("w", "e"):
if char == "w":
x -= h_distance
else:
x += h_distance
h_distance = 1
else:
# char is either n or s, which means that the next char is either e
# or w, and it only moves x by 0.5
if char == "n":
y += 0.5
else:
y -= 0.5
h_distance = 0.5
tiles[(x, y)] = not tiles[(x, y)]
print(sum(tiles.values()))
# Part 2
# this again?
from operator import add
directions = (
(1, 0), # e
(0.5, -0.5), # se
(-0.5, -0.5), # sw
(-1, 0), # w
(-0.5, 0.5), # nw
(0.5, 0.5)
)
# change our data representation into a set of coords of black tiles
black = set(filter(lambda x: tiles[x], tiles))
for _ in range(100):
nearby_black = defaultdict(int)
for coords in black:
for offset in directions:
nearby_black[tuple(map(add, coords, offset))] += 1
flip_to_white = set([
coords
for coords in black
if (
coords not in nearby_black # black tile w/ 0 adjacent black tiles
or nearby_black[coords] > 2 # black tile w/ >2 adjacent black tiles
)
])
flip_to_black = set([
coords
for coords in nearby_black
if nearby_black[coords] == 2
])
black = (black | flip_to_black) - flip_to_white
print(len(black))