from itertools import product from lib import get_data from collections import defaultdict data = get_data(__file__) def neighbors(p): d = len(p) for xs in product(range(-1, 2), repeat=d): if all(x == 0 for x in xs): continue yield tuple([p[i] + xs[i] for i in range(len(xs))]) for d in [3, 4]: points = set() lines = data.splitlines() for y in range(len(lines)): for x in range(len(lines[0])): if lines[y][x] == "#": points.add(tuple([x, y] + [0] * (d - 2))) for _ in range(6): new_points = set() actives = defaultdict(int) for p in points: nbcount = 0 for nb in neighbors(p): actives[nb] += 1 if nb in points: nbcount += 1 if nbcount == 2 or nbcount == 3: new_points.add(p) for p, count in actives.items(): if p in points: continue elif count == 3: new_points.add(p) points = new_points print(len(points))