-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol24.py
156 lines (106 loc) · 3.15 KB
/
sol24.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import utils
import re
def initialise():
global instructions
instructions = utils.loadInputFile("input_24.txt")
print(instructions)
for idx, instruction in enumerate(instructions):
instructions[idx] = re.sub(r"(e)|(w)|(se)|(sw)|(ne)|(nw)", r"\g<1>\g<2>\g<3>\g<4>\g<5>\g<6>,", instruction)[:-1]
def processSteps(direction):
global x, y
# https://www.redblobgames.com/grids/hexagons/#coordinates
if direction == "e":
x += 1
elif direction == "w":
x -= 1
elif direction == "se":
if y % 2 != 0:
x += 1
y -= 1
elif direction == "sw":
if y % 2 == 0:
x -= 1
y -= 1
elif direction == "ne":
if y % 2 != 0:
x += 1
y += 1
elif direction == "nw":
if y % 2 == 0:
x -= 1
y += 1
def getNeighbours(tile):
# e, se, sw, w, nw, and ne.
# decode
x = int(tile.split(":")[0])
y = int(tile.split(":")[1])
e = str(x+1) + ":" + str(y)
w = str(x-1) + ":" + str(y)
if y % 2 != 0:
se = str(x+1) + ":" + str(y-1)
else:
se = str(x) + ":" + str(y-1)
if y % 2 == 0:
sw = str(x-1) + ":" + str(y-1)
else:
sw = str(x) + ":" + str(y-1)
if y % 2 != 0:
ne = str(x+1) + ":" + str(y+1)
else:
ne = str(x) + ":" + str(y+1)
if y % 2 == 0:
nw = str(x-1) + ":" + str(y+1)
else:
nw = str(x) + ":" + str(y+1)
neighbours = [e, se, sw, w, nw, ne]
assert(len(set(neighbours)) == 6)
return neighbours
def findAdjacent(tile):
global blackTiles
adjacentBlackTiles = 0
neighbours = getNeighbours(tile)
for neighbour in neighbours:
if neighbour in blackTiles:
adjacentBlackTiles += 1
return adjacentBlackTiles
def part1():
global instructions, x, y, blackTiles
blackTiles = {}
for directions in instructions:
x = 0
y = 0
for direction in directions.split(","):
processSteps(direction)
coordinate = str(x) + ":" + str(y)
if coordinate in blackTiles:
del blackTiles[coordinate]
else:
blackTiles[coordinate] = 1
print(len(blackTiles))
def part2():
global blackTiles
print(blackTiles)
for day in range(100):
toRemove = []
toAdd = []
for blackTile in blackTiles:
adjacentBlackTiles = findAdjacent(blackTile)
if adjacentBlackTiles == 0 or adjacentBlackTiles > 2:
toRemove.append(blackTile)
whiteTiles = []
for blackTile in blackTiles:
whiteTiles.extend([x for x in getNeighbours(blackTile) if x not in blackTiles])
whiteTiles = list(set(whiteTiles))
for whiteTile in whiteTiles:
adjacentBlackTiles = findAdjacent(whiteTile)
if adjacentBlackTiles == 2:
toAdd.append(whiteTile)
toAdd = list(set(toAdd))
for tile in toAdd:
blackTiles[tile] = 1
for tile in toRemove:
del blackTiles[tile]
print("Day ", day+1, ": ", len(blackTiles), sep="")
initialise()
part1()
part2()