Compare commits
No commits in common. "94f8d2f4438da4edfe50449461756b6fa351f690" and "d1d2009929420ddf0ac6b2032285c62e26f09a6f" have entirely different histories.
94f8d2f443
...
d1d2009929
|
@ -1,47 +0,0 @@
|
|||
import sys
|
||||
from itertools import product
|
||||
|
||||
|
||||
def main():
|
||||
lines = [line.rstrip() for line in sys.stdin]
|
||||
|
||||
print("Actives:", solve(lines, 3))
|
||||
print("Hyperactives:", solve(lines, 4))
|
||||
|
||||
|
||||
def solve(lines, dimensions):
|
||||
s = {}
|
||||
for y, line in enumerate(lines):
|
||||
for x, c in enumerate(line):
|
||||
p = (x, y) + (0,) * (dimensions - 2)
|
||||
s[p] = 1 if c == "#" else 0
|
||||
for _ in range(6):
|
||||
cycle(s)
|
||||
return sum(s.values())
|
||||
|
||||
|
||||
def cycle(s):
|
||||
future = {}
|
||||
work_set = set(nn for pos in s for nn in near(*pos))
|
||||
for pos in work_set:
|
||||
an = 0
|
||||
for nn in near(*pos):
|
||||
an += s.get(nn, 0)
|
||||
if (v := s.get(pos, 0)) == 1 and not 2 <= an <= 3:
|
||||
future[pos] = 0
|
||||
elif v == 0 and an == 3:
|
||||
future[pos] = 1
|
||||
for f, fv in future.items():
|
||||
s[f] = fv
|
||||
|
||||
|
||||
def near(*comp):
|
||||
num_comp = len(comp)
|
||||
offsets = list(product([-1, 0, 1], repeat=num_comp))
|
||||
offsets.remove((0,) * num_comp)
|
||||
for p in offsets:
|
||||
yield tuple(c + o for c, o in zip(comp, p))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue