-
Notifications
You must be signed in to change notification settings - Fork 1
/
day24.py
70 lines (48 loc) · 2.06 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
with open("inputs/day24.txt") as file:
data = [line.strip() for line in file]
paths = [line.replace("nw", "0").replace("sw", "2").replace("se", "3").replace("ne", "5")
.replace("w", "1").replace("e", "4") for line in data] # Replace with numbers for easier processing
MOVES = ((1, -1), (2, 0), (1, 1), (-1, 1), (-2, 0), (-1, -1)) # Moves in all 6 directions
SIZE = 215 # Adjust to speed things up. Too low and you get an IndexError. Part 1: min 60 Part 2: min 215
tiles = [["W" for j in range(SIZE)] for i in range(SIZE)]
middle = (SIZE//2, SIZE//2)
# Part 1 ===
for path in paths:
new_coord = middle
for move in path:
new_coord = tuple(sum(pair) for pair in zip(new_coord, MOVES[int(move)]))
tile = tiles[new_coord[1]][new_coord[0]]
if tile == "W":
tiles[new_coord[1]][new_coord[0]] = "B"
else:
tiles[new_coord[1]][new_coord[0]] = "W"
part1 = 0
for row in tiles:
part1 += row.count("B")
# Part 2 ===
for i in range(100):
for y, row in enumerate(tiles):
for x, tile in enumerate(row):
neighbours = 0
for move in MOVES: # Count black neighbours
if y + move[1] < SIZE and x + move[0] < SIZE: # Border check
if tiles[y + move[1]][x + move[0]] in {"B", "V"}: # Black or formerly black
neighbours += 1
if tile == "B": # Black tile flip conditions
if neighbours == 0 or neighbours > 2:
tiles[y][x] = "V" # Wannabe white, will change later
elif tile == "W": # White tile flip conditions
if neighbours == 2:
tiles[y][x] = "P" # Wannabe black, will change later
for y, row in enumerate(tiles): # Finish tile flipping
for x, tile in enumerate(row):
if tile == "P":
tiles[y][x] = "B"
elif tile == "V":
tiles[y][x] = "W"
part2 = 0
for row in tiles:
part2 += row.count("B")
print("Part 1:", part1)
print("Part 2:", part2)
assert part1 == 400 and part2 == 3768