-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadv21.rb
executable file
·2016 lines (1751 loc) · 64.4 KB
/
adv21.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
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
require 'set'
# ##############################################################################
# load "adv21.rb" ; d21011()
# ##############################################################################
# ###########################################################################
#
# 2021 DAY 25
#
# ###########################################################################
# v...>>.vv>
# .vv>>.vv..
# >>.>v>...v
# >>v>>.>.v.
# v>v.vv.v..
# >.>>..v...
# .vv..>.>v.
# v.v..>>v.v
# ....v..v.>
# 412
def d21251()
lines = input(2125).split("\n")
ymax, xmax = lines.length, lines.first.length
east, south =
lines.each_with_index.map { |l, y|
l.chars.each_with_index.map { |c, x| [c, x, y] if (c != ".") }
}.flatten(1).compact.partition { |k, _, _| k == ">"
}.then { |ls| ls.map { |l| l.map { |x| x.drop(1) }.to_set } }
def step(east, south, xmax, ymax)
e_bk, e_ok = east.partition { |x, y|
nx, ny = (x + 1) % xmax, y
east.include?([nx, ny]) or south.include?([nx, ny]) }
e_ok.map! { |x, y| [(x + 1) % xmax, y] }
east.replace(e_ok + e_bk)
s_bk, s_ok = south.partition { |x, y|
nx, ny = x, (y + 1) % ymax
east.include?([nx, ny]) or south.include?([nx, ny]) }
s_ok.map! { |x, y| [x, (y + 1) % ymax] }
south.replace(s_ok + s_bk)
end
(1..).find {
oeast, osouth = east.to_a.to_set, south.to_a.to_set
step( east, south, xmax, ymax )
( oeast == east and osouth == south )
}
end
# ###########################################################################
#
# 2021 DAY 24
#
# ###########################################################################
def d21240(arg)
prog = input(2124).split("\n").map(&:split)
def runprog(prog, input)
vars = { "w" => 0, "x" => 0, "y" => 0, "z" => 0 }
opv = lambda { |x| (vars[x] || x.to_i) }
for code, num in prog.each_with_index
debug(vars.map { |n, v| "%s=%10d " % [n, v] }.join)
debug("%3d - %s" % [num, code])
case code
in ["inp", a] then vars[a] = input.shift || break
in ["add", a, b] then vars[a] = opv.(a) + opv.(b)
in ["mul", a, b] then vars[a] = opv.(a) * opv.(b)
in ["div", a, b] then vars[a] = opv.(a) / opv.(b)
in ["mod", a, b] then vars[a] = opv.(a) % opv.(b)
in ["eql", a, b] then vars[a] = (opv.(a) == opv.(b)) ? 1 : 0
end
end
debug(vars.map { |n, v| "%s=%10d " % [n, v] }.join)
vars
end
input = arg.chars.map(&:to_i)
result = runprog(prog, input)
result["z"]
end
# W1 W2 W3 W4 W5 W6 W7
# inp w inp w inp w inp w inp w inp w inp w
# mul x 0 mul x 0 mul x 0 mul x 0 mul x 0 mul x 0 mul x 0
# add x z add x z add x z add x z add x z add x z add x z
# mod x 26 mod x 26 mod x 26 mod x 26 mod x 26 mod x 26 mod x 26
# div z 1 # div z 1 div z 1 div z 26 div z 1 div z 1 div z 26
# add x 12# add x 11 add x 14 add x -6 add x 15 add x 12 add x -9
# eql x w eql x w eql x w eql x w eql x w eql x w eql x w
# eql x 0 eql x 0 eql x 0 eql x 0 eql x 0 eql x 0 eql x 0
# mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0
# add y 25 add y 25 add y 25 add y 25 add y 25 add y 25 add y 25
# mul y x mul y x mul y x mul y x mul y x mul y x mul y x
# add y 1 add y 1 add y 1 add y 1 add y 1 add y 1 add y 1
# mul z y mul z y mul z y mul z y mul z y mul z y mul z y
# mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0
# add y w add y w add y w add y w add y w add y w add y w
# add y 4 # add y 10 add y 12 add y 14 add y 6 add y 16 add y 1
# mul y x mul y x mul y x mul y x mul y x mul y x mul y x
# add z y add z y add z y add z y add z y add z y add z y
# W8 W9 W10 W11 W12 W13 W14
# inp w inp w inp w inp w inp w inp w inp w
# mul x 0 mul x 0 mul x 0 mul x 0 mul x 0 mul x 0 mul x 0
# add x z add x z add x z add x z add x z add x z add x z
# mod x 26 mod x 26 mod x 26 mod x 26 mod x 26 mod x 26 mod x 2
# div z 1 div z 1 div z 26 div z 26 div z 26 div z 26 div z 2
# add x 14 add x 14 add x -5 add x -9 add x -5 add x -2 add x -
# eql x w eql x w eql x w eql x w eql x w eql x w eql x w
# eql x 0 eql x 0 eql x 0 eql x 0 eql x 0 eql x 0 eql x 0
# mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0
# add y 25 add y 25 add y 25 add y 25 add y 25 add y 25 add y 2
# mul y x mul y x mul y x mul y x mul y x mul y x mul y x
# add y 1 add y 1 add y 1 add y 1 add y 1 add y 1 add y 1
# mul z y mul z y mul z y mul z y mul z y mul z y mul z y
# mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0 mul y 0
# add y w add y w add y w add y w add y w add y w add y w
# add y 7 add y 8 add y 11 add y 8 add y 3 add y 1 add y 8
# mul y x mul y x mul y x mul y x mul y x mul y x mul y x
# add z y add z y add z y add z y add z y add z y add z y
# 1 - inp w ### W = INPUT
# 2 - mul x 0 # X = 0
# 3 - add x z # X = X + Z
# 4 - mod x 26 # X = Z % 26
# 5 - div z 1 ### Z = Z / 1
# 6 - add x 12 ### X = X + 12
# 7 - eql x w # X = ( X == W ) ? 1 : 0
# 8 - eql x 0 # X = ( X == 0 ) ? 1 : 0
# ### X = ( ( X + 12 ) != W )
# 9 - mul y 0 # Y = 0
# 10 - add y 25 # Y = Y + 25
# 11 - mul y x # Y = Y * X
# 12 - add y 1 # Y = Y + 1
# 13 - mul z y # Z = Z * Y
# # Z = Z * ( 25 * X ) + 1
# 14 - mul y 0 # Y = 0
# 15 - add y w # Y = Y + W
# 16 - add y 4 ### Y = Y + 4
# ### Y = W + 4
# 17 - mul y x # Y = Y * X
# 18 - add z y # Z = Z + Y
# W = INPUT
#
# X = ( ( (Z_ % 26) * 12 ) != W )
# ^
# Z = ( ( Z_ / 1 ) * ( ( 25 * X ) + 1 ) ) + ( ( W + 4 ) * X )
# ^ ^
# V1 = 12 11 14 -6 15 12 -9 14 14 -5 -9 -5 -2 -7
# V2 = 1 1 1 26 1 1 26 1 1 26 26 26 26 26
# V3 = 4 10 12 14 6 16 1 7 8 11 8 3 1 8
# # X1 = ( ( 0 + 12 ) != W1 )
# # X2 = ( ( (Z1 % 26) + 11 ) != W2 )
# # X3 = ( ( (Z2 % 26) + 14 ) != W3 )
# # X4 = ( ( (Z3 % 26) + -6 ) != W4 )
# # X5 = ( ( (Z4 % 26) + 15 ) != W5 )
# # X6 = ( ( (Z5 % 26) + 12 ) != W6 )
# # X7 = ( ( (Z6 % 26) + -9 ) != W7 )
# # X8 = ( ( (Z7 % 26) + 14 ) != W8 )
# # X9 = ( ( (Z8 % 26) + 14 ) != W9 )
# # X10 = ( ( (Z9 % 26) + -5 ) != W10 )
# # X11 = ( ( (Z10 % 26) + -9 ) != W11 )
# # X12 = ( ( (Z11 % 26) + -5 ) != W12 )
# # X13 = ( ( (Z12 % 26) + -2 ) != W13 )
# # X14 = ( ( (Z13 % 26) + -7 ) != W14 )
# # Z1 = ( ( Z0 / 1 ) * ( ( 25 * X1 ) + 1 ) ) + ( ( W1 + 4 ) * X1 )
# # Z2 = ( ( Z1 / 1 ) * ( ( 25 * X2 ) + 1 ) ) + ( ( W2 + 10) * X2 )
# # Z3 = ( ( Z2 / 1 ) * ( ( 25 * X3 ) + 1 ) ) + ( ( W3 + 12) * X3 )
# # Z4 = ( ( Z3 / 26) * ( ( 25 * X4 ) + 1 ) ) + ( ( W4 + 14) * X4 )
# # Z5 = ( ( Z4 / 1 ) * ( ( 25 * X5 ) + 1 ) ) + ( ( W5 + 6 ) * X5 )
# # Z6 = ( ( Z5 / 1 ) * ( ( 25 * X6 ) + 1 ) ) + ( ( W6 + 16) * X6 )
# # Z7 = ( ( Z6 / 26) * ( ( 25 * X7 ) + 1 ) ) + ( ( W7 + 1 ) * X7 )
# # Z8 = ( ( Z7 / 1 ) * ( ( 25 * X8 ) + 1 ) ) + ( ( W8 + 7 ) * X8 )
# # Z9 = ( ( Z8 / 1 ) * ( ( 25 * X9 ) + 1 ) ) + ( ( W9 + 8 ) * X9 )
# # Z10 = ( ( Z9 / 26) * ( ( 25 * X10 ) + 1 ) ) + ( ( W10 + 11) * X10 )
# # Z11 = ( ( Z10 / 26) * ( ( 25 * X11 ) + 1 ) ) + ( ( W11 + 8 ) * X11 )
# # Z12 = ( ( Z11 / 26) * ( ( 25 * X12 ) + 1 ) ) + ( ( W12 + 3 ) * X12 )
# # Z13 = ( ( Z12 / 26) * ( ( 25 * X13 ) + 1 ) ) + ( ( W13 + 1 ) * X13 )
# # Z14 = ( ( Z13 / 26) * ( ( 25 * X14 ) + 1 ) ) + ( ( W14 + 8 ) * X14 )
# X1 = 1
# X2 = 1
# X3 = 1
# X4 = ( (Z3 % 26) + -6 ) != W4
# X5 = 1
# X6 = 1
# X7 = ( (Z6 % 26) + -9 ) != W7
# X8 = 1
# X9 = 1
# X10 = ( (Z9 % 26) + -5 ) != W10
# X11 = ( (Z10 % 26) + -9 ) != W11
# X12 = ( (Z11 % 26) + -5 ) != W12
# X13 = ( (Z12 % 26) + -2 ) != W13
# X14 = ( (Z13 % 26) + -7 ) != W14
# Z1 = ( W1 + 4 )
# Z2 = ( Z1 * 26 ) + ( W2 + 10)
# Z3 = ( Z2 * 26 ) + ( W3 + 12)
# Z4 = (X4 == 1) ? ( ( Z3 / 26) * 26 ) + ( W4 + 14) : (Z3 / 26)
# Z5 = ( Z4 * 26 ) + ( W5 + 6 )
# Z6 = ( Z5 * 26 ) + ( W6 + 16)
# Z7 = (X7 == 1) ? ( ( Z6 / 26) * 26 ) + ( W7 + 1 ) : (Z6 / 26)
# Z8 = ( Z7 * 26 ) + ( W8 + 7 )
# Z9 = ( Z8 * 26 ) + ( W9 + 8 )
# Z10 = (X10 == 1) ? ( ( Z9 / 26) * 26 ) + ( W10 + 11) : (Z9 / 26)
# Z11 = (X11 == 1) ? ( ( Z10 / 26) * 26 ) + ( W11 + 8 ) : (Z10 / 26)
# Z12 = (X12 == 1) ? ( ( Z11 / 26) * 26 ) + ( W12 + 3 ) : (Z11 / 26)
# Z13 = (X13 == 1) ? ( ( Z12 / 26) * 26 ) + ( W13 + 1 ) : (Z12 / 26)
# Z14 = (X14 == 1) ? ( ( Z13 / 26) * 26 ) + ( W14 + 8 ) : (Z13 / 26)
def d2124x(arg)
w = arg.chars.map(&:to_i).unshift(nil)
z1 = (w[1] + 4)
z2 = ( z1 * 26) + (w[2] + 10)
z3 = ( z2 * 26) + (w[3] + 12)
x4 = (((z3 % 26) - 6) != w[4] ) ? 1 : 0
z4 = (x4 == 1) ? ((z3 / 26) * 26) + (w[4] + 14) : (z3 / 26)
z5 = ( z4 * 26) + (w[5] + 6)
z6 = ( z5 * 26) + (w[6] + 16)
x7 = (((z6 % 26) - 9) != w[7] ) ? 1 : 0
z7 = (x7 == 1) ? ((z6 / 26) * 26) + (w[7] + 1) : (z6 / 26)
z8 = ( z7 * 26) + (w[8] + 7)
z9 = ( z8 * 26) + (w[9] + 8)
x10 = (((z9 % 26) - 5) != w[10]) ? 1 : 0
z10 = (x10 == 1) ? ((z9 / 26) * 26) + (w[10] +11) : (z9 / 26)
x11 = (((z10 % 26) - 9) != w[11]) ? 1 : 0
z11 = (x11 == 1) ? ((z10 / 26) * 26) + (w[11] + 8) : (z10 / 26)
x12 = (((z11 % 26) - 5) != w[12]) ? 1 : 0
z12 = (x12 == 1) ? ((z11 / 26) * 26) + (w[12] + 3) : (z11 / 26)
x13 = (((z12 % 26) - 2) != w[13]) ? 1 : 0
z13 = (x13 == 1) ? ((z12 / 26) * 26) + (w[13] + 1) : (z12 / 26)
x14 = (((z13 % 26) - 7) != w[14]) ? 1 : 0
z14 = (x14 == 1) ? ((z13 / 26) * 26) + (w[14] + 8) : (z13 / 26)
z14
end
# 91398299697996
def d21241()
for w1 in (9..1).step(-1) do
z1 = (w1 + 4)
for w2 in (9..1).step(-1) do
z2 = ( z1 * 26) + (w2 + 10)
for w3 in (9..1).step(-1) do
z3 = ( z2 * 26) + (w3 + 12)
for w4 in (9..1).step(-1) do
x4 = (((z3 % 26) - 6) != w4 ) ? 1 : 0
next if x4 != 0
z4 = (x4 == 1) ? ((z3 / 26) * 26) + (w4 + 14) : (z3 / 26)
for w5 in (9..1).step(-1) do
z5 = ( z4 * 26) + (w5 + 6)
for w6 in (9..1).step(-1) do
z6 = ( z5 * 26) + (w6 + 16)
for w7 in (9..1).step(-1) do
x7 = (((z6 % 26) - 9) != w7 ) ? 1 : 0
next if x7 != 0
z7 = (x7 == 1) ? ((z6 / 26) * 26) + (w7 + 1) : (z6 / 26)
for w8 in (9..1).step(-1) do
z8 = ( z7 * 26) + (w8 + 7)
for w9 in (9..1).step(-1) do
z9 = ( z8 * 26) + (w9 + 8)
for w10 in (9..1).step(-1) do
x10 = (((z9 % 26) - 5) != w10) ? 1 : 0
next if x10 != 0
z10 = (x10 == 1) ? ((z9 / 26) * 26) + (w10 +11) : (z9 / 26)
for w11 in (9..1).step(-1) do
x11 = (((z10 % 26) - 9) != w11) ? 1 : 0
next if x11 != 0
z11 = (x11 == 1) ? ((z10 / 26) * 26) + (w11 + 8) : (z10 / 26)
for w12 in (9..1).step(-1) do
x12 = (((z11 % 26) - 5) != w12) ? 1 : 0
next if x12 != 0
z12 = (x12 == 1) ? ((z11 / 26) * 26) + (w12 + 3) : (z11 / 26)
for w13 in (9..1).step(-1) do
x13 = (((z12 % 26) - 2) != w13) ? 1 : 0
next if x13 != 0
z13 = (x13 == 1) ? ((z12 / 26) * 26) + (w13 + 1) : (z12 / 26)
for w14 in (9..1).step(-1) do
x14 = (((z13 % 26) - 7) != w14) ? 1 : 0
next if x14 != 0
z14 = (x14 == 1) ? ((z13 / 26) * 26) + (w14 + 8) : (z13 / 26)
return [w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14
] if z14 == 0
end end end end end end end end end end end end end end
# [9, 1, 3, 9, 8, 2, 9, 9, 6, 9, 7, 9, 9, 6]
end
# 41171183141291
def d21242()
for w1 in (1..9) do
z1 = (w1 + 4)
for w2 in (1..9) do
z2 = ( z1 * 26) + (w2 + 10)
for w3 in (1..9) do
z3 = ( z2 * 26) + (w3 + 12)
for w4 in (1..9) do
x4 = (((z3 % 26) - 6) != w4 ) ? 1 : 0
next if x4 != 0
z4 = (x4 == 1) ? ((z3 / 26) * 26) + (w4 + 14) : (z3 / 26)
for w5 in (1..9) do
z5 = ( z4 * 26) + (w5 + 6)
for w6 in (1..9) do
z6 = ( z5 * 26) + (w6 + 16)
for w7 in (1..9) do
x7 = (((z6 % 26) - 9) != w7 ) ? 1 : 0
next if x7 != 0
z7 = (x7 == 1) ? ((z6 / 26) * 26) + (w7 + 1) : (z6 / 26)
for w8 in (1..9) do
z8 = ( z7 * 26) + (w8 + 7)
for w9 in (1..9) do
z9 = ( z8 * 26) + (w9 + 8)
for w10 in (1..9) do
x10 = (((z9 % 26) - 5) != w10) ? 1 : 0
next if x10 != 0
z10 = (x10 == 1) ? ((z9 / 26) * 26) + (w10 +11) : (z9 / 26)
for w11 in (1..9) do
x11 = (((z10 % 26) - 9) != w11) ? 1 : 0
next if x11 != 0
z11 = (x11 == 1) ? ((z10 / 26) * 26) + (w11 + 8) : (z10 / 26)
for w12 in (1..9) do
x12 = (((z11 % 26) - 5) != w12) ? 1 : 0
next if x12 != 0
z12 = (x12 == 1) ? ((z11 / 26) * 26) + (w12 + 3) : (z11 / 26)
for w13 in (1..9) do
x13 = (((z12 % 26) - 2) != w13) ? 1 : 0
next if x13 != 0
z13 = (x13 == 1) ? ((z12 / 26) * 26) + (w13 + 1) : (z12 / 26)
for w14 in (1..9) do
x14 = (((z13 % 26) - 7) != w14) ? 1 : 0
next if x14 != 0
z14 = (x14 == 1) ? ((z13 / 26) * 26) + (w14 + 8) : (z13 / 26)
return [w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14
] if z14 == 0
end end end end end end end end end end end end end end
# [4, 1, 1, 7, 1, 1, 8, 3, 1, 4, 1, 2, 9, 1]
end
# XXXXXXXXXXXXXXXXXXXXXXX
#
# ALGO MEANING FOUND AFTERWARDS ON REDDIT
# (looks very similar to synacor challenge)
# PUSH[W1 + 4 ]
# PUSH[W2 + 10]
# PUSH[W3 + 12]
# OR POP[XXXX] IF W4 == XXXX-6 # X4 ==0
# PUSH[W5 + 6 ]
# PUSH[W6 + 16]
# OR POP[XXXX] IF W7 == XXXX-9 # X7 ==0
# PUSH[W8 + 7 ]
# PUSH[W9 + 8 ]
# OR POP[XXXX] IF W10== XXXX-5 # X10==0
# OR POP[XXXX] IF W11== XXXX-9 # X11==0
# OR POP[XXXX] IF W12== XXXX-5 # X12==0
# OR POP[XXXX] IF W13== XXXX-2 # X13==0
# OR POP[XXXX] IF W14== XXXX-7 # X14==0
# > PUSH W1 + 4
# > PUSH W2 + 10
# > PUSH W3 + 12
# < POP IF W4 == W3 + 12 -6
# > PUSH W5 + 6
# > PUSH W6 + 16
# < POP IF W7 == W6 + 16 -9
# > PUSH W8 + 7
# > PUSH W9 + 8
# < POP IF W10== W9 + 8 -5
# < POP IF W11== W8 + 7 -9
# < POP IF W12== W5 + 6 -5
# < POP IF W13== W2 + 10 -2
# < POP IF W14== W1 + 4 -7
# < W4 == W3 + 6
# < W7 == W6 + 7
# < W10 == W9 + 3
# < W11 == W8 - 2
# < W12 == W5 + 1
# < W13 == W2 + 8
# < W14 == W1 - 3
# W1 W2 W3 W4 W5 W6 W7 W8 W9 W10 W11 W12 W13 W14
# W3+6 W6+7 W9+3 W8-2 W5+1 W2+8 W1-3
# 9 1 3 9 8 2 9 9 6 9 7 9 9 6
# >>> PART1 91398299697996
# W1 W2 W3 W4 W5 W6 W7 W8 W9 W10 W11 W12 W13 W14
# W3+6 W6+7 W9+3 W8-2 W5+1 W2+8 W1-3
# 4 1 1 7 1 1 8 3 1 4 1 2 9 1
# >>> PART2 41171183141291
# ###########################################################################
#
# 2021 DAY 23
#
# ###########################################################################
# input example
# ############# #############
# #...........# #...........#
# ###B#A#B#C### ###B#C#B#D###
# #D#A#D#C# #A#D#C#A#
# ######### #########
module D2123
HALLWAY = [[1, 1], [2, 1], [4, 1], [6, 1], [8, 1], [10, 1], [11, 1]]
def finalpos(pods, ((x, y), t))
return false if (x != {"A" => 3, "B" => 5, "C" => 7, "D" => 9}[t])
return ((y + 1)..@@ymax).all? { |yr| pods[[x, yr]] == t }
end
def pathcost(pods, t, (x1, y1), (x2, y2))
( (1...y1) .step( 1) .map { |y| [x1, y] } +
(x2...x1).step(x2 <= x1 ? 1 : -1).map { |x| [x , 1] } +
(y2...1) .step(-1) .map { |y| [x2, y] } )
.each { |(x, y)| break nil if pods.key?([x, y]) }
.then { |p|
( { "A" => 1, "B" => 10, "C" => 100, "D" => 1000 }[t] *
p.length) if not p.nil? }
end
def possible_moves(pods, ((x, y), t))
return [] if finalpos(pods, [[x, y], t])
dx = {"A" => 3, "B" => 5, "C" => 7, "D" => 9}[t]
( ( ( y != 1 ) ? HALLWAY : [] ) +
(@@ymax..2).step(-1).find { |y| pods[[dx, y]] != t }.then {|y| [[dx, y]] }
).map { |(xh, yh)|
cost = pathcost(pods, t, [x, y], [xh, yh])
[ [xh, yh], cost ] if cost
}.compact
end
def tryall(pods, energy=0)
@@ymax ||= pods.keys.map(&:last).max
@@cache ||= {} # pods -> energy
match = @@cache.fetch(pods, nil)
return nil if match and match <= energy
@@cache[pods] = energy
return energy if pods.all? { |pod| finalpos(pods, pod) }
allmoves = pods.map { |pod|
possible_moves(pods, pod).map { |mov| [pod, mov] } }.flatten(1)
allmoves.map { |((x1, y1), t), ((x2, y2), en)|
newpods = pods.except( [x1, y1] ).update({ [x2, y2] => t })
tryall(newpods, energy + en)
}.compact.min
end
end
# 16506 (took ~50s)
def d21231()
include D2123
pods = { [3, 2] => "B", [5, 2] => "A", [7, 2] => "B", [9, 2] => "C",
[3, 3] => "D", [5, 3] => "A", [7, 3] => "D", [9, 3] => "C" }
tryall(pods)
end
# 48304 (took ~200s)
def d21232()
include D2123
pods = { [3, 2] => "B", [5, 2] => "A", [7, 2] => "B", [9, 2] => "C",
[3, 3] => "D", [5, 3] => "C", [7, 3] => "B", [9, 3] => "A",
[3, 4] => "D", [5, 4] => "B", [7, 4] => "A", [9, 4] => "C",
[3, 5] => "D", [5, 5] => "A", [7, 5] => "D", [9, 5] => "C" }
tryall(pods)
end
# ###########################################################################
#
# 2021 DAY 22
#
# ###########################################################################
# 587785
def d21221()
input(2122).split("\n")
.map { |line| line.scan(/(?:on|off|[-0-9]+)/) }
.map { |sw, *ps| [sw] + ps.map(&:to_i) }
.filter { |_, *ps| ps.all? { |c| c.between?(-50, 50) } }
.tap { |steps| debug("nbsteps", steps.length) }
.reduce(Set[]) { |acc, (sw, x1, x2, y1, y2, z1, z2)|
newset = (x1..x2).to_a.product((y1..y2).to_a, (z1..z2).to_a).to_set
acc = (sw == "on") ? (acc | newset) : (acc - newset)
debug("len", acc.length)
acc
}
.size
end
# ["on x=0..9,y=0..9,z=0..9",
# "on x=4..6,y=4..6,z=4..6"]
# x1 x2 y1 y2 z1 z2
# [ 0, 9, 0, 9, 0, 9 ] break [ 4, 6, 4, 6, 4, 6 ]
# 9 A A A A D D D B B B
# 8 A A A A D D D B B B
# 7 A A A A D D D B B B
# 6 A A A A . . . B B B
# 5 A A A A . . . B B B
# 4 A A A A . . . B B B
# 3 A A A A C C C B B B
# 2 A A A A C C C B B B
# 1 A A A A C C C B B B
# 0 A A A A C C C B B B
# / 0 1 2 3 4 5 6 7 8 9
#y4-6z7-9 A F F F B
#y4-6z4-6 A . . . B
#y4-6z0-3 A E E E B
# [ 0, 3, 0, 9, 0, 9 ] A lowx (front)
# [ 7, 9, 0, 9, 0, 9 ] B highx (back)
# [ 4, 6, 0, 3, 0, 9 ] C lowy (below)
# [ 4, 6, 7, 9, 0, 9 ] D highy (above)
# [ 4, 6, 4, 6, 0, 3 ] E lowz (lowdeep)
# [ 4, 6, 4, 6, 7, 9 ] F highz (hghdeep)
# 1167985679908143
def d21222()
input(2122).split("\n")
.map { |line| line.scan(/(?:on|off|[-0-9]+)/) }
.map { |sw, *ps| [sw] + ps.map(&:to_i) }
# .filter { |_, *ps| ps.all? { |c| c.between?(-50, 50) } }
.tap { |steps| debug("nbsteps", steps.length) }
.reduce([]) { |acc, (sw, nx1, nx2, ny1, ny2, nz1, nz2)|
# maintain the list of non-overlapping "on" cubes
newcubes =
acc.map { |(ox1, ox2, oy1, oy2, oz1, oz2)|
if ( nx1 > ox2 or nx2 < ox1 or
ny1 > oy2 or ny2 < oy1 or
nz1 > oz2 or nz2 < oz1 )
# old cube does not overlap -> keep it unchanged
[[ox1, ox2, oy1, oy2, oz1, oz2]]
else
# old cube overlaps -> lets do the breaking
gx1, sx2 = [ox1, nx1].max, [ox2, nx2].min
gy1, sy2 = [oy1, ny1].max, [oy2, ny2].min
[
(nx1 > ox1) ? [ox1 , nx1-1, oy1 , oy2 , oz1 , oz2 ] : nil,
(nx2 < ox2) ? [nx2+1, ox2 , oy1 , oy2 , oz1 , oz2 ] : nil,
(ny1 > oy1) ? [gx1 , sx2 , oy1 , ny1-1, oz1 , oz2 ] : nil,
(ny2 < oy2) ? [gx1 , sx2 , ny2+1, oy2 , oz1 , oz2 ] : nil,
(nz1 > oz1) ? [gx1 , sx2 , gy1 , sy2 , oz1 , nz1-1] : nil,
(nz2 < oz2) ? [gx1 , sx2 , gy1 , sy2 , nz2+1, oz2 ] : nil,
].compact
end
}.flatten(1)
# add the new cube (now non-overlapping) if it is "on"
newcubes.push([nx1, nx2, ny1, ny2, nz1, nz2]) if sw == "on"
newcubes
}
.map { |(x1, x2, y1, y2, z1, z2)|
(x2 - x1 + 1) * (y2 - y1 + 1) * (z2 - z1 + 1)
}.sum
end
# ###########################################################################
#
# 2021 DAY 21
#
# ###########################################################################
# 679329
def d21211()
pps = input(2121).split("\n").map{ _1.split.last.to_i }
psc, die, n = [0, 0], 0, 0
loop do
roll = [1, 2, 3].map { (die + _1) % 100}.sum
die = (die + 3) % 100
pps[n % 2] = (((pps[n % 2] + roll) - 1) % 10) + 1
psc[n % 2] = psc[n % 2] + pps[n % 2]
n = n + 1
break if psc.max >= 1000
end
psc.min * n * 3
end
#
# # total roll for player 1 at round n :
# r1 = ( (( n * (n + 1) * 18 ) / 2 ) + (6 * (n + 1)) )
#
#
# # position of player 1 at round n :
# p1 = ( ( init1 + r1 - 1 ) % 10 ) + 1
#
#
# # score of player 1 (depends on previous round)
# s1 = s1 + p1
#
# 433315766324816 (took ~15s) ugly and slow solution
def d21212()
def nextplay(play, n)
play.to_a
.product( [ [3, 1], [4, 3], [5, 6], [6, 7], [7, 6], [8, 3], [9, 1] ] )
.map { |( (pos1, scr1, pos2, scr2), nbu), (newrol, nbr)|
if n == 1
pos1 = ( ( ( pos1 + newrol - 1 ) % 10 ) + 1 )
scr1 += pos1
else
pos2 = ( ( ( pos2 + newrol - 1 ) % 10 ) + 1 )
scr2 += pos2
end
{ [ pos1, scr1, pos2, scr2 ] => nbu * nbr } }
.reduce( {} ) { |acc, nh|
acc.merge(nh) { |_, oldv, newv| oldv + newv} }
end
p1, p2 = input(2121).split("\n").map{ _1.split.last.to_i }
play = { [ p1, 0, p2, 0 ] => 1 }
nb1, nb2 = 0, 0
loop do
play = nextplay( play, 1 )
play, endx = play.partition{ |(r1, s1, r2, s2), n| s1 < 21 }
nb1 += endx.map(&:last).sum
play = nextplay( play, 2 )
play, endx = play.partition{ |(r1, s1, r2, s2), n| s2 < 21 }
nb2 += endx.map(&:last).sum
break [nb1, nb2].max if play.empty?
end
end
# cleaner and faster solution
def d2121rec()
p1, p2 = input(2121).split("\n").map{ _1.split.last.to_i }
@cache = { }
def play(p1, s1, p2, s2)
return [1, 0] if s1 >= 21
return [0, 1] if s2 >= 21
match = @cache.fetch([p1, s1, p2, s2], nil)
return match if match
[1, 2, 3].product([1, 2, 3], [1, 2, 3])
.map(&:sum).map { |roll|
np1 = ( ( ( p1 + roll - 1 ) % 10 ) + 1 )
play(p2, s2, np1, s1 + np1).reverse }
.transpose.map(&:sum)
.tap { |a1, a2| @cache[[p1, s1, p2, s2]] = [a1, a2] }
end
play(p1, 0, p2, 0).max
end
# ###########################################################################
#
# 2021 DAY 20
#
# ###########################################################################
module D2120
def resize(img, siz)
((xmin, ymin), (xmax, ymax)) = img.keys().minmax
((xmin - siz)..(xmax + siz)).map { |x|
((ymin - siz)..(ymax + siz)).map { |y|
[[x, y], img.fetch([x, y], ".")] }
}.flatten(1).to_h
end
def getimage(lines)
lines.each_with_index.map { |l, y|
l.chars.each_with_index.map { |c, x| [[x, y], c] }
}.flatten(1).to_h
end
def enhance(enh, img)
((xmin, ymin), (xmax, ymax)) = img.keys().minmax
((xmin)..(xmax)).map { |x|
((ymin)..(ymax)).map { |y|
[[x - 1, y - 1], [x, y - 1], [x + 1, y - 1],
[x - 1, y ], [x, y ], [x + 1, y ],
[x - 1, y + 1], [x, y + 1], [x + 1, y + 1]]
.map { |nx, ny| img.fetch([nx, ny], ".") }
.map { |c| {"#" => "1", "." => "0"}[c] }.join.to_i(2)
.then{ |i| [ [x, y], enh[i] ] }
} }.flatten(1).to_h
end
def tostr(img)
((xmin, ymin), (xmax, ymax)) = img.keys().minmax
(ymin..ymax).map { |y| (xmin..xmax).map { |x| img[[x,y]] }.join }.join("\n")
end
def enhloop(enh, img, nbiter, debug=false)
# not sure if (+3, -1) works for all enhancement lists
img = resize(img, nbiter + 3)
(1..nbiter).each { |n|
img = enhance(enh, img)
img = resize(resize(img, -1), 1) if n.even?
puts tostr(img) + "\n\n" if debug }
img
end
end
# 5065
def d21201()
include D2120
enh, _, *inp = input(2120).split("\n")
enhloop(enh, getimage(inp), 2)
.values.filter { _1 == "#" }.length
end
# 14790 (took ~30s)
def d21202()
include D2120
enh, _, *inp = input(2120).split("\n")
enhloop(enh, getimage(inp), 50)
.values.filter { _1 == "#" }.length
end
# ................. ................. ................. .................
# ................. ................. ................. ...........##....
# ................. ................. ...........#..... .....##.#.###....
# ................. ......##.##...... .....#..#.#...... ....###..#.#.#...
# ......#..#....... .....#..#.#...... ....#.#...###.... ...###..#.#.##...
# ......#.......... .....##.#..#..... ....#...##.#..... ...####.##.#.....
# ......##..#...... .....####..#..... ....#.....#.#.... ...###..#...##...
# ........#........ ......#..##...... .....#.#####..... ....#.#....#.....
# ........###...... .......##..#..... ......#.#####.... .....##.#..#.#...
# ................. ........#.#...... .......##.##..... ......#.####.....
# ................. ................. ........###...... .......####......
# ................. ................. ................. ........#.#......
# ................. ................. ................. .................
# ................. ################# ................. #################
# ................. ################# ................. #################
# ................. ################# ................. ######.##########
# ................. ################# .....#.#......... ####...##.#######
# ......#..#....... ######...######## ....###.##....... ###.#.#.#..######
# ......#.......... #####.#.#..###### ........##....... ####.#....#######
# ......##..#...... ######....####### ....##.##.#...... ###.#...#..######
# ........#........ ######......##### ........##....... ####..####.######
# ........###...... ########...###### .....#...#....... ####.############
# ................. ########..####### .........#....... #########.#######
# ................. ################# .......#......... ######.##########
# ................. ################# ................. #################
# ................. ################# ................. #################
# ###########################################################################
#
# 2021 DAY 19
#
# ###########################################################################
module D2119
# if report n0 and report n1 overlap,
# -> update coordinates from report n1 relatively to n0 (absolute coord)
# -> return coordinates of scanner n1
def adjust(reports, n0, n1)
# 48 configs - should be 24 ???
orientations =
[0, 1, 2]
.permutation(3).to_a
.product([-1, 1].product([-1, 1], [-1, 1]))
.map { |a, b| a.zip(b) }
orientations.each { |(a1, d1), (a2, d2), (a3, d3)|
# optim? we would still find overlap without trying 11 first points
reports[n0].each { |x0, y0, z0|
reports[n1].each { |r1|
# for each orientation, try every pair of points from r0 & r1
# if we consider them as the same point, how many other points match ?
x1, y1, z1 = r1[a1] * d1, r1[a2] * d2, r1[a3] * d3
xoff, yoff, zoff = x1 + x0, y1 + y0, z1 + z0
# position of scanner n1 is [xoff, yoff, zoff] ( from scanner n0 pov )
reports_n1_adjusted = reports[n1].map { |r1|
[xoff - r1[a1] * d1, yoff - r1[a2] * d2, zoff - r1[a3] * d3] }
#
nboverlaps = reports_n1_adjusted.intersection(reports[n0]).length
next if nboverlaps < 12
reports[n1] = reports_n1_adjusted
return xoff, yoff, zoff
} } }
false
end
# assemble the full map
# -> update all reports with absolute coordinates (relative to first scanner)
# -> return list of coords of all scanners
def assemble(reports)
n_done, n_todo = [0], reports.each_index.drop(1)
n_ttry = n_done.dup # to be tried as ref
sc_pos = [[0, 0, 0]] # scanner positions
while not n_todo.empty?
nref = n_ttry.shift()
n_todo.dup.each { |n1|
s1_pos = adjust(reports, nref, n1)
next unless s1_pos
n_todo.delete(n1)
n_ttry.push(n1)
n_done.push(n1)
sc_pos.push(s1_pos) }
end
sc_pos
end
# parse reports of all scanners
def getreports(input)
input.split("\n\n")
.map { |s| s.split("\n") }.map { |_, *lines|
lines.map { |l| l.split(",").map(&:to_i) } }
end
end
# 394 (took > 300s !)
def d21191()
include D2119
reports = getreports(input(2119))
assemble(reports)
reports.flatten(1).uniq.length
end
# 12304
def d21192()
include D2119
reports = getreports(input(2119))
scanpos = assemble(reports)
scanpos.combination(2).map { |(x1, y1, z1), (x2, y2, z2)|
(x2 - x1).abs + (y2 - y1).abs + (z2 - z1).abs }.max
end
# ###########################################################################
#
# 2021 DAY 18
#
# ###########################################################################
module D2118
# ##################################################################
# explodes(inp)
# modifies its argument by performing first explosion
# returns true if something changed, false otherwise
def explode(inp, elt = inp, crd = [])
case elt
in [Integer => e0, Integer => e1] if crd.length >= 4 then
# on a deep pair of numbers -> explode criteria met
def allnums(elt, crd = [])
return [crd] if elt.is_a? Integer
allnums(elt[0], crd + [0]) + allnums(elt[1], crd + [1])
end
allnums = allnums( inp )
# update first number on the right
idx = allnums.index(crd + [0]) - 1
if (0...allnums.length).include?(idx)
*hd, ls = allnums[idx]
hd.reduce(inp, :fetch)[ls] += e0
end
# update first number on the left
idx = allnums.index(crd + [1]) + 1
if (0...allnums.length).include?(idx)
*hd, ls = allnums[idx]
hd.reduce(inp, :fetch)[ls] += e1
end
# replace exploding pair by number 0
*hd, ls = crd
hd.reduce(inp, :fetch)[ls] = 0
return true # list was modified
in Integer then
# on a regular number -> nothing to do
return false # nothing changed
in [e0, e1] then
# on a pair -> look deeper
return (explode(inp, e0, crd + [0]) or explode(inp, e1, crd + [1]))
end
end
# ##################################################################
# split(inp)
# modifies its argument by performing first split
# returns true if something changed, false otherwise
def split(inp, elt = inp, crd = [])
case elt
in Integer => val if val >= 10 then
# on a number >= 10 -> split criteria met
# insert the new pair
*hd, ls = crd
hd.reduce(inp, :fetch)[ls] = [val.fdiv(2).floor, val.fdiv(2).ceil]
return true
in Integer then
# on another integer -> nothing to do
return false
in [e0, e1] then
# on a pair -> continue digging
return (split(inp, e0, crd + [0]) or split(inp, e1, crd + [1]))
end
end
# ##################################################################
# addred(lst)
# returns reduced addition of given snailfish numbers in lst
# ( does not modify anything )
def addred(lst)
def deepcp(elt)
case elt
in [_, _] then elt.map{ deepcp _1 }
in _ then elt
end
end
def reduce(inp)
loop do
next if explode(inp)
next if split(inp)
return inp
end
end
# lst must be copied first as it will be modified by explode & split
deepcp(lst).reduce { |acc, obj| reduce([acc, obj]) }
end
# ##################################################################
def magnitude(elt)
case elt
in Integer then elt
in [e0, e1] then
3 * magnitude(e0) + 2 * magnitude(e1)
end
end
# ##################################################################
end
# 3699
def d21181()
include D2118
input(2118).split("\n")
.then { |l| eval("[%s]" % l.join(",")) }
.then { |l| magnitude(addred(l)) }
end
# 4735
def d21182()
include D2118
input(2118).split("\n")
.then { |l| eval("[%s]" % l.join(",")) }
.then { |l|
l.combination(2).map{ |a, b|
[ magnitude(addred([a, b])),
magnitude(addred([b, a])) ]
}.flatten.max }
end
def d2118unit()
include D2118
def testfunc(fct, inp) send(fct, inp); inp end
# ##################################################################
# explode
fail unless testfunc(:explode, [[[[[9,8],1],2],3],4]) == [[[[0,9],2],3],4]