From 7598330ed38d5f69cee25894115529322c1ec2c6 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Wed, 8 Dec 2021 21:34:12 +0000 Subject: [PATCH] Do day 5 --- 5_1.py | 18 ++++++++++++++++++ 5_2.py | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 5_1.py create mode 100644 5_2.py diff --git a/5_1.py b/5_1.py new file mode 100644 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 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))) -- 2.46.2