#!/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))