aboutsummaryrefslogtreecommitdiff
path: root/2015/18/puzzles.py
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-02 09:58:43 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-02 09:58:43 +0100
commit7fb03d975972887cdc42259bd849e9b808f72b7a (patch)
tree3aac16716a2be5b824ef6c77eba47034e531558e /2015/18/puzzles.py
parent641aa833290fb4ba76db692aa7346e6b136e8e34 (diff)
Merge both solutions into one file
Diffstat (limited to '2015/18/puzzles.py')
-rw-r--r--2015/18/puzzles.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/2015/18/puzzles.py b/2015/18/puzzles.py
new file mode 100644
index 0000000..5fbe572
--- /dev/null
+++ b/2015/18/puzzles.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+
+def neighbours(data: list[list[str]], x: int, y: int) -> int:
+ acc = 0
+
+ for x1, y1 in [
+ (x - 1, y - 1),
+ (x - 1, y),
+ (x - 1, y + 1),
+ (x, y - 1),
+ (x, y + 1),
+ (x + 1, y - 1),
+ (x + 1, y),
+ (x + 1, y + 1),
+ ]:
+ try:
+ if x1 >= 0 and y1 >= 0 and data[x1][y1] == "#":
+ acc += 1
+ except IndexError:
+ pass
+
+ return acc
+
+
+def simulate(data: list[list[str]]) -> list[list[str]]:
+ # START PART 1
+ cond = lambda x, y: (data[x][y] == "#" and neighbours(data, x, y) in [2, 3]) or (
+ data[x][y] == "." and neighbours(data, x, y) == 3
+ )
+ # END PART 1
+ # START PART 2
+ cond = lambda x, y: (
+ ((i, j) in [(0, 0), (0, 99), (99, 0), (99, 99)])
+ or (data[x][y] == "#" and neighbours(data, x, y) in [2, 3])
+ or (data[x][y] == "." and neighbours(data, x, y) == 3)
+ )
+ # END PART 2
+
+ return [["#" if cond(i, j) else "." for j in range(100)] for i in range(100)]
+
+
+def main() -> None:
+ with open("input", "r", encoding="utf-8") as f:
+ data = [list(l.strip()) for l in f.readlines()]
+
+ # START PART 2
+ data[0][0], data[0][99], data[99][0], data[99][99] = "#", "#", "#", "#"
+ # END PART 2
+
+ for i in range(100):
+ data = simulate(data)
+
+ print(sum(data[i].count("#") for i in range(100)))
+
+
+if __name__ == "__main__":
+ main()