summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--day5.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/day5.py b/day5.py
new file mode 100644
index 0000000..f0eb483
--- /dev/null
+++ b/day5.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+import numpy as np
+import re
+
+with open("day5.txt") as data:
+    coords = []
+    xMax = yMax = 0
+    for line in data:
+        x1, y1, x2, y2 = map(int, re.split(r'\D+', line.strip()))
+        xMax = max(xMax, max(x1, x2))
+        yMax = max(yMax, max(y1, y2))
+        if x1 == x2:
+            ystep = 1 if y1 < y2 else -1
+            coords.extend([(x1, y) for y in range(y1, y2 + ystep, ystep)])
+        elif y1 == y2:
+            xstep = 1 if x1 < x2 else -1
+            coords.extend([(x, y1) for x in range(x1, x2 + xstep, xstep)])
+        else:
+            xstep = 1 if x1 < x2 else -1
+            ystep = 1 if y1 < y2 else -1
+            coords.extend([coord for coord in zip(range(x1, x2 + xstep, xstep), range(y1, y2 + ystep, ystep))])
+
+    vents = np.zeros((xMax+1, yMax+1)).astype(int)
+    for x, y in coords:
+        vents[x, y] += 1
+    print(np.count_nonzero(vents > 1))