2020 day 17
This commit is contained in:
parent
bb9e3321d7
commit
25443fba48
42
2020/d17.py
Normal file
42
2020/d17.py
Normal file
@ -0,0 +1,42 @@
|
||||
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))
|
@ -155,7 +155,8 @@ Solutions and utility script for Advent of Code challenges in Python.
|
||||
- Day 14: 40:26 (Made a bunch of mistakes even misunderstanding Python)
|
||||
- Day 15: 17:57 (Too slow for an easy one like this)
|
||||
- Day 16: 33:00 (Not too unhappy really.)
|
||||
- Day 17:
|
||||
- Day 17: 10:00 (40th)
|
||||
- Day 18:
|
||||
|
||||
## AoC 2022
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user