-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
230 lines (188 loc) · 6.3 KB
/
day10.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
cadybaltz
12/10/2023
AoC 2023 Day 10
Part 1: 6856, 01:40:43
Part 2: 5940, 04:27:22
"""
import copy
import sys
# 674, 803, 995, 6815
start = None
def is_squeeze_up(x, y, graph):
if y + 1 >= len(graph[0]):
return False
up = {
'J': ['L', 'F', '|'],
'7': ['F', '|', 'L'],
'|': ['L', 'F', '|']
}
if graph[x][y] in up and graph[x][y + 1] in up[graph[x][y]]:
return True
return False
def is_squeeze_side(x, y, graph):
if x + 1 >= len(graph):
return False
side = {
'J': ['7', 'F', '-'],
'L': ['F', '-', '7'],
'-': ['7', 'F', '-']
}
if graph[x][y] in side and graph[x + 1][y] in side[graph[x][y]]:
return True
return False
def update_helper(count, coords, graph, distances):
if count < distances[coords[0]][coords[1]]:
update(count, coords, graph, distances)
def update(count, coords, graph, distances):
if graph[coords[0]][coords[1]] == '.':
return
distances[coords[0]][coords[1]] = min(count, distances[coords[0]][coords[1]])
curr = graph[coords[0]][coords[1]]
move = {
'|': [(1, 0), (-1, 0)],
'-': [(0, 1), (0, -1)],
'L': [(0, 1), (-1, 0)],
'J': [(-1, 0), (0, -1)],
'7': [(1, 0), (0, -1)],
'F': [(1, 0), (0, 1)]
}
for sym, moves in move.items():
if curr == sym:
for move in moves:
update_helper(count + 1, (coords[0] + move[0], coords[1] + move[1]), graph, distances)
if curr == 'S':
# left
if coords[1] > 0:
if graph[coords[0]][coords[1] - 1] in ['-', 'L', 'F']:
update_helper(count + 1, (coords[0], coords[1] - 1), graph, distances)
# right
if coords[1] < len(graph[0]) - 1:
if graph[coords[0]][coords[1] + 1] in ['-', 'J', '7']:
update_helper(count + 1, (coords[0], coords[1] + 1), graph, distances)
# up
if coords[0] > 0:
if graph[coords[0] - 1][coords[1]] in ['|', '7', 'F']:
update_helper(count + 1, (coords[0] - 1, coords[1]), graph, distances)
# down
if coords[0] < len(graph) - 1:
if graph[coords[0] + 1][coords[1]] in ['|', 'J', 'L']:
update_helper(count + 1, (coords[0] + 1, coords[1]), graph, distances)
return None
def solution(file):
lines = file.readlines()
start_coords = None
graph = []
distances = []
for x in range(len(lines)):
line = lines[x].strip()
r = []
r_n = []
for val in line:
r.append(val)
r_n.append(sys.maxsize)
graph.append(r)
distances.append(r_n)
# PART 1:
for x in range(len(graph)):
for y in range(len(graph[0])):
if graph[x][y] == 'S':
start_coords = (x, y)
update(0, start_coords, graph, distances)
maxi = 0
for x in distances:
for y in x:
if y != sys.maxsize:
maxi = max(y, maxi)
# PART 2:
pt_2_graph = []
for x in range(len(distances)):
row = []
for y in range(len(distances[0])):
# any location not part of the main loop should be considered
if distances[x][y] == sys.maxsize:
row.append(0)
else:
row.append(graph[x][y])
pt_2_graph.append(row)
# first expand horizontally
hori_expand = []
for x in range(len(pt_2_graph)):
row = []
for y in range(len(pt_2_graph[0])):
if pt_2_graph[x][y] == 0:
# only keep one 0 to remember how many spots were in the original
row.append(0)
row.append(-1)
elif is_squeeze_up(x, y, pt_2_graph):
# add empty space for parallel
row.append(pt_2_graph[x][y])
row.append(-1)
else:
row.append(pt_2_graph[x][y])
row.append(pt_2_graph[x][y])
hori_expand.append(row)
# now expand vertically too
expand = []
for x in range(len(hori_expand)):
row = []
new_row = []
for y in range(len(hori_expand[0])):
if hori_expand[x][y] == 0 or hori_expand[x][y] == -1:
row.append(hori_expand[x][y])
new_row.append(-1)
elif is_squeeze_side(x, y, hori_expand):
# add empty space for parallel
row.append(1)
new_row.append(-1)
else:
row.append(1)
new_row.append(1)
expand.append(row)
expand.append(new_row)
# start spreading the number 2 from the edges
for x in range(len(expand)):
# left to right
y = 0
while y < len(expand[0]):
if expand[x][y] == 1:
break
else:
expand[x][y] = 2
y += 1
# right to left
y = len(expand[0]) - 1
while y >= 0:
if expand[x][y] == 1:
break
else:
expand[x][y] = 2
y -= 1
# keep spreading 2 until there are no more changes to make
changed_last_iter = True
while changed_last_iter:
changed_last_iter = False
for x in range(len(expand)):
for y in range(len(expand[0])):
if expand[x][y] == 2:
directions = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
neighbors = [(x + r, y + c) for r, c in directions if
0 <= x + r < len(expand) and 0 <= y + c < len(expand[0])]
for neighbor in neighbors:
if expand[neighbor[0]][neighbor[1]] == 0 or expand[neighbor[0]][neighbor[1]] == -1:
changed_last_iter = True
expand[neighbor[0]][neighbor[1]] = 2
# count all of the original spaces that were not changed to 2
total = 0
for x in range(len(expand)):
for y in range(len(expand[0])):
if expand[x][y] == 0:
total += 1
return maxi, total
if __name__ == '__main__':
sys.setrecursionlimit(100000)
if sys.argv[1] == 't':
input = open("test.txt", "r")
else:
input = open("input.txt", "r")
print(solution(input))