-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent_of_code_2017.py
385 lines (343 loc) · 14.4 KB
/
advent_of_code_2017.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# http://adventofcode.com/2017/about
from math import ceil
problem_1_input = \
"892195969991735837915273868729548694237967495115412399373194562526947585337233793568278265279199883197167" + \
"634791293177986152566236718332617536487236879747167999983363832257912445756887314879229925864477761357139" + \
"855548522513798899853896612387146687716264599943289416326727256525173953861534244979466587895429399159924" + \
"916364476319573895566795393368411672387263615582128377676293612892723762237191146714286233543514411813323" + \
"197995953854871628225358543514157867372265718724276911699514971458844849349726276329135118243155698271218" + \
"844347387457343656446381799296893222256198484465873714311777937421161581798189554141474236239447612421883" + \
"232173914183732126332838194648583472419154369952477422666389517569944428464617457124369349242479612422673" + \
"241361777576466946622932243728551273284837934497511114334421486262244982914734452113946361245377351849815" + \
"584855691778894798219822463298387771923329337634394654439458564233259451453345316753241438267739439225497" + \
"515276522424441532462541528195782818326918562247278496495764435386667383577543385186827269732261223156824" + \
"351164841648424564925198783625721396988984481558391866483955533972212164693898955412719161648411279149413" + \
"443192896864258215498543827458438871355879336892721675937111952479183496982825163456282747678364612135596" + \
"373533447719867384667516572262124225585623974278833981365494628646614588114147473559138853453189448624976" + \
"774641922469183942857695986376428944876851497914443873513862319484181787593572987444669767939526294424531" + \
"262999564948571142342741129862311311313166798363442745792896227642881893134498151552326647933689596516859" + \
"342242244584714818773791567187322217164347852843751875979415198165627534263527828414549217234322361937785" + \
"185174993256753483876378332521824515977173397535784236923629636713469151526399149548322849831431526219478" + \
"653861754364155275865511643923249858589466142474763778413826829226663398467569555747267195129525138917561" + \
"785436449855933951538973995881954521124753369223898312843734771532342383282987422334196585128526526324291" + \
"777689689492346231786335851551413876834969878"
def problem_1_a():
def parse_digits(digits):
"""
:param digits: str of digits
:return:
"""
total = 0
for i, c in enumerate(digits):
if int(digits[i]) == int(digits[(i + 1) % len(digits)]):
total += int(c)
return total
print("1122", parse_digits("1122"))
print("1111", parse_digits("1111"))
print("1234", parse_digits("1234"))
print("91212129", parse_digits("91212129"))
print(parse_digits(problem_1_input))
def problem_1_b():
def parse_digits(digits):
"""
:param digits: str of digits
:return:
"""
total = 0
len_digits = len(digits)
for i, c in enumerate(digits):
a = int(digits[i])
b = int(digits[(i + len_digits // 2) % len_digits])
if a == b:
total += int(c)
return total
for digits in ["1212", "1221", "123425", "123123", "12131415", problem_1_input]:
print(digits, parse_digits(digits))
puzzle_2_input = """
737 1866 1565 1452 1908 1874 232 1928 201 241 922 281 1651 1740 1012 1001
339 581 41 127 331 133 51 131 129 95 499 527 518 435 508 494
1014 575 1166 259 152 631 1152 1010 182 943 163 158 1037 1108 1092 887
56 491 409 1263 1535 41 1431 1207 1393 700 1133 53 131 466 202 62
632 403 118 352 253 672 711 135 116 665 724 780 159 133 90 100
1580 85 1786 1613 1479 100 94 1856 546 76 1687 1769 1284 1422 1909 1548
479 356 122 372 786 1853 979 116 530 123 1751 887 109 1997 160 1960
446 771 72 728 109 369 300 746 86 910 566 792 616 84 338 57
6599 2182 200 2097 4146 7155 7018 1815 1173 4695 201 7808 242 3627 222 7266
1729 600 651 165 1780 2160 626 1215 149 179 1937 1423 156 129 634 458
1378 121 146 437 1925 2692 130 557 2374 2538 2920 2791 156 317 139 541
1631 176 1947 259 2014 153 268 752 2255 347 227 2270 2278 544 2379 349
184 314 178 242 145 410 257 342 183 106 302 320 288 151 449 127
175 5396 1852 4565 4775 665 4227 171 4887 181 2098 4408 2211 3884 2482 158
1717 3629 244 258 281 3635 235 4148 3723 4272 3589 4557 4334 4145 3117 4510
55 258 363 116 319 49 212 44 303 349 327 330 316 297 313 67
"""
def problem_2_a():
def calculate_checksum(text):
checksum = 0
for row in text.strip('\n').split('\n'):
sorted_values = sorted([int(c) for c in row.split('\t')])
chk = max(sorted_values) - min(sorted_values)
checksum += chk
print(chk)
return checksum
a = """
5 1 9 5
7 5 3
2 4 6 8
"""
print("Checksum:", calculate_checksum(a))
print("Checksum:", calculate_checksum(puzzle_2_input))
def problem_2_b():
def calculate_checksum(text):
checksum = 0
for row in text.strip('\n').split('\n'):
int_values = [int(c) for c in row.split('\t')]
for i, x in enumerate(int_values):
for j, y in enumerate(int_values):
if (x != y) and (x % y == 0):
chk = int(x / y)
print(chk)
checksum += chk
return checksum
a = """
5 9 2 8
9 4 7 3
3 8 6 5
"""
print("Checksum:", calculate_checksum(a))
print("Checksum:", calculate_checksum(puzzle_2_input))
def problem_3_a():
def build_grid(target_num, stop_at_num=True):
array_size = ceil(target_num ** 0.5)
if array_size % 2 == 0:
array_size += 1
a = []
for i in range(array_size):
a.append([" "]*array_size)
center = int((array_size) / 2)
a[center][center] = 1
count = 2
for i in range(2, target_num):
layer = i
length = layer * 2 - 1
start_x = center + layer - 1
start_y = center + max(0, layer - 2)
x = start_x
y = start_y
for j in range(length - 1):
a[y - j][x] = count
if count >= target_num and stop_at_num:
return a, x - center, (y - j) - center
count += 1
y -= length-2
for j in range(1, length):
a[y][x - j] = count
if count >= target_num and stop_at_num:
return a, (x - j) - center, y - center
count += 1
x -= length-1
for j in range(1, length):
a[y + j][x] = count
if count >= target_num and stop_at_num:
return a, x - center, (y + j) - center
count += 1
y += length - 1
for j in range(1, length):
a[y][x + j] = count
if count >= target_num and stop_at_num:
return a, (x + j) - center, y - center
count += 1
x += length - 1
if count > target_num:
break
return a, 0, 0
g, x, y = build_grid(265149)
# for line in g:
# print("|" + "|".join(["{0:>3}".format(c) for c in line]) + "|")
print([x, y])
print(abs(x) + abs(y))
def problem_3_b():
def print_grid(g):
for line in g:
print("|" + "|".join(["{0:>6}".format(c) for c in line]) + "|")
def get_value(grid, x, y):
def read_cell(grid, x, y):
if x < 0 or y < 0:
return 0
try:
x = grid[y][x]
except IndexError:
return 0
if x == " ":
return 0
return x
total = 0
total += read_cell(grid, x - 1, y - 1)
total += read_cell(grid, x - 1, y)
total += read_cell(grid, x - 1, y + 1)
total += read_cell(grid, x, y - 1)
total += read_cell(grid, x, y + 1)
total += read_cell(grid, x + 1, y - 1)
total += read_cell(grid, x + 1, y)
total += read_cell(grid, x + 1, y + 1)
return total
def build_grid(target_num, stop_at_num=True):
array_size = ceil(target_num ** 0.5)
if array_size % 2 == 0:
array_size += 1
a = []
for i in range(array_size):
a.append([" "]*array_size)
center = int((array_size) / 2)
a[center][center] = 1
count = 2
for i in range(2, target_num):
layer = i
length = layer * 2 - 1
x = center + layer - 1
y = center + max(0, layer - 2)
for j in range(length - 1):
total = get_value(a, x, y - j)
a[y - j][x] = total
if count >= target_num and stop_at_num:
return a, x - center, (y - j) - center
count += 1
y -= length-2
for j in range(1, length):
total = get_value(a, x - j, y)
a[y][x - j] = total
if count >= target_num and stop_at_num:
return a, (x - j) - center, y - center
count += 1
x -= length-1
for j in range(1, length):
total = get_value(a, x, y + j)
a[y + j][x] = total
if count >= target_num and stop_at_num:
return a, x - center, (y + j) - center
count += 1
y += length - 1
for j in range(1, length):
total = get_value(a, x + j, y)
a[y][x + j] = total
if count >= target_num and stop_at_num:
return a, (x + j) - center, y - center
count += 1
x += length - 1
if count > target_num:
break
return a, 0, 0
g, x, y = build_grid(81)
print_grid(g)
print([x, y])
print(abs(x) + abs(y))
def problem_4_a():
def check_valid_passphrase(passphrase):
passphrase_list = passphrase.split(' ')
# print(passphrase_list)
# print(list(set(passphrase_list)))
return len(set(passphrase_list)) == len(passphrase_list)
def find_valid_passphrases(passphrases):
passphrases = passphrases.split('\n')
return len([p for p in passphrases if check_valid_passphrase(p)])
print(check_valid_passphrase("aa bb cc dd ee"))
print(check_valid_passphrase("aa bb cc dd aa"))
print(check_valid_passphrase("aa bb cc dd aaa"))
with open("D:\\GitHub_ryanvilbrandt\\personal\\inputs\\advent_of_code_2017_problem_4_input.txt") as f:
problem_4_input = f.read()
print(find_valid_passphrases(problem_4_input))
def problem_4_b():
def check_valid_passphrase(passphrase):
passphrase_list = ["".join(sorted(p)) for p in passphrase.split(' ')]
# print(passphrase_list)
# print(list(set(passphrase_list)))
return len(set(passphrase_list)) == len(passphrase_list)
def find_valid_passphrases(passphrases):
passphrases = passphrases.split('\n')
return len([p for p in passphrases if check_valid_passphrase(p)])
print(check_valid_passphrase("abcde fghij"))
print(check_valid_passphrase("abcde xyz ecdab"))
print(check_valid_passphrase("a ab abc abd abf abj"))
print(check_valid_passphrase("iiii oiii ooii oooi oooo"))
print(check_valid_passphrase("oiii ioii iioi iiio"))
with open("D:\\GitHub_ryanvilbrandt\\personal\\inputs\\advent_of_code_2017_problem_4_input.txt") as f:
problem_4_input = f.read()
print(find_valid_passphrases(problem_4_input))
def problem_5_a():
def jump(jump_list):
index = 0
jump_count = 0
while 0 <= index < len(jump_list):
# print(jump_list)
last_index = index
index += jump_list[index]
jump_list[last_index] += 1
jump_count += 1
# print(jump_list)
# print()
return jump_count
a = [0, 3, 0, 1, -3]
print(a, jump(a[:]))
with open("D:\\GitHub_ryanvilbrandt\\personal\\inputs\\advent_of_code_2017_problem_5_input.txt") as f:
input_list = f.read().strip('\n').split('\n')
input_list = [int(x) for x in input_list]
print(jump(input_list))
def problem_5_b():
def jump(jump_list):
index = 0
jump_count = 0
while 0 <= index < len(jump_list):
# print(jump_list)
last_index = index
index += jump_list[index]
jump_list[last_index] += 1 if jump_list[last_index] < 3 else -1
jump_count += 1
# print(jump_list)
# print()
return jump_count
a = [0, 3, 0, 1, -3]
print(a, jump(a[:]))
with open("D:\\GitHub_ryanvilbrandt\\personal\\inputs\\advent_of_code_2017_problem_5_input.txt") as f:
input_list = f.read().strip('\n').split('\n')
input_list = [int(x) for x in input_list]
print(jump(input_list))
def problem_6_a():
def check_history(history, new_list):
if new_list in history:
return False
history.append(new_list[:])
return True
def reallocate(blocks):
history = []
loops = 0
while check_history(history, blocks):
max_index = blocks.index(max(blocks))
blocks_to_redistribute = blocks[max_index]
blocks[max_index] = 0
for i in range(blocks_to_redistribute):
blocks[(max_index + i + 1) % len(blocks)] += 1
# print(history, blocks)
loops += 1
return loops
print(reallocate([0, 2, 7, 0]))
print(reallocate([4, 1, 15, 12, 0, 9, 9, 5, 5, 8, 7, 3, 14, 5, 12, 3]))
def problem_6_b():
def check_history(history, new_list):
if history.count(new_list) == 2:
return False
history.append(new_list[:])
return True
def reallocate(blocks):
history = []
loops = 0
while check_history(history, blocks):
max_index = blocks.index(max(blocks))
blocks_to_redistribute = blocks[max_index]
blocks[max_index] = 0
for i in range(blocks_to_redistribute):
blocks[(max_index + i + 1) % len(blocks)] += 1
# print(history, blocks)
loops += 1
return loops
print(reallocate([0, 2, 7, 0]))
print(reallocate([4, 1, 15, 12, 0, 9, 9, 5, 5, 8, 7, 3, 14, 5, 12, 3]))