summary refs log tree commit diff stats
path: root/day6.py
diff options
context:
space:
mode:
Diffstat (limited to 'day6.py')
-rw-r--r--day6.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/day6.py b/day6.py
new file mode 100644
index 0000000..86c4abc
--- /dev/null
+++ b/day6.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+import numpy as np
+import re
+
+num_parse = re.compile(r'(toggle|off|on) (\d+,\d+) through (\d+,\d+)$')
+
+with open('day6.txt') as data:
+    instructions = [num_parse.search(line).groups((1, 2, 3)) for line in data]
+
+lights = np.zeros((1000, 1000), dtype=bool)
+for inst in instructions:
+    x1, y1 = map(int, inst[1].split(','))
+    x2, y2 = map(int, inst[2].split(','))
+    match inst[0]:
+        case 'toggle':
+            lights[x1:x2+1, y1:y2+1] = np.logical_not(lights[x1:x2+1, y1:y2+1])
+        case 'on':
+            lights[x1:x2+1, y1:y2+1] = True
+        case 'off':
+            lights[x1:x2+1, y1:y2+1] = False
+
+print(np.count_nonzero(lights))
+
+lights = np.zeros((1000, 1000), dtype=int)
+for inst in instructions:
+    x1, y1 = map(int, inst[1].split(','))
+    x2, y2 = map(int, inst[2].split(','))
+    match inst[0]:
+        case 'toggle':
+            lights[x1:x2+1, y1:y2+1] += 2
+        case 'on':
+            lights[x1:x2+1, y1:y2+1] += 1
+        case 'off':
+            region = lights[x1:x2+1, y1:y2+1]
+            region -= 1
+            region[region < 0] = 0
+
+print(np.sum(lights))