-
Notifications
You must be signed in to change notification settings - Fork 0
/
5b.py
59 lines (44 loc) · 1.37 KB
/
5b.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
from os import path
INPUT = path.join("input", "5.txt")
def traverse(line, map):
current = line[0].copy()
vector = [0, 0]
if line[0][0] > line[1][0]:
vector[0] = -1
elif line[0][0] < line[1][0]:
vector[0] = 1
if line[0][1] > line[1][1]:
vector[1] = -1
elif line[0][1] < line[1][1]:
vector[1] = 1
while (current[0] != line[1][0]) or (current[1] != line[1][1]):
map[current[0]][current[1]] += 1
current[0] += vector[0]
current[1] += vector[1]
map[current[0]][current[1]] += 1 # get the last one
def main():
max_x = 0
max_y = 0
lines = []
with open(INPUT, "r") as in_file:
for row in in_file:
coords = row.split(" -> ")
line = []
for coord in coords:
line += [[int(x) for x in coord.split(",")]]
if int(coord.split(",")[0]) > max_x:
max_x = int(coord.split(",")[0])
if int(coord.split(",")[1]) > max_y:
max_y = int(coord.split(",")[1])
lines.append(line)
map = [[0 for _ in range(max_y + 1)] for _ in range(max_x + 1)]
for line in lines:
traverse(line, map)
score = 0
for row in map:
for item in row:
if item >= 2:
score += 1
print(score)
if __name__ == "__main__":
main()