summaryrefslogtreecommitdiff
path: root/17_2020.py
diff options
context:
space:
mode:
Diffstat (limited to '17_2020.py')
-rw-r--r--17_2020.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/17_2020.py b/17_2020.py
new file mode 100644
index 0000000..fdd889a
--- /dev/null
+++ b/17_2020.py
@@ -0,0 +1,38 @@
+import fileinput
+import re
+import itertools
+
+
+def solve(p1):
+ ON = set()
+ L = list([l.strip() for l in fileinput.input()])
+ for r, l in enumerate(L):
+ for c, ch in enumerate(l):
+ if ch == "#":
+ ON.add((r, c, 0, 0))
+
+ for _ in range(6):
+ NEW_ON = set()
+ CHECK = set()
+ for x, y, z, w in ON:
+ for dx, dy, dz, dw in itertools.product([-1, 0, 1], repeat=4):
+ if w + dw == 0 or (not p1):
+ CHECK.add((x + dx, y + dy, z + dz, w + dw))
+
+ for x, y, z, w in CHECK:
+ nbr = 0
+ for dx, dy, dz, dw in itertools.product([-1, 0, 1], repeat=4):
+ if dx != 0 or dy != 0 or dz != 0 or dw != 0:
+ if (x + dx, y + dy, z + dz, w + dw) in ON:
+ nbr += 1
+ if (x, y, z, w) not in ON and nbr == 3:
+ NEW_ON.add((x, y, z, w))
+ if (x, y, z, w) in ON and nbr in [2, 3]:
+ NEW_ON.add((x, y, z, w))
+ ON = NEW_ON
+
+ return len(ON)
+
+
+print(solve(True))
+print(solve(False))