summaryrefslogtreecommitdiffstats
path: root/5.py
blob: 802ccc7e8951c7f95c1d0729944d8a21e2d2cb31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()))