2021-12-04 21:38:39 -08:00

54 lines 999 B Python Raw Permalink Blame History

 ```from collections import defaultdict ``` ``` ``` ``` ``` ```def range_incl(a, b): ``` ``` a, b = sorted([a, b]) ``` ``` while a <= b: ``` ``` yield a ``` ``` a += (1 if a <= b else -1) ``` ``` ``` ``` ``` ```def diag_range(x1, y1, x2, y2): ``` ``` while True: ``` ``` yield(x1, y1) ``` ``` if x1 == x2 and y1 == y2: ``` ``` return ``` ``` x1 += (1 if x1 <= x2 else -1) ``` ``` y1 += (1 if y1 <= y2 else -1) ``` ``` ``` ``` ``` ```with open("input.txt") as f: ``` ``` lines = [i.strip() for i in f.readlines()] ``` ``` ``` ``` ``` ```points = defaultdict(lambda: 0) ``` ``` ``` ```for line in lines: ``` ``` p1, p2 = line.split(" -> ") ``` ``` ``` ``` x1, y1 = p1.split(",") ``` ``` x2, y2 = p2.split(",") ``` ``` ``` ``` x1 = int(x1) ``` ``` y1 = int(y1) ``` ``` x2 = int(x2) ``` ``` y2 = int(y2) ``` ``` ``` ``` if x1 == x2 or y1 == y2: ``` ``` for xr in range_incl(x1, x2): ``` ``` for yr in range_incl(y1, y2): ``` ``` points[(xr, yr)] += 1 ``` ``` else: ``` ``` for xr, yr in diag_range(x1, y1, x2, y2): ``` ``` points[(xr, yr)] += 1 ``` ``` ``` ```overlaps = 0 ``` ``` ``` ```for point, hits in points.items(): ``` ``` if hits > 1: ``` ``` overlaps += 1 ``` ``` ``` ```print(overlaps) ``` ``` ``` ```# 26m ```