-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.py
75 lines (63 loc) · 2.28 KB
/
5.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
INPUT_FILE = f"input/{__file__.split('.')[0].rstrip('b')}"
def prettyPrint(array):
for i in array:
print(" ".join(map(str, i)))
def myRange(start, stop):
out = []
for i in range(start, stop + 1):
out.append(i)
return out
def getInput():
with open(INPUT_FILE, 'r') as f:
return f.read().splitlines()
def partOne(partTwo):
inp = getInput()
maxX = maxY = 0
graph = []
# First we need to figure out the dimensions of the final array
for line in inp:
line = line.split(" -> ")
y1 = int(line[0].split(',')[0])
y2 = int(line[1].split(',')[0])
x1 = int(line[0].split(',')[1])
x2 = int(line[1].split(',')[1])
x1 = x1 if x1 > x2 else x2
y1 = y1 if y1 > y2 else y2
maxX = maxX if maxX > x1 else x1
maxY = maxY if maxY > y1 else y1
# Then we create the graph with all dots for untraversed items
for i in range(0, maxY + 1):
graph.insert(i, ['.'] * (maxX + 1))
# Then we figure out what crosses where
for line in inp:
line = line.split(" -> ")
y1 = int(line[0].split(',')[0])
y2 = int(line[1].split(',')[0])
x1 = int(line[0].split(',')[1])
x2 = int(line[1].split(',')[1])
if x1 == x2:
for i in myRange(min(y1, y2), max(y1, y2)):
graph[x1][i] = 1 if graph[x1][i] == "." else graph[x1][i] + 1
elif y1 == y2:
for i in myRange(min(x1, x2), max(x1, x2)):
graph[i][y1] = 1 if graph[i][y1] == "." else graph[i][y1] + 1
elif partTwo:
i = x1
j = y1
xBound = x2 + 1 if x1 < x2 else x2 - 1
yBound = y2 + 1 if y1 < y2 else y2 - 1
while i != xBound and j != yBound:
graph[i][j] = 1 if graph[i][j] == "." else graph[i][j] + 1
i = i + 1 if x1 < x2 else i - 1
j = j + 1 if y1 < y2 else j - 1
overlaps = 0
for row in graph:
for col in row:
if col != "." and col > 1:
overlaps += 1
return overlaps
if __name__ == "__main__":
one = partOne(False)
two = partOne(True)
print(f"Part one: {one}")
print(f"Part two: {two}")