-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_24b.py
102 lines (89 loc) · 2.75 KB
/
day_24b.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
#!/usr/bin/python3
def getNeighbourCount(coords, isWhiteUp):
neighbour_increments = [(1, 1), (1, -1), (-1, 1), (-1, -1), (2, 0), (-2, 0)]
count = 0
for neighbour_increment in neighbour_increments:
if (
coords[0] + neighbour_increment[0],
coords[1] + neighbour_increment[1],
) in isWhiteUp and not isWhiteUp[
coords[0] + neighbour_increment[0], coords[1] + neighbour_increment[1]
]:
count = count + 1
return count
def MakeArtLive(isWhiteUp):
neighbour_increments = [(1, 1), (1, -1), (-1, 1), (-1, -1), (2, 0), (-2, 0)]
to_flip = list()
for coords in isWhiteUp:
count = getNeighbourCount(coords, isWhiteUp)
if isWhiteUp[coords] and count == 2:
to_flip.append(coords)
elif not isWhiteUp[coords] and (count == 0 or count > 2):
to_flip.append(coords)
for coords in to_flip:
isWhiteUp[coords] = not isWhiteUp[coords]
def AddRequredTiles(isWhiteUp):
neighbour_increments = [(1, 1), (1, -1), (-1, 1), (-1, -1), (2, 0), (-2, 0)]
to_update = list()
for coords in isWhiteUp:
for neighbour_increment in neighbour_increments:
new_coords = (
coords[0] + neighbour_increment[0],
coords[1] + neighbour_increment[1],
)
if new_coords not in isWhiteUp:
to_update.append(new_coords)
for coords in to_update:
if coords not in isWhiteUp:
isWhiteUp[coords] = True
def getCooordinatesFromPath(path):
i = 0
x = 0
y = 0
while i < len(path):
if path[i] == "e":
x = x + 2
i = i + 1
elif path[i] == "w":
x = x - 2
i = i + 1
elif path[i : i + 2] == "nw":
x = x - 1
y = y + 1
i = i + 2
elif path[i : i + 2] == "ne":
x = x + 1
y = y + 1
i = i + 2
elif path[i : i + 2] == "sw":
x = x - 1
y = y - 1
i = i + 2
elif path[i : i + 2] == "se":
x = x + 1
y = y - 1
i = i + 2
return x, y
def main():
f = open("../input/day_24_input")
n_days = 100
isWhiteUp = dict()
for path in f:
path = path.strip("\n")
x, y = getCooordinatesFromPath(path)
coord = (x, y)
if coord in isWhiteUp:
isWhiteUp[coord] = not isWhiteUp[coord]
else:
isWhiteUp[coord] = False
for _ in range(0, n_days):
AddRequredTiles(isWhiteUp)
MakeArtLive(isWhiteUp)
count = 0
for key in isWhiteUp:
if not isWhiteUp[key]:
count = count + 1
print(count)
return count
if __name__ == "__main__":
main()