-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.py
262 lines (224 loc) · 8.78 KB
/
day15.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import pprint
pp = pprint.PrettyPrinter()
import heapq as hq
def open_file(file):
with open(file, 'r+') as f:
lines = f.readlines()
output_lines = []
for line in lines:
output_lines.append(line.strip())
return output_lines
class Graph:
def __init__(self):
self.v = 0
self.graph = {}
def add_vertice(self, vertice):
self.graph[vertice] = []
self.v += 1
def add_edge(self, source, dest, cost):
self.graph[source].append({'dest': dest, 'cost': cost})
def generate_graph(input_array):
graph = Graph()
for row_no in range(len(input_array)):
for col_no in range(len(input_array[0])):
graph.add_vertice((row_no, col_no))
for row_no in range(len(input_array)):
for col_no in range(len(input_array[0])):
if row_no != 0: # adding top
graph.add_edge((row_no, col_no), (row_no-1, col_no), input_array[row_no-1][col_no])
if row_no != len(input_array)-1: # adding bottom
try:
graph.add_edge((row_no, col_no), (row_no+1, col_no), input_array[row_no+1][col_no])
except:
print (col_no, row_no)
if col_no != 0: # adding left
graph.add_edge((row_no, col_no), (row_no, col_no-1), input_array[row_no][col_no-1])
if col_no != len(input_array[0])-1: # adding right
graph.add_edge((row_no, col_no), (row_no, col_no+1), input_array[row_no][col_no+1])
return graph
# For learning purposes, the code below was my first attempt implementing Dijkstra without heapq
# def solver(part):
# input_file = open_file('input/day15_test.txt')
# input_array = []
# for line in input_file:
# input_array.append(list(map(int, list(line.strip()))))
# if part == 1:
# max_x = len(input_array[0])-1
# max_y = len(input_array)-1
# # graph.graph is adjacency list, where graph.graph[(x,y)] gives you [{'dest': (x2,y2), 'cost': 5}]
# graph = generate_graph(input_array)
# # Initiating the visits dictionary
# visits = {}
# for vertice in graph.graph:
# visits[vertice] = {}
# visits[vertice]['total_cost'] = float('inf')
# # visits[vertice]['prev_v'] = None
# visits[vertice]['done'] = False
# visits[(0,0)]['total_cost'] = 0
# visits[(0,0)]['done'] = True
# current_node = (0,0)
# while True:
# # Dijkstra's algorithm here
# for neighbor in graph.graph[current_node]:
# if visits[current_node]['total_cost'] + neighbor['cost'] < visits[neighbor['dest']]['total_cost']:
# visits[neighbor['dest']]['total_cost'] = visits[current_node]['total_cost'] + neighbor['cost']
# visits[current_node]['done'] = True
# # do check on whether to break or not here
# current_least_cost = float('inf')
# for vertice in visits:
# if visits[vertice]['total_cost'] < current_least_cost and visits[vertice]['done'] == False:
# current_least_cost = visits[vertice]['total_cost']
# if (visits[(max_x,max_y)]['total_cost'] <= current_least_cost):
# break
# # If not, set next node to check
# for vertice in visits:
# if visits[vertice]['total_cost'] == current_least_cost and visits[vertice]['done'] == False:
# current_node = vertice
# break
# if current_node[0]%10==0 and current_node[1]%10==0: print('Current Node: ', current_node, end='\r')
# return visits[(max_x,max_y)]['total_cost']
# elif part == 2:
# # pp.pprint(input_array)
# x_length = len(input_array[0])
# for i in range(1,5): # expanding horizontally
# for row in range(len(input_array)):
# for col in range(x_length):
# # print('current num', num)
# new_num = input_array[row][col] + i
# if new_num >= 10:
# new_num -= 9
# input_array[row].append(new_num)
# # print(input_array)
# print("@@@@@@")
# y_length = len(input_array)
# x_length = len(input_array[0])
# for i in range(1,5): # expanding vertically
# for row in range(y_length):
# new_row = []
# for col in range(x_length):
# new_num = input_array[row][col] + i
# if new_num >= 10:
# new_num -= 9
# new_row.append(new_num)
# input_array.append(new_row)
# # print(input_array)
# # print("########")
# max_x = len(input_array[0])-1
# max_y = len(input_array)-1
# # graph.graph is adjacency list, where graph.graph[(x,y)] gives you [{'dest': (x2,y2), 'cost': 5}]
# graph = generate_graph(input_array)
# # Initiating the visits dictionary
# visits = {}
# for vertice in graph.graph:
# visits[vertice] = {}
# visits[vertice]['total_cost'] = float('inf')
# # visits[vertice]['prev_v'] = None
# # visits[vertice]['done'] = False
# visits[(0,0)]['total_cost'] = 0
# visited = set()
# visited.add((0,0))
# # visits[(0,0)]['done'] = True
# current_node = (0,0)
# while True:
# # Dijkstra's algorithm here
# for neighbor in graph.graph[current_node]:
# if visits[current_node]['total_cost'] + neighbor['cost'] < visits[neighbor['dest']]['total_cost']:
# visits[neighbor['dest']]['total_cost'] = visits[current_node]['total_cost'] + neighbor['cost']
# # visits[current_node]['done'] = True
# visited.add((current_node))
# # do check on whether to break or not here
# # current_least_cost = float('inf')
# heap = []
# for vertice in visits:
# if vertice not in visited:
# hq.heappush(heap, (visits[vertice]['total_cost'], vertice))
# current_least_cost = heap[0][0]
# if (visits[(max_x,max_y)]['total_cost'] <= current_least_cost):
# break
# # If not, set next node to check
# current_node = heap[0][1]
# # for vertice in visits:
# # if visits[vertice]['total_cost'] == current_least_cost and visits[vertice]['done'] == False:
# # current_node = vertice
# # break
# if current_node[0]%10==0 and current_node[1]%10==0: print('Current Node: ', current_node, end='\r')
# return visits[(max_x,max_y)]['total_cost']
# Implementing Dijkstra WITH heapq
def solver(part):
input_file = open_file('input/day15.txt')
input_array = []
for line in input_file:
input_array.append(list(map(int, list(line.strip()))))
if part == 1:
max_x = len(input_array[0])-1
max_y = len(input_array)-1
# graph.graph is adjacency list, where graph.graph[(x,y)] gives you [{'dest': (x2,y2), 'cost': 5}]
graph = generate_graph(input_array)
# Initiating the distances dictionary
distances = {}
for vertex in graph.graph:
distances[vertex] = float('inf')
distances[(0,0)] = 0
heap = [(0, (0,0))]
while distances[(max_x,max_y)] > heap[0][0]:
current_dist, current_vertex = hq.heappop(heap)
if current_dist > distances[current_vertex]:
continue
# print(graph.graph)
for dest_dict in graph.graph[current_vertex]:
dest = dest_dict['dest']
cost = dest_dict['cost']
new_dist = current_dist + cost
if new_dist < distances[dest]:
distances[dest] = new_dist
hq.heappush(heap, (new_dist, dest))
return(distances[(max_x,max_y)])
elif part == 2:
# pp.pprint(input_array)
x_length = len(input_array[0])
for i in range(1,5): # expanding horizontally
for row in range(len(input_array)):
for col in range(x_length):
# print('current num', num)
new_num = input_array[row][col] + i
if new_num >= 10:
new_num -= 9
input_array[row].append(new_num)
# print(input_array)
y_length = len(input_array)
x_length = len(input_array[0])
for i in range(1,5): # expanding vertically
for row in range(y_length):
new_row = []
for col in range(x_length):
new_num = input_array[row][col] + i
if new_num >= 10:
new_num -= 9
new_row.append(new_num)
input_array.append(new_row)
# print(input_array)
# print("########")
max_x = len(input_array[0])-1
max_y = len(input_array)-1
graph = generate_graph(input_array)
distances = {}
for vertex in graph.graph:
distances[vertex] = float('inf')
distances[(0,0)] = 0
heap = [(0, (0,0))]
while distances[(max_x,max_y)] > heap[0][0]:
current_dist, current_vertex = hq.heappop(heap)
if current_dist > distances[current_vertex]:
continue
# print(graph.graph)
for dest_dict in graph.graph[current_vertex]:
dest = dest_dict['dest']
cost = dest_dict['cost']
new_dist = current_dist + cost
if new_dist < distances[dest]:
distances[dest] = new_dist
hq.heappush(heap, (new_dist, dest))
# print(distances)
return distances[(max_x,max_y)]
print("Part 1: ", solver(1))
print("Part 2: ", solver(2))