From b8e6840a1780b4358bbd7c483720a14c87d93788 Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Sun, 5 Dec 2021 20:15:02 -0800 Subject: solution for day 5 --- day5.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 day5.py 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)) -- cgit 1.4.1-2-gfad0