-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday16.2.py
328 lines (255 loc) · 9.54 KB
/
day16.2.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# Teil 2
# Finally SUCCESSFUL!!!
file = "AOC_day16_input.txt"
document = []
directions = {"L": (0, -1), "R": (0, 1), "U": (-1, 0), "D": (1, 0)}
def einlesen(filename):
"""
liest alle Zeilen eines Textfiles ein und speichert jede einzelne Zeile in einer Liste
"""
with open(filename, "r") as file:
# read file line by line and output the lines
for line in file:
line = line.strip()
document.append(line)
return document
def print_field(matrix):
# danke an
# https://plainenglish.io/blog/a-python-example-of-the-flood-fill-algorithm-bced7f96f569
# this function will print the contents of the array
for y in range(len(matrix)):
for x in range(len(matrix[0])):
# value by column and row
print(matrix[y][x], end=' ')
if x == len(matrix[0])-1:
print(" ")
def matrix_in_datei_schreiben(matrix, dateiname):
"""
Schreibt die Matrix in eine Textdatei
"""
with open(dateiname, "w") as file:
for zeile in matrix:
file.write(' '.join(zeile) + '\n')
def merge_matrices(input_matrix, output_matrix):
""" Führt die # aus der output_matrix mit der input_matrix zusammen und gibt eine dritte Matrix aus. """
merged_matrix = []
for y in range(len(output_matrix)):
# Konvertiere die Zeile der input_matrix in eine Liste von Zeichen
input_row_list = list(input_matrix[y])
for x in range(len(output_matrix[0])):
if output_matrix[y][x] == "#" and input_matrix[y][x] == ".":
input_row_list[x] = "#"
# Konvertiere die modifizierte Zeilenliste zurück in einen String
merged_matrix.append(''.join(input_row_list))
return merged_matrix
def matrix_copy(matrix):
new_line = ""
new_matrix = []
for line in matrix:
new_line = "."*len(line)
new_matrix.append(new_line)
return new_matrix
def array_size(matrix):
"""ermittelt die Größe einer Matrix-Liste"""
rows = len(matrix)
cols = len(matrix[0])
return rows, cols
def walk(current_pos, direction):
"""
ermittelt die nächste Position ausgehend von einer current_position
"""
d = direction
dist = 1
dr, dc = directions[d]
new_pos = (current_pos[0] + dr * dist, current_pos[1] + dc * dist) #dr delta row, dc delta cols
return new_pos
def energize(pos):
"""schreibt "#" an die position im ergebnisdokument"""
string_zeile = ""
row, col = pos
string_zeile = erg_document[row][:col] + "#" + erg_document[row][col + 1:]
erg_document[row] = string_zeile
def count_energized_tiles(matrix):
t_val = 0
for line in matrix:
l_val = line.count("#")
t_val = t_val + l_val
return t_val
def beam_splitten(pos, vektor):
if pos in pos_liste and dir_liste[pos_liste.index(pos)] == vektor:
hdj = 1
else:
pos_liste.append(pos) #aktuelle position in die pos_liste eintragn
dir_liste.append(vektor) #zugehörigen vektor in die Ricthungsliste eintragen
def get_next_direction(pos, direction):
row, col = pos
# zeichen gibt an, was der Beam antrifft
zeichen = document[row][col]
# print("Zeichen gefunden", zeichen, "an Feld", pos)
vektor = direction
if direction == "R":
if zeichen == "/":
vektor = "U"
elif zeichen == "\\":
vektor = "D"
elif zeichen == "|":
vektor = "D" #vektor für den neuen Beam
beam_splitten(pos, vektor)
vektor = "U" #vektor für den "alten" beam
elif direction == "L":
if zeichen == "/":
vektor = "D"
elif zeichen == "\\":
vektor = "U"
elif zeichen == "|":
vektor = "D"
beam_splitten(pos, vektor)
vektor = "U" #vektor für den "alten" beam
elif direction == "D":
if zeichen == "/":
vektor = "L"
elif zeichen == "\\":
vektor = "R"
elif zeichen == "-":
vektor = "R"
beam_splitten(pos, vektor)
vektor = "L" #vektor für den "alten" beam
elif direction == "U":
if zeichen == "/":
vektor = "R"
elif zeichen == "\\":
vektor = "L"
elif zeichen == "-":
vektor = "R"
beam_splitten(pos, vektor)
vektor = "L" #vektor für den "alten" beam
new_direction = vektor
return new_direction
def main_loop(start_pos, start_dir):
i = 0
ze_count = 0
en_tiles = 0
for item in pos_liste:
# beide listen lesen für den nächsten Beam
row, col = item
pos = item
drx = dir_liste[i]
ze = 0
while True:
# if ze%10 == 0:
# print("Cycle", i, "Pos", item, "durchlauf", ze)
# print("pos", pos, "row", row, "col", col, "dir", drx)
energize(pos) # im Ergebnisdokument die alte Position markieren
n_drx = get_next_direction(pos, drx) #ermittle mir zur position die Richtung
# print("new direction", n_drx)
n_pos = walk(pos, n_drx) # gehe in diese Richtung
# print("new position after walk", n_pos)
# position und richtung auf die neuen Werte setzen
drx = n_drx
pos = n_pos
row, col = pos
ze +=1
if row < 0 or row >= max_rows or col < 0 or col >= max_cols:
# print("Und raus aus der Matrix für", item)
# print(" ")
break
# print("pos", pos, "row", row, "col", col, "dir", drx)
energize(pos) # im Ergebnisdokument die neue Position markieren
if ze > abbruch_cycles:
ze_count += 1
print("-->", abbruch_cycles, "erreicht")
break
i += 1
if i > abbruch_cycles:
print("--> ABBRUCH", abbruch_cycles, "erreicht")
break
en_tiles = count_energized_tiles(erg_document)
print(" ")
print("Energized Tiles:", en_tiles)
print("anzahl abbrüche innere Schleife", ze_count)
return en_tiles, ze_count
####
einlesen(file)
max_rows, max_cols = array_size(document)
print("Die Ursprungsmatrix ist", max_rows, "x", max_cols, "groß")
print(" ")
#print("Vorgabe")
#print_field(document)
erg_document = matrix_copy(document)
abbruch_cycles = 10000
max_energ_tiles = 0
mmax_tiles = 0
start_position = [0, 0]
start_direction = "R"
for rr in range(max_rows):
start_position = [rr, 0]
pos_liste = []
dir_liste = []
pos_liste.append(start_position)
dir_liste.append(start_direction)
energ_tiles, inner_abbr = main_loop(start_position, start_direction)
print("Number of energized tiles with start pos", start_position, start_direction, inner_abbr, ":", energ_tiles)
if energ_tiles > max_energ_tiles:
max_energ_tiles = energ_tiles
erg_document = matrix_copy(document)
print(" ")
print("maximale Anzahle energized tiles", max_energ_tiles)
if max_energ_tiles > mmax_tiles:
mmax_tiles = max_energ_tiles
start_position = [0, max_cols-1]
start_direction = "L"
for rr in range(max_rows):
start_position = [rr, max_rows-1]
pos_liste = []
dir_liste = []
pos_liste.append(start_position)
dir_liste.append(start_direction)
energ_tiles, inner_abbr = main_loop(start_position, start_direction)
print("Number of energized tiles with start pos", start_position, start_direction, inner_abbr, ":", energ_tiles)
if energ_tiles > max_energ_tiles:
max_energ_tiles = energ_tiles
erg_document = matrix_copy(document)
print(" ")
print("maximale Anzahle energized tiles", max_energ_tiles)
if max_energ_tiles > mmax_tiles:
mmax_tiles = max_energ_tiles
start_position = [0, 0]
start_direction = "D"
for cc in range(max_cols):
start_position = [0, cc]
pos_liste = []
dir_liste = []
pos_liste.append(start_position)
dir_liste.append(start_direction)
energ_tiles, inner_abbr = main_loop(start_position, start_direction)
print("Number of energized tiles with start pos", start_position, start_direction, inner_abbr, ":", energ_tiles)
if energ_tiles > max_energ_tiles:
max_energ_tiles = energ_tiles
erg_document = matrix_copy(document)
print(" ")
print("maximale Anzahle energized tiles", max_energ_tiles)
if max_energ_tiles > mmax_tiles:
mmax_tiles = max_energ_tiles
start_position = [max_rows-1, 0]
start_direction = "U"
for cc in range(max_cols):
start_position = [max_rows-1, cc]
pos_liste = []
dir_liste = []
pos_liste.append(start_position)
dir_liste.append(start_direction)
energ_tiles, inner_abbr = main_loop(start_position, start_direction)
print("Number of energized tiles with start pos", start_position, start_direction, inner_abbr, ":", energ_tiles)
if energ_tiles > max_energ_tiles:
max_energ_tiles = energ_tiles
erg_document = matrix_copy(document)
print(" ")
print("maximale Anzahle energized tiles", max_energ_tiles)
if max_energ_tiles > mmax_tiles:
mmax_tiles = max_energ_tiles
print("TOTAL ENERGIZABLE TILES", mmax_tiles)
#print("Kontrollmatrix")
#kontroll_matrix = merge_matrices(document, erg_document)
#print_field(kontroll_matrix)
#matrix_in_datei_schreiben(erg_document, "AOC_day16_output.txt")
#matrix_in_datei_schreiben(kontroll_matrix, "AOC_day16_kontroll_matrix.txt")