-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.py
94 lines (80 loc) · 2.62 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
input = open("day05_input.txt", "r").read().strip().split('\n')
def part1():
covered = dict()
for line in input:
first, sec = line.split(' -> ')
x1, y1 = map(int, first.split(','))
x2, y2 = map(int, sec.split(','))
if (x1 == x2):
maxn = max(y2, y1)
minn = min(y2, y1)
for num in range(minn, (maxn + 1)):
coordinate = (x1, num)
if coordinate in covered:
covered[coordinate] = covered[(x1, num)] + 1
else:
covered[coordinate] = 1
elif (y1 == y2):
maxn = max(x2, x1)
minn = min(x2, x1)
for num in range(minn, (maxn + 1)):
coordinate = (num, y1)
if coordinate in covered:
covered[coordinate] = covered[coordinate] + 1
else:
covered[coordinate] = 1
count = 0
for cov in covered:
if covered[cov] > 1:
count += 1
print(count)
def part2():
covered = dict()
for line in input:
first, sec = line.split(' -> ')
x1, y1 = map(int, first.split(','))
x2, y2 = map(int, sec.split(','))
if (x1 == x2):
maxn = max(y2, y1)
minn = min(y2, y1)
for num in range(minn, (maxn + 1)):
if (x1, num) in covered:
covered[(x1, num)] = covered[(x1, num)] + 1
else:
covered[(x1, num)] = 1
elif (y1 == y2):
maxn = max(x2, x1)
minn = min(x2, x1)
for num in range(minn, (maxn + 1)):
if (num, y1) in covered:
covered[(num, y1)] = covered[(num, y1)] + 1
else:
covered[(num, y1)] = 1
elif abs(x2 - x1) == abs(y2 - y1):
max_x = max(x2, x1)
min_x = min(x2, x1)
max_y = max(y2, y1)
min_y = min(y2, y1)
i = 0
for num in range(min_x, (max_x + 1)):
if (y1 < y2):
y = min_y + i
else:
y = max_y - i
if (x1 < x2):
x = min_x + i
else:
x = max_x - i
coordinate = (x, y)
if coordinate in covered:
covered[coordinate] = covered[coordinate] + 1
else:
covered[coordinate] = 1
i += 1
count = 0
for cov in covered:
if covered[cov] > 1:
count += 1
print(count)
part1()
part2()