aboutsummaryrefslogtreecommitdiff
path: root/2015/18/puzzle-1.py
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
committerThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
commite7c9108b95e39d7ea5a29ae06d619c4727f11027 (patch)
tree237261eef3afd0720be77dbcbb9599fa66a24b67 /2015/18/puzzle-1.py
Initial commit
Diffstat (limited to '2015/18/puzzle-1.py')
-rwxr-xr-x2015/18/puzzle-1.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/2015/18/puzzle-1.py b/2015/18/puzzle-1.py
new file mode 100755
index 0000000..ef6d36e
--- /dev/null
+++ b/2015/18/puzzle-1.py
@@ -0,0 +1,49 @@
+#!/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]]:
+ ndata = [["." for i in range(100)] for j in range(100)]
+
+ for i in range(100):
+ for j in range(100):
+ if data[i][j] == "#" and neighbours(data, i, j) in [2, 3]:
+ ndata[i][j] = "#"
+ elif data[i][j] == "." and neighbours(data, i, j) == 3:
+ ndata[i][j] = "#"
+
+ return ndata
+
+def main() -> None:
+ with open("input", "r", encoding="utf-8") as f:
+ data = [list(l.strip()) for l in f.readlines()]
+
+ for i in range(100):
+ data = simulate(data)
+
+ print(sum(data[i].count("#") for i in range(100)))
+
+
+if __name__ == "__main__":
+ main()