summaryrefslogtreecommitdiff
path: root/06/main.py
diff options
context:
space:
mode:
authorPrefetch2022-12-31 22:21:39 +0100
committerPrefetch2022-12-31 22:21:39 +0100
commit68615a9ad2c942254135cffb00cf25a84a3b1356 (patch)
tree1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /06/main.py
Initial commit
Diffstat (limited to '06/main.py')
-rwxr-xr-x06/main.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/06/main.py b/06/main.py
new file mode 100755
index 0000000..cf9f85c
--- /dev/null
+++ b/06/main.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+
+import numpy as np
+
+
+
+def solve_partn(partn, lines):
+ # In part 1, we treat an int32 as a boolean: 1 = True, 0 = False
+ grid = np.zeros((1000, 1000), dtype="int32")
+
+ for line in lines:
+ words = line.split()
+ if words[0] == "turn":
+ xi, yi = [int(n) for n in words[2].split(",")]
+ xf, yf = [int(n) + 1 for n in words[4].split(",")]
+
+ if partn == 1:
+ if words[1] == "on":
+ grid[xi : xf, yi : yf] = 1
+ elif words[1] == "off":
+ grid[xi : xf, yi : yf] = 0
+ else: # partn == 2
+ if words[1] == "on":
+ grid[xi : xf, yi : yf] += 1
+ elif words[1] == "off":
+ grid[xi : xf, yi : yf] -= 1
+ grid = np.clip(grid, 0, 1000000)
+
+ elif words[0] == "toggle":
+ xi, yi = [int(n) for n in words[1].split(",")]
+ xf, yf = [int(n) + 1 for n in words[3].split(",")]
+
+ if partn == 1:
+ grid[xi : xf, yi : yf] = 1 - grid[xi : xf, yi : yf] # NOT
+ else: # partn == 2
+ grid[xi : xf, yi : yf] += 2
+
+ return np.sum(grid)
+
+
+
+def main():
+ # Read instructions from input text file
+ with open("input.txt", "r") as f:
+ lines = f.read().splitlines()
+
+ print("Part 1 solution:", solve_partn(1, lines)) # 400410 for me
+ print("Part 2 solution:", solve_partn(2, lines)) # 15343601 for me
+
+
+
+if __name__ == "__main__":
+ main()