from collections import defaultdict, namedtuple from utils import open_day, Point2D vents = [ tuple( Point2D(*map(int, h.split(','))) for h in l.rstrip().split(' -> ') ) for l in open_day(5).read().rstrip().split('\n') ] def gen(frm, to): if to < frm: to = to - 1 increment = -1 else: to = to + 1 increment = 1 for i in range(frm, to, increment): yield i points = defaultdict(int) for vent in vents: if vent[0].x == vent[1].x: for y in gen(vent[0].y, vent[1].y): points[(vent[0].x, y)] += 1 elif vent[0].y == vent[1].y: for x in gen(vent[0].x, vent[1].x): points[(x, vent[0].y)] += 1 print(sum(n >= 2 for n in points.values())) for vent in vents: if vent[0].x != vent[1].x and vent[0].y != vent[1].y: for x, y in zip(gen(vent[0].x, vent[1].x), gen(vent[0].y, vent[1].y)): points[(x, y)] += 1 print(sum(n >= 2 for n in points.values()))