This repository was archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path17_conway_cubes.py
52 lines (39 loc) · 1.48 KB
/
17_conway_cubes.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
################################
# --- Day 17: Conway Cubes --- #
################################
import AOCUtils
from itertools import combinations
# Assume dimensions >= 2
def conwayCubes(rawGrid, dimensions, cycles=6):
allMoves = set(list(combinations([-1, 0, 1]*dimensions, dimensions)))
nullMove = set([tuple([0]*dimensions)])
moves = list(allMoves - nullMove)
active = set()
for x in range(len(rawGrid)):
for y in range(len(rawGrid[0])):
if rawGrid[x][y] == "#":
pos = tuple([x, y] + [0]*(dimensions - 2))
active.add(pos)
for cycle in range(cycles):
toBeUpdated = set(active)
for pos in active:
for delta in moves:
neighbor = tuple(k+d for k, d in zip(pos, delta))
toBeUpdated.add(neighbor)
newActive = set(active)
for pos in toBeUpdated:
neighbors = 0
for delta in moves:
neighbor = tuple(k+d for k, d in zip(pos, delta))
neighbors += int(neighbor in active)
if pos in active and neighbors not in [2, 3]:
newActive.remove(pos)
elif pos not in active and neighbors == 3:
newActive.add(pos)
active = newActive
return len(active)
################################
rawGrid = AOCUtils.loadInput(17)
print("Part 1: {}".format(conwayCubes(rawGrid, 3)))
print("Part 2: {}".format(conwayCubes(rawGrid, 4)))
AOCUtils.printTimeTaken()