summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-05 20:15:02 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-05 20:15:02 -0800
commitb8e6840a1780b4358bbd7c483720a14c87d93788 (patch)
tree1b0e393524d381123e605f133c8e590d39a2d351
parentf248d715d4a3c19f84475fcb6d076a6874fcf7e0 (diff)
downloadAdventOfCode2021-b8e6840a1780b4358bbd7c483720a14c87d93788.tar.gz
solution for day 5
-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))