-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.py
182 lines (157 loc) · 4.99 KB
/
Day5.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import numpy as np
# AoC Day 5 - Part 1
def gte_arr_coords(value, array):
# Defining a function to determine the index location of the value entered within an numpy array
# and if it is equal to or greater than specified value
crd = []
"""Finds the index location of the specified value within the specified array"""
xy = np.where(array >= value)
coords_list = zip(xy[0], xy[1])
for coord in coords_list:
crd.append(coord)
return crd
file1 = open("input5.txt", "r")
xy1 = []
xy2 = []
xy_all = []
count = 0
for line in file1:
str_split = line.replace("\n", "").split(" -> ")
str_len = len(str_split)
for i in range(0, str_len):
count += 1
if count % 2 == 0:
tmp = str_split[i].split(",")
map_xy2 = map(int, tmp)
tmp1 = list(map_xy2)
xy2.append(tmp1)
xy_all.append(tmp1[0])
xy_all.append(tmp1[1])
else:
tmp2 = str_split[i].split(",")
map_xy1 = map(int, tmp2)
tmp3 = list(map_xy1)
xy1.append(tmp3)
xy_all.append(tmp3[0])
xy_all.append(tmp3[1])
xy_len = len(xy1)
grid_len = max(xy_all) + 1
j = 0
hv = []
tmp = []
# Determining horizontal or vertical lines
for i in range(0, xy_len):
if xy1[i][0] == xy2[i][0]:
# x values are the same
if xy1[i][1] < xy2[i][1]:
tmp = []
for j in range(xy1[i][1], xy2[i][1]+1):
tmp.append([xy1[i][0], j])
elif xy2[i][1] < xy1[i][1]:
tmp = []
for j in range(xy2[i][1], xy1[i][1]+1):
tmp.append([xy2[i][0], j])
hv.append(tmp)
elif xy1[i][1] == xy2[i][1]:
# y values are the same
if xy1[i][0] < xy2[i][0]:
tmp = []
for j in range(xy1[i][0], xy2[i][0]+1):
tmp.append([j, xy1[i][1]])
elif xy2[i][0] < xy1[i][0]:
tmp = []
for j in range(xy2[i][0], xy1[i][0]+1):
tmp.append([j, xy2[i][1]])
hv.append(tmp)
# print(hv)
arr0 = np.zeros((grid_len, grid_len))
hv_flat = [item for sublist in hv for item in sublist]
for i in hv_flat:
y = i[1]
x = i[0]
arr0[y, x] += 1
coords = gte_arr_coords(2, arr0)
print(f"--- Part 1a ---\n"
f"The number of points where at least two lines overlap is: {len(coords)}\n")
# --- Part 2 ---
def diagonal_coord_increment(x1, x2, y1, y2):
"""To determine the diagonal incrementing coordinates please
enter the values in the order of x1, x2, y1, y2"""
if x1 < x2:
if y1 < y2:
temp1 = []
temp2 = []
for num in range(x1, x2 + 1):
temp1.append(num)
for num in range(y1, y2 + 1):
temp2.append(num)
r = [list(a) for a in zip(temp1, temp2)]
if y1 > y2:
temp1 = []
temp2 = []
for num in range(x1, x2 + 1):
temp1.append(num)
for num in range(y2, y1 + 1):
temp2.append(num)
temp2.reverse()
r = [list(a) for a in zip(temp1, temp2)]
elif x1 > x2:
if y1 < y2:
temp1 = []
temp2 = []
for num in range(x2, x1 + 1):
temp1.append(num)
for num in range(y1, y2 + 1):
temp2.append(num)
temp1.reverse()
r = [list(a) for a in zip(temp1, temp2)]
if y1 > y2:
temp1 = []
temp2 = []
for num in range(x2, x1 + 1):
temp1.append(num)
for num in range(y2, y1 + 1):
temp2.append(num)
temp1.reverse()
temp2.reverse()
r = [list(a) for a in zip(temp1, temp2)]
return r
xy_len = len(xy1)
grid_len = max(xy_all) + 1
j = 0
hv = []
tmp = []
for i in range(0, xy_len):
if xy1[i][0] == xy2[i][0]:
# x values are the same
if xy1[i][1] < xy2[i][1]:
tmp = []
for j in range(xy1[i][1], xy2[i][1]+1):
tmp.append([xy1[i][0], j])
elif xy2[i][1] < xy1[i][1]:
tmp = []
for j in range(xy2[i][1], xy1[i][1]+1):
tmp.append([xy2[i][0], j])
hv.append(tmp)
elif xy1[i][1] == xy2[i][1]:
# y values are the same
if xy1[i][0] < xy2[i][0]:
tmp = []
for j in range(xy1[i][0], xy2[i][0]+1):
tmp.append([j, xy1[i][1]])
elif xy2[i][0] < xy1[i][0]:
tmp = []
for j in range(xy2[i][0], xy1[i][0]+1):
tmp.append([j, xy2[i][1]])
hv.append(tmp)
else:
hv.append(diagonal_coord_increment(xy1[i][0], xy2[i][0], xy1[i][1], xy2[i][1]))
arr0 = np.zeros((grid_len, grid_len))
hv_flat = [item for sublist in hv for item in sublist]
for i in hv_flat:
y = i[1]
x = i[0]
arr0[y, x] += 1
coords = gte_arr_coords(2, arr0)
print(f"--- Part 1b ---\n"
f"The number of points where at least two lines overlap is: {len(coords)}\n")