2020-12-17 18:44:15 +01:00
|
|
|
import sys
|
2020-12-17 18:48:54 +01:00
|
|
|
from itertools import product
|
2020-12-17 18:44:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-12-17 18:48:54 +01:00
|
|
|
lines = [line.rstrip() for line in sys.stdin]
|
2020-12-17 18:44:15 +01:00
|
|
|
|
2020-12-17 18:48:54 +01:00
|
|
|
print("Actives:", solve(lines, 3))
|
|
|
|
print("Hyperactives:", solve(lines, 4))
|
2020-12-17 18:44:15 +01:00
|
|
|
|
|
|
|
|
2020-12-17 18:48:54 +01:00
|
|
|
def solve(lines, dimensions):
|
2020-12-17 18:44:15 +01:00
|
|
|
s = {}
|
|
|
|
for y, line in enumerate(lines):
|
|
|
|
for x, c in enumerate(line):
|
2020-12-17 18:48:54 +01:00
|
|
|
p = (x, y) + (0,) * (dimensions - 2)
|
|
|
|
s[p] = 1 if c == "#" else 0
|
2020-12-17 18:44:15 +01:00
|
|
|
for _ in range(6):
|
|
|
|
cycle(s)
|
|
|
|
return sum(s.values())
|
|
|
|
|
|
|
|
|
|
|
|
def cycle(s):
|
|
|
|
future = {}
|
2020-12-17 18:48:54 +01:00
|
|
|
work_set = set(nn for pos in s for nn in near(*pos))
|
|
|
|
for pos in work_set:
|
2020-12-17 18:44:15 +01:00
|
|
|
an = 0
|
2020-12-17 18:48:54 +01:00
|
|
|
for nn in near(*pos):
|
2020-12-17 18:44:15 +01:00
|
|
|
an += s.get(nn, 0)
|
2020-12-17 18:48:54 +01:00
|
|
|
if (v := s.get(pos, 0)) == 1 and not 2 <= an <= 3:
|
|
|
|
future[pos] = 0
|
2020-12-17 18:44:15 +01:00
|
|
|
elif v == 0 and an == 3:
|
2020-12-17 18:48:54 +01:00
|
|
|
future[pos] = 1
|
2020-12-17 18:44:15 +01:00
|
|
|
for f, fv in future.items():
|
|
|
|
s[f] = fv
|
|
|
|
|
|
|
|
|
2020-12-17 18:48:54 +01:00
|
|
|
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))
|
2020-12-17 18:44:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|