]> git.friedersdorff.com Git - max/advent_of_code_2021.git/commitdiff
Do day 5
authorMaximilian Friedersdorff <max@friedersdorff.com>
Wed, 8 Dec 2021 21:34:12 +0000 (21:34 +0000)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Wed, 8 Dec 2021 21:34:12 +0000 (21:34 +0000)
5_1.py [new file with mode: 0644]
5_2.py [new file with mode: 0644]

diff --git a/5_1.py b/5_1.py
new file mode 100644 (file)
index 0000000..189124c
--- /dev/null
+++ b/5_1.py
@@ -0,0 +1,18 @@
+"""Find the dangerous vents"""
+import numpy as np
+
+vent_field = np.zeros((1000, 1000))
+
+with open("5_input.txt") as f:
+    for line in f:
+        p1, p2 = line.split("->")
+        x1, y1, x2, y2 = [int(x) for x in p1.split(",") + p2.split(",")]
+
+        if x1 == x2:
+            for y in range(min(y1, y2), max(y1, y2) + 1):
+                vent_field[x1][y] += 1
+        elif y1 == y2:
+            for x in range(min(x1, x2), max(x1, x2) + 1):
+                vent_field[x][y1] += 1
+
+print(len(np.argwhere(vent_field > 1)))
diff --git a/5_2.py b/5_2.py
new file mode 100644 (file)
index 0000000..54d9a86
--- /dev/null
+++ b/5_2.py
@@ -0,0 +1,26 @@
+"""Find the dangerous vents"""
+import numpy as np
+
+vent_field = np.zeros((1000, 1000))
+
+with open("5_input.txt") as f:
+    for line in f:
+        p1, p2 = line.split("->")
+        x1, y1, x2, y2 = [int(x) for x in p1.split(",") + p2.split(",")]
+
+        if x1 == x2:
+            for y in range(min(y1, y2), max(y1, y2) + 1):
+                vent_field[x1][y] += 1
+        elif y1 == y2:
+            for x in range(min(x1, x2), max(x1, x2) + 1):
+                vent_field[x][y1] += 1
+        else:
+            m = (y2 - y1) // (x2 - x1)
+            c = y1 - (m * x1)
+            for x in range(min(x1, x2), max(x1, x2) + 1):
+                y = (m * x) + c
+                vent_field[x][y] += 1
+
+
+
+print(len(np.argwhere(vent_field > 1)))