-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.py
75 lines (69 loc) · 2.38 KB
/
day05.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from utils import get_input
import numpy as np
def part1(data):
n = max([max(line) for line in lines])
ocean = np.zeros((n+1,n+1))
for line in lines:
if line[0]==line[2]:
if line[3]>line[1]:
for i in range(line[1], line[3]+1):
ocean[line[0],i] += 1
else:
for i in range(line[3], line[1]+1):
ocean[line[0],i] += 1
if line[1]==line[3]:
if line[2]>line[0]:
for i in range(line[0], line[2]+1):
ocean[i,line[1]] += 1
else:
for i in range(line[2], line[0]+1):
ocean[i,line[1]] += 1
return np.sum(ocean>1)
def part2(data):
n = max([max(line) for line in lines])
ocean = np.zeros((n+1,n+1))
for line in lines:
if line[0]==line[2]:
if line[3]>line[1]:
for i in range(line[1], line[3]+1):
ocean[line[0],i] += 1
else:
for i in range(line[3], line[1]+1):
ocean[line[0],i] += 1
if line[1]==line[3]:
if line[2]>line[0]:
for i in range(line[0], line[2]+1):
ocean[i,line[1]] += 1
else:
for i in range(line[2], line[0]+1):
ocean[i,line[1]] += 1
if abs(line[0]-line[2])==abs(line[1]-line[3]):
diff_x = line[0]-line[2]
diff_y = line[1]-line[3]
if diff_x < 0:
for i in range(line[0],line[2]+1):
if diff_y < 0:
ocean[i,line[1]+i-line[0]] += 1
else:
ocean[i,line[1]-i+line[0]] += 1
else:
for i in range(line[2],line[0]+1):
if diff_y < 0:
ocean[i,line[1]-i+line[0]] += 1
else:
ocean[i,line[3]+i-line[2]] += 1
return np.sum(ocean>1)
def get_lines(data):
lines = []
for line in data:
start, end = line.split('->')
start_x, start_y = start.split(',')
end_x, end_y = end.split(',')
lines.append((int(start_x), int(start_y), int(end_x), int(end_y)))
return lines
if __name__ == "__main__":
day = 5
data = get_input(day)
lines = get_lines(data)
print(part1(lines))
print(part2(lines))