summaryrefslogtreecommitdiffstats
path: root/5.py
blob: 1e61efbeff03a2d1c55fec276684c0d30baa6835 (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
38
39
from collections import defaultdict, namedtuple
from utils import open_day

Pos = namedtuple('Pos', ['x', 'y'])

vents = [
    tuple(
        Pos(*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()))