-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc08.jl
777 lines (750 loc) · 8.8 KB
/
aoc08.jl
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# ---
# jupyter:
# jupytext:
# formats: ipynb,jl:percent
# text_representation:
# extension: .jl
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.7.1
# kernelspec:
# display_name: Julia 1.5.2
# language: julia
# name: julia-1.5
# ---
# %%
ex1 = "nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6";
# %%
input ="acc +0
jmp +516
acc -16
nop +326
jmp +366
jmp +400
jmp +494
acc -5
jmp +475
acc +29
nop +24
jmp +186
jmp -5
acc +50
acc +49
acc +49
nop +507
jmp +236
acc -4
acc +50
acc +48
jmp +293
jmp +461
acc +48
acc +2
jmp +241
acc +38
jmp +544
jmp +1
jmp +382
acc -4
acc -10
acc +44
jmp +444
jmp +1
acc +20
acc -1
acc +25
jmp +332
acc +18
jmp +184
acc +1
acc +20
acc -7
acc -12
jmp +84
acc +30
jmp -19
nop +491
acc +37
acc +44
jmp +220
acc +3
acc +19
jmp +485
jmp +222
nop +264
acc +4
jmp +151
nop -36
acc +48
jmp +356
acc +28
acc +29
nop +397
jmp +168
nop +115
acc -13
acc -11
jmp +470
jmp +201
acc +48
acc -6
acc -18
acc -13
jmp +442
jmp +266
acc +10
acc +30
acc -7
acc -8
jmp +380
acc +1
nop +106
acc +9
acc -11
jmp -79
jmp +166
acc -9
jmp +300
acc +39
acc +1
acc +27
acc +26
jmp +367
jmp +291
acc +18
jmp +100
acc +25
acc +43
acc -8
acc -12
jmp +475
acc +15
jmp +492
acc -5
acc -12
acc +35
acc +25
jmp +360
nop +301
acc +32
acc -1
jmp +436
acc +21
jmp +109
acc +1
nop +230
jmp -113
acc +24
acc +12
acc +13
acc +27
jmp -52
jmp +476
acc +15
acc -18
acc -13
jmp +323
acc -10
acc +35
acc -6
nop +377
jmp +8
jmp +430
nop +50
acc +33
acc -1
jmp -72
acc -19
jmp -92
jmp +1
jmp -87
nop -120
nop -34
jmp +159
jmp +183
jmp +1
acc -4
acc +49
acc -4
jmp +73
acc -18
acc +34
acc -8
jmp +403
acc +32
acc +0
acc +0
jmp +390
acc +19
jmp +305
acc -15
jmp +176
acc +3
jmp +36
acc +29
acc +21
jmp +1
acc -17
jmp +144
acc +16
acc +4
acc +49
acc -7
jmp -65
acc +28
acc +2
acc +23
acc +46
jmp +278
jmp +424
acc +45
acc +7
jmp -49
jmp -50
acc -11
nop -121
jmp -140
jmp +1
acc -8
jmp +206
acc -7
jmp +1
acc +20
acc +30
jmp +375
nop +312
jmp +111
acc +23
jmp -15
acc +27
acc +46
acc +36
jmp +360
acc +1
acc +25
acc +29
jmp -137
jmp +177
acc +15
acc -13
acc +48
acc +29
jmp +76
nop -174
acc +27
jmp +214
acc +4
jmp +120
nop -165
acc -16
jmp +2
jmp +238
jmp +224
jmp -154
acc +12
jmp +162
acc +31
acc +26
acc -13
acc +18
jmp +173
acc +38
jmp +1
jmp +199
jmp -202
jmp +127
acc -12
jmp -42
acc -12
acc +36
acc -4
acc +24
jmp +343
acc -17
nop +277
acc +13
jmp +56
jmp +41
nop +92
acc +32
jmp +182
nop +111
acc +0
acc +29
jmp +219
nop -234
acc +0
acc +31
acc +49
jmp +216
jmp +247
acc +24
acc -10
jmp +218
acc +14
acc -19
acc +14
jmp +11
jmp +82
acc +5
acc +28
acc +35
jmp -93
acc +24
jmp -210
acc +39
jmp -107
jmp -150
acc -7
jmp -185
acc +25
acc -15
jmp +63
acc +14
acc +50
jmp -7
acc +13
jmp +298
nop +69
jmp -268
acc -5
jmp +66
jmp +48
nop -1
acc +38
acc +3
acc +46
jmp +259
nop +235
acc +4
jmp -186
jmp +94
acc -16
acc +6
jmp +287
acc -19
jmp -221
jmp -69
acc +8
nop -102
acc +31
jmp +204
jmp -81
acc +12
acc +21
acc -8
nop +151
jmp -155
acc +50
nop +145
acc -6
acc +0
jmp -296
acc +16
acc +30
acc +11
jmp +254
acc +0
acc -8
nop +162
acc -17
jmp -144
acc +6
acc +2
acc +32
acc -5
jmp +21
jmp -77
acc +38
jmp +117
acc +27
acc -11
nop -203
jmp +201
jmp -305
acc +40
nop -330
acc -7
jmp -279
jmp +60
nop -163
jmp -53
nop +115
acc +24
jmp +1
acc +45
jmp -92
acc +0
acc +13
jmp -302
acc +25
jmp +208
nop -293
jmp +5
jmp +1
acc -13
nop -272
jmp +62
acc -4
acc +32
acc +49
jmp -124
acc -19
nop -246
jmp -353
nop -349
acc +13
acc +21
jmp -165
acc +14
acc +37
nop +28
acc +7
jmp -308
acc +11
jmp +71
jmp -139
nop -19
acc -7
acc -3
jmp -302
acc -2
acc +1
nop -43
jmp -280
acc +10
acc +9
jmp -358
acc +39
acc +16
acc -11
jmp +1
jmp +142
acc -15
acc -17
acc +9
acc +11
jmp -172
jmp -90
nop +135
acc -1
jmp -379
acc +39
acc -13
jmp -57
acc -3
acc -7
jmp +1
jmp +80
acc +7
acc +11
acc +30
nop +69
jmp -140
acc +11
acc +7
acc +3
jmp -417
jmp -314
acc -6
jmp -6
jmp -224
acc +0
jmp -131
acc +7
acc +33
jmp +20
acc +32
acc -14
nop -375
jmp -394
acc +8
acc +47
acc +29
acc -6
jmp -381
acc -8
jmp -353
jmp -108
acc -16
acc +21
jmp -329
acc +13
acc +0
acc +0
jmp -402
acc -8
jmp -307
jmp -308
jmp -358
acc +38
acc -19
acc +33
jmp -276
jmp -114
acc -6
jmp +55
acc +16
jmp -244
nop +22
acc -7
acc +19
jmp -154
jmp +18
jmp -50
acc +31
acc +50
acc +2
jmp -260
acc -16
jmp -135
acc +50
acc -9
acc +50
acc +27
jmp -323
acc +26
nop -249
acc +46
acc +3
jmp -474
acc +50
acc -7
jmp -78
jmp +1
acc -6
acc +6
jmp +94
acc +47
acc -3
acc +13
jmp -485
nop -499
acc +35
acc +35
acc -1
jmp -259
nop -486
acc +48
acc -4
jmp +23
acc +33
acc +37
acc +10
jmp +61
acc -1
jmp +1
jmp +1
acc -8
jmp +61
jmp -237
jmp -164
acc +39
acc +38
nop -302
jmp -468
acc -15
jmp -6
acc -9
acc +1
acc +41
acc +48
jmp -285
acc +1
acc +24
jmp -111
jmp -90
acc +46
acc +21
acc +43
jmp -103
acc -9
nop -5
jmp -274
acc -13
jmp -262
jmp -39
acc +43
nop -463
acc +15
acc +23
jmp -176
nop -305
acc +2
acc +28
jmp -198
acc +15
acc -18
jmp -473
acc -13
acc +9
jmp -46
acc +1
acc +43
acc +6
jmp +10
acc +24
acc -3
jmp -424
acc -1
acc +47
jmp +1
acc +20
jmp -17
jmp -59
nop -19
acc +42
acc +47
jmp -9
jmp -576
nop -148
acc +0
jmp +12
acc -15
jmp -209
acc +13
jmp -442
nop -548
nop -62
acc +22
jmp -92
jmp -516
acc +8
jmp -301
acc +1
jmp -146
acc -15
jmp -366
jmp -440
acc +17
acc +25
acc +28
jmp -240
acc -2
acc +25
nop -68
acc +14
jmp +1";
# %%
instruction_set = ex1
instruction_list = split(instruction_set, "\n")
acc = 0
index = 1
still = true
while still
ins_split = split(instruction_list[index], " ")
instruction_list[index] = "Done Done"
action, argument = ins_split[1], ins_split[2]
print(index, '\t', action, '\t', argument, '\n')
if action == "acc"
print("acc = acc"*argument*'\n')
eval(Meta.parse("acc = acc"*argument))
# argument will be like +1 or -2
index +=1
elseif action == "jmp"
eval(Meta.parse("index = index"*argument))
elseif action == "Done"
print("Acc value is ", acc, '\n')
return
elseif action == "nop"
index +=1
end
end
# %%
instruction_set = input
instruction_list = split(instruction_set, "\n")
acc = 0
index = 1
still = true
while still
ins_split = split(instruction_list[index], " ")
instruction_list[index] = "Done Done"
action, argument = ins_split[1], ins_split[2]
#print(index, '\t', action, '\t', argument, '\n')
if action == "acc"
eval(Meta.parse("acc = acc"*argument))
# argument will be like +1 or -2
index +=1
elseif action == "jmp"
eval(Meta.parse("index = index"*argument))
elseif action == "Done"
print("Acc value is ", acc, '\n')
return
elseif action == "nop"
index +=1
end
end
# %%
eval(Meta.parse("a = 1"*"+1"))
# %% [markdown]
# ## Part 2
# %%
instruction_set = input
instruction_list = split(instruction_set, "\n")
indices_to_change = [i for i in 1:length(instruction_list) if (contains(instruction_list[i], r"jmp|nop"))]
# %%
indices_reached = []
indices_changed = []
acc = 0
index = 1
changed_this_round = false
verbose = true
last_reached = true
while last_reached
if index == length(instruction_list) + 1
push!(indices_reached, index)
last_reached = false
print("Last entry reached. acc value is ", acc, '\n')
break
end
if index in indices_reached
if true print("\n\nRestart\n\n") end
changed_this_round = false
indices_reached = []
index = 1
acc = 0
end
ins_split = split(instruction_list[index], " ")
action, argument = ins_split[1], ins_split[2]
if action == "acc"
eval(Meta.parse("acc = acc"*argument))
# argument will be like +1 or -2
push!(indices_reached, index)
if true print(index, '\t', action, '\t', argument, '\n') end
index +=1
elseif action == "jmp"
push!(indices_reached, index)
if index in indices_to_change && !(index in indices_changed) && !changed_this_round
if true print(index,'+','\t', "nop", '\t', argument, '\n') end
push!(indices_changed, index)
index +=1 #equivalent to nop
changed_this_round = true
else
if true print(index, '\t', action, '\t', argument, '\n') end
eval(Meta.parse("index = index"*argument))
end
elseif action == "nop"
# update indices_reached
push!(indices_reached, index)
# move
if index in indices_to_change && !(index in indices_changed) && argument != "+0" && !changed_this_round
if true print(index,'+','\t', "jmp", '\t', argument, '\n') end
push!(indices_changed, index)
eval(Meta.parse("index = index"*argument)) #equivalent to jmp
changed_this_round = true
else
if true print(index,'\t', "jmp", '\t', argument, '\n') end
index +=1
end
end
end
# %%
# %%