From: Maximilian Friedersdorff Date: Wed, 8 Dec 2021 21:34:12 +0000 (+0000) Subject: Do day 5 X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=7598330ed38d5f69cee25894115529322c1ec2c6;hp=bf9c96c93ff81123236abd0103ea20feae8e3142;p=max%2Fadvent_of_code_2021.git Do day 5 --- 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)))