-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.rb
715 lines (700 loc) · 6.05 KB
/
08.rb
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
require 'set'
program = DATA.read.lines.map do |ins|
op, arg = ins.scan(/(\w+) (-\d+|\+\d+)/).flatten
[op, arg.to_i]
end.freeze
def run program
accumulator = 0
ip = 0
seen = Set[]
while ip < program.length do
op, arg = program[ip]
if seen.include? ip
raise StandardError, accumulator
break
end
seen.add(ip)
case op
when "acc"
accumulator += arg
ip += 1
when "nop"
ip += 1
when "jmp"
ip += arg
end
end
accumulator
end
begin
run program
rescue StandardError => acc
puts acc
end
swap = { "nop" => "jmp", "jmp" => "nop" }
program.each_with_index do |instr, index|
op = instr.first
if swap.has_key? op
program[index][0] = swap[op]
begin
accumulator = run program
puts accumulator
rescue StandardError => acc
end
program[index][0] = op
end
end
__END__
acc +9
acc -2
acc -12
acc +33
jmp +301
nop +508
jmp +216
acc +27
acc +35
acc +43
acc +31
jmp +309
acc +18
acc -19
acc +7
jmp +44
acc -13
acc -17
acc +31
jmp +311
nop +612
jmp +143
acc +22
nop +85
jmp +458
acc -3
jmp +13
acc -19
acc +27
acc +12
jmp +483
acc +40
acc +6
jmp +128
jmp +10
acc +0
acc -3
acc -2
jmp -11
acc +43
acc -12
jmp +158
acc +0
jmp +240
jmp +1
acc +5
acc +15
jmp +187
nop +563
jmp +51
acc -16
jmp +158
jmp +322
acc +47
nop -1
jmp +299
acc +26
acc +25
jmp +232
jmp -9
acc +15
jmp +54
jmp +558
acc +7
acc -7
jmp +399
nop +447
jmp +71
acc +26
acc +46
jmp +145
acc +38
acc +30
acc +21
jmp +263
acc +10
jmp +168
acc +22
nop +561
jmp -26
jmp +1
acc -7
jmp -5
acc +28
acc -6
jmp +370
jmp +94
acc +50
acc +42
acc -9
acc +30
jmp +70
acc +29
jmp +166
acc -5
acc -18
nop +84
acc +2
jmp +366
jmp -40
acc -4
acc -15
acc -1
jmp +169
jmp +1
acc -4
acc +0
jmp -45
nop -21
nop +241
acc -18
acc +19
jmp +26
nop -51
jmp +260
acc +17
jmp +428
acc +6
jmp +405
acc +22
acc +10
nop +471
jmp +352
acc -6
acc +48
acc +7
acc +3
jmp +57
acc -10
acc +16
acc +16
acc +43
jmp +432
acc -5
acc +0
nop +339
acc +49
jmp +17
acc +33
nop +166
acc -5
jmp +392
nop +246
acc -7
acc +21
acc +30
jmp +398
acc +36
acc +24
acc -15
acc -9
jmp +114
acc +19
jmp +11
acc +43
nop +182
jmp -129
nop -29
acc -6
acc +2
jmp +398
jmp +78
acc +36
jmp +393
acc +15
nop -11
acc -7
acc -9
jmp +76
acc +0
acc +27
jmp +25
acc +27
nop -54
jmp +458
acc +3
acc +29
acc -4
acc +43
jmp +413
acc +33
acc +13
jmp +382
jmp -83
acc +42
acc +24
jmp +64
acc +23
acc -13
nop +110
acc -5
jmp +114
jmp +113
nop +112
acc +26
jmp -133
jmp -12
jmp +1
jmp +330
acc +25
acc -1
acc +30
acc +42
jmp -187
jmp +1
acc +20
acc +35
acc +36
jmp -125
jmp +165
acc +28
acc -17
acc -12
jmp +1
jmp -120
nop +1
acc +2
acc +26
jmp +398
acc +20
acc -1
jmp -127
acc +36
acc +14
jmp +1
jmp +331
acc +50
acc +1
acc -10
nop +159
jmp -83
jmp +374
acc +17
jmp +372
acc +44
nop -39
jmp +228
acc +17
jmp +74
acc +16
acc +33
acc -2
jmp +152
jmp +29
acc +8
acc +27
nop +59
jmp -32
acc +28
jmp -227
nop -35
jmp -168
acc +13
nop +390
jmp -204
acc +16
acc +44
jmp -230
jmp +25
acc +30
jmp +383
acc -11
acc +38
acc +11
jmp +341
acc +35
acc +46
acc -1
jmp +94
acc -4
acc +12
jmp +111
jmp +133
nop +283
acc +13
acc +37
jmp +74
nop -218
jmp -178
acc +46
acc +25
acc -5
jmp -174
acc +28
acc +39
acc +36
acc +22
jmp -172
acc +19
jmp -250
nop +62
acc +44
nop +347
acc +40
jmp +345
acc -3
acc -13
acc -11
jmp +56
jmp -180
acc +17
acc -4
acc +46
nop -165
jmp +321
acc -4
jmp +1
acc +9
acc -12
jmp -155
acc +5
jmp -96
acc +0
acc -2
acc +38
jmp +67
acc -4
nop -283
acc +28
jmp +324
acc -9
acc +43
acc -1
acc +9
jmp -290
acc +3
acc +22
nop +84
acc -17
jmp -210
acc +7
jmp -260
nop -232
nop +87
acc +43
acc +36
jmp +96
jmp +238
acc +13
acc -14
acc +32
acc +11
jmp -146
acc +13
acc +37
acc -10
jmp +187
acc +49
acc +15
jmp -234
jmp -328
jmp -136
jmp +143
jmp +1
acc +27
acc +22
jmp +1
jmp -5
acc +30
nop -7
acc -6
jmp -71
acc -17
acc +15
jmp -52
jmp -126
acc -4
jmp +151
jmp +52
nop -86
acc +25
jmp +187
nop -22
jmp -219
acc +33
nop -120
acc +0
jmp +215
acc +46
acc +38
jmp +1
jmp -262
jmp +157
acc -15
acc +48
acc +39
acc +10
jmp -137
acc +47
acc +50
jmp -324
nop +214
acc +39
jmp -178
acc +49
acc -10
jmp -268
jmp +50
acc -14
nop -100
jmp +20
acc +45
acc -12
acc -4
jmp -208
acc -19
jmp -340
acc +36
nop -358
acc +5
jmp -348
acc +47
nop -18
acc -12
jmp -131
acc +19
acc +10
acc +19
acc +31
jmp -164
nop +162
nop -260
jmp +146
acc +32
acc -1
nop -14
jmp -192
acc +3
acc +31
nop -185
jmp -208
jmp -69
acc +43
acc +43
jmp -68
acc -16
acc +5
acc -9
jmp +126
acc +33
acc +2
acc +34
acc -9
jmp -16
acc +34
acc -19
jmp -266
nop +135
nop -389
acc +33
jmp -195
acc +48
jmp +1
acc -12
jmp +143
nop -317
acc -14
nop -127
acc +32
jmp -372
acc +24
nop -41
nop -42
jmp -344
acc +23
nop +117
nop +92
acc +42
jmp +143
acc +48
acc -6
nop -272
acc -13
jmp -379
acc -2
acc +44
acc +9
jmp -369
acc +6
acc +25
acc +34
jmp -301
nop -227
acc +43
jmp -141
acc +12
acc +41
acc +17
acc -11
jmp +29
jmp -121
acc +6
acc +7
acc +7
jmp +131
nop +144
nop -142
acc -13
acc -18
jmp +149
acc +14
acc +49
acc +25
acc -17
jmp -9
acc +26
acc -4
jmp -230
acc -18
acc +36
acc +27
nop -142
jmp +21
acc +34
nop +54
jmp -476
acc +10
jmp -174
nop -354
acc +1
jmp -324
acc +40
jmp +94
acc -12
jmp -136
nop -454
acc -14
jmp +116
acc +12
acc -1
nop -453
jmp -241
jmp -479
acc -19
jmp -87
acc +27
acc +48
acc +0
jmp -476
acc +16
acc +46
jmp -534
acc +0
jmp -344
acc +0
acc +28
jmp +10
jmp -248
nop -186
jmp +1
acc +26
jmp -153
acc +14
acc -8
nop -416
jmp -91
jmp -409
jmp -326
acc +2
acc +8
acc -18
acc +33
jmp -468
jmp -175
acc -7
acc +45
jmp -18
jmp -375
acc -8
jmp +28
acc -16
nop -38
acc +37
acc +48
jmp -343
acc +10
acc +26
acc -9
acc -16
jmp -348
acc +37
jmp -453
acc -2
acc +27
acc +17
acc +28
jmp -406
acc +25
acc +24
acc +44
acc +44
jmp -532
acc +10
jmp -531
acc +39
acc +40
jmp -284
acc +19
acc +3
nop -533
acc -3
jmp -162
nop -438
acc -5
jmp -114
acc +45
acc +1
acc +28
acc +9
jmp -550
jmp -222
jmp -106
acc -7
nop -263
nop -375
jmp -381
acc -4
nop -223
jmp -171
jmp -465
acc -2
nop -562
jmp -190
acc +40
jmp -4
acc +30
acc +21
jmp -435
acc +1
acc +10
jmp +1
jmp -157
acc -7
acc +18
acc -3
acc +24
jmp -113
acc +21
jmp -339
acc +34
jmp -563
acc +27
jmp -589
jmp -61
acc +35
acc +50
acc +8
jmp -553
acc +48
acc -15
acc +29
acc +24
jmp +1