-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday17.py
executable file
·48 lines (31 loc) · 1.08 KB
/
day17.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
#!/usr/bin/env python3
import sys
from itertools import product
def neighbors(coords):
ranges = ((c - 1, c, c + 1) for c in coords)
yield from product(*ranges)
def all_neighbors(cube):
return set(n for p in cube for n in neighbors(p))
def alive_neighbors(cube, coords):
return sum(p in cube for p in neighbors(coords)) - (coords in cube)
def evolve(cube):
new = set()
for p in all_neighbors(cube):
alive = alive_neighbors(cube, p)
if alive == 3 or (alive == 2 and p in cube):
new.add(p)
return new
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
grid = tuple(map(str.rstrip, fin))
h, w = len(grid), len(grid[0])
cube = set((x, y, 0) for x, y in product(range(h), range(w)) if grid[x][y] == '#')
for _ in range(6):
cube = evolve(cube)
total_alive = len(cube)
print('Part 1:', total_alive)
hypercube = set((x, y, 0, 0) for x, y in product(range(h), range(w)) if grid[x][y] == '#')
for _ in range(6):
hypercube = evolve(hypercube)
total_alive = len(hypercube)
print('Part 2:', total_alive)