-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc24.py
85 lines (74 loc) · 2.45 KB
/
aoc24.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
from collections import defaultdict
def get_input():
with open('input24.txt') as file:
return [x.strip() for x in file]
def parse_line(line):
while len(line)>0:
if line.startswith("w"):
yield complex(-2, 0)
elif line.startswith("e"):
yield complex(2, 0)
elif line.startswith("se"):
yield complex(1, -1)
elif line.startswith("sw"):
yield complex(-1, -1)
elif line.startswith("ne"):
yield complex(1, 1)
elif line.startswith("nw"):
yield complex(-1, 1)
if line.startswith("s") or line.startswith("n"):
line = line[2:]
else:
line = line[1:]
def neighbours(point):
return [point+x for x in [
complex(-2, 0), complex(2, 0), complex(1, -1),
complex(-1, -1), complex(-1, 1), complex(1, 1)]]
def flip(flipped):
counts = defaultdict(int)
for point in flipped:
for neighbour in neighbours(point):
counts[neighbour] = counts[neighbour] + 1
tmp = set(flipped)
for point in flipped:
if counts[point] == 0 or counts[point] > 2:
tmp.remove(point)
for point in counts:
if point not in flipped and counts[point]==2:
tmp.add(point)
return tmp
# Test input
input = [
"sesenwnenenewseeswwswswwnenewsewsw",
"neeenesenwnwwswnenewnwwsewnenwseswesw",
"seswneswswsenwwnwse",
"nwnwneseeswswnenewneswwnewseswneseene",
"swweswneswnenwsewnwneneseenw",
"eesenwseswswnenwswnwnwsewwnwsene",
"sewnenenenesenwsewnenwwwse",
"wenwwweseeeweswwwnwwe",
"wsweesenenewnwwnwsenewsenwwsesesenwne",
"neeswseenwwswnwswswnw",
"nenwswwsewswnenenewsenwsenwnesesenew",
"enewnwewneswsewnwswenweswnenwsenwsw",
"sweneswneswneneenwnewenewwneswswnese",
"swwesenesewenwneswnwwneseswwne",
"enesenwswwswneneswsenwnewswseenwsese",
"wnwnesenesenenwwnenwsewesewsesesew",
"nenewswnwewswnenesenwnesewesw",
"eneswnwswnwsenenwnwnwwseeswneewsenese",
"neswnwewnwnwseenwseesewsenwsweewe",
"wseweeenwnesenwwwswnew",
]
input = get_input()
flipped = set()
for line in input:
point = sum(parse_line(line))
if point in flipped:
flipped.remove(point)
else:
flipped.add(point)
print(f"There are {len(flipped)} flipped tiles initially.")
for i in range(0, 100):
flipped = flip(flipped)
print(f"After 100 days there are {len(flipped)} flipped tiles.")