Created
December 17, 2020 07:52
-
-
Save kung-foo/ef99d2154eec2faba91bfefd60e6be3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from itertools import product | |
from copy import deepcopy | |
src = open("input.txt", "r").readlines() | |
# src = """ | |
# .#. | |
# ..# | |
# ###""".splitlines() | |
src = [r.strip() for r in src if r.strip()] | |
sz = len(src) | |
dim = 4 | |
dirs = list(product([-1, 0, 1], repeat=dim)) | |
dirs.remove(tuple([0] * dim)) | |
start = set() | |
for y, r in enumerate(src): | |
for x, c in enumerate(r): | |
if c == "#": | |
if dim == 3: | |
start.add((0, y, x)) | |
else: | |
start.add((0, 0, y, x)) | |
def adj(p): | |
for d in dirs: | |
if dim == 3: | |
yield p[0] + d[0], p[1] + d[1], p[2] + d[2] | |
else: | |
yield p[0] + d[0], p[1] + d[1], p[2] + d[2], p[3] + d[3] | |
def inactives(points): | |
s = set() | |
for p in points: | |
for a in adj(p): | |
if a not in points: | |
s.add(a) | |
return s | |
def neighbors(p, points): | |
n = 0 | |
for a in adj(p): | |
if a in points: | |
n += 1 | |
if n > 3: | |
return n | |
return n | |
def turn(points): | |
cp = deepcopy(points) | |
for p in points: | |
if neighbors(p, points) not in [2, 3]: | |
cp.remove(p) | |
for p in inactives(points): | |
if neighbors(p, points) == 3: | |
cp.add(p) | |
return cp | |
board = start | |
for _ in range(6): | |
board = turn(board) | |
print(len(board)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment