-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021-12-05-aoc.py
38 lines (34 loc) · 1 KB
/
2021-12-05-aoc.py
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
from aocd import get_data
import numpy as np
import re
def get_points(x1,y1,x2,y2):
if x1 == x2:
y1, y2 = min(y1, y2), max(y1, y2)
return [(x1,y) for y in range(y1, y2+1)]
if y1 == y2:
x1, x2 = min(x1, x2), max(x1, x2)
return [(x, y1) for x in range(x1, x2+1)]
else:
a = x1 < x2
b = y1 < y2
xx2 = x2+1 if a else x2 -1
yy2 = y2+1 if b else y2-1
r1 = 1 if a else -1
r2 = 1 if b else -1
return [(x,y) for (x,y) in zip(range(x1, xx2, r1), range(y1, yy2, r2))]
def challenge():
data = get_data(day = 5)
n = 0
pipe_points = {}
for d in data.split("\n"):
x1, y1, x2, y2 = re.split(r"\,| -> ", d)
p = get_points(int(x1), int(y1), int(x2), int(y2))
for pp in p:
if pp in pipe_points:
pipe_points[pp] += 1
if pipe_points[pp] == 2:
n += 1
else:
pipe_points[pp] = 1
print(n)
challenge()