-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-5.2.py
36 lines (29 loc) · 968 Bytes
/
day-5.2.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
import re
data = None
with open("input.txt",encoding="utf-8") as inpf:
data = [[y.strip() for y in x.split("->")] for x in inpf.read().split("\n") if x.strip()]
data = [[tuple([int(z) for z in y.split(",")]) for y in x] for x in data]
maxx = max([max([y[0] for y in x]) for x in data])
maxy = max([max([y[1] for y in x]) for x in data])
floor = [[0]*(maxx+1) for x in range(maxy+1)]
for d in data:
if d[0][0] == d[1][0] or d[0][1] == d[1][1]:
x1 = min(d[0][0],d[1][0])
x2 = max(d[0][0],d[1][0])
y1 = min(d[0][1],d[1][1])
y2 = max(d[0][1],d[1][1])
for x in range(x1,x2+1):
for y in range(y1,y2+1):
floor[y][x] += 1
else:
x1 = d[0][0]
x2 = d[1][0]
y1 = d[0][1]
y2 = d[1][1]
while(x1 != x2 and y1 != y2):
floor[y1][x1] += 1
x1 = x1 + (1 if x1<x2 else -1)
y1 = y1 + (1 if y1<y2 else -1)
floor[y1][x1] += 1
answer = sum([len([g for g in f if g > 1]) for f in floor])
print(answer)