-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadv22.rb
More file actions
executable file
·1827 lines (1568 loc) · 46.4 KB
/
adv22.rb
File metadata and controls
executable file
·1827 lines (1568 loc) · 46.4 KB
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'
# ##############################################################################
#
# 2022 DAY 25
#
# ##############################################################################
# SNAFU Decimal
# 1=-0-2 1747
# 12111 906
# 2=0= 198
# 21 11
# 2=01 201
# 111 31
# 20012 1257
# 112 32
# 1=-1= 353
# 1-12 107
# 12 7
# 1= 3
# 122 37
def snaf2dec(s)
cs = { "2" => 2, "1" => 1, "0" => 0, "-" => -1, "=" => -2 }
s.chars.reverse.each_with_index.map { |c, n| cs[c] * 5**n }.sum
end
def dec2snaf(i)
while i > 0 do
i, r = (i + 2).divmod(5); ( rs ||= [] ) << r
end
rs.reverse.map{ ["=", "-", "0", "1", "2"][_1] }.join
end
# 2=000=22-0-102=-1001
def d22251()
input(2225).split("\n").map { snaf2dec(_1) }.sum.then { dec2snaf(_1) }
end
# ##############################################################################
#
# 2022 DAY 24
#
# ##############################################################################
# #.######
# #>>.<^<#
# #.<..<<#
# #>v.><>#
# #<^v^^>#
# ######.#
def getblizzs()
blizz =
input(2224).split("\n").each_with_index.map { |l, y|
l.chars.each_with_index.filter_map { |c, x|
case c
when ">" then [x - 1, y - 1, 1, 0]
when "<" then [x - 1, y - 1, -1, 0]
when "^" then [x - 1, y - 1, 0, -1]
when "v" then [x - 1, y - 1, 0, 1]
else nil end
} }.flatten(1)
xmax, ymax = blizz.map { |x, y, _, _| [x, y] }.transpose.map(&:max)
blizy = blizz.filter{ |x, y, dx, dy| dy == 0 }.group_by { |_, y, _, _| y }
blizx = blizz.filter{ |x, y, dx, dy| dx == 0 }.group_by { |x, _, _, _| x }
[blizx, blizy, xmax, ymax]
end
def expmoves(blizx, blizy, xmax, ymax, x, y, t)
[ [x, y - 1], [x - 1, y], [x, y], [x + 1, y], [x, y + 1] ]
.filter_map { |nx, ny|
[nx, ny, t + 1] if not (
# invalid position ?
(((nx < 0) or (nx > xmax) or (ny < -1) or (ny == -1 and nx != 0) or
(ny > (ymax + 1)) or (ny == (ymax + 1) and nx != xmax)) ) or
# vertical bliz here ?
(blizx.fetch(nx, []).any? { |_, wy, _, wdy|
ny == (wy + wdy * (t + 1)) % (ymax + 1) }) or
# horizontal bliz here ?
(blizy.fetch(ny, []).any? { |wx, _, wdx, _|
nx == (wx + wdx * (t + 1)) % (xmax + 1) }) ) }
end
def blizzwalk(blizx, blizy, xmax, ymax, start, goal, t0)
visiting = Set[]
dst = lambda { |(x1, y1), (x2, y2)| (x2 - x1).abs + (y2 - y1).abs }
tovisit = [ [*start, t0, t0 + dst.(start, goal)] ]
while not tovisit.empty? do
x, y, t, d = tovisit.shift
debug(visiting.length) if [x, y] == goal
return t if [x, y] == goal
expmoves(blizx, blizy, xmax, ymax, x, y, t)
.filter { |mv| not visiting.include?(mv) }
.each { |mv| visiting.add(mv) }
.each { |nx, ny, nt|
nd = nt + dst.([nx, ny], goal)
ps = tovisit.index { |_, _, _, _d| _d >= nd } || -1
tovisit.insert(ps, [nx, ny, nt, nd])
}
end
end
# 269 (2s)
def d22241()
blizx, blizy, xmax, ymax = getblizzs()
start, goal = [0, -1], [xmax, ymax + 1]
blizzwalk(blizx, blizy, xmax, ymax, start, goal, 0)
end
# 825 (7s)
def d22242()
blizx, blizy, xmax, ymax = getblizzs()
start, goal = [0, -1], [xmax, ymax + 1]
t1 = debug(blizzwalk(blizx, blizy, xmax, ymax, start, goal, 0))
t2 = debug(blizzwalk(blizx, blizy, xmax, ymax, goal, start, t1))
debug(blizzwalk(blizx, blizy, xmax, ymax, start, goal, t2))
end
# ##############################################################################
#
# 2022 DAY 23
#
# ##############################################################################
# ..... # ..##. # ..... # ..#..
# ..##. # ..... # ..##. # ....#
# ..#.. # ..#.. # .#... # #....
# ..... # ...#. # ....# # ....#
# ..##. # ..#.. # ..... # .....
# ..... # ..... # ..#.. # ..#..
def getelves()
input(2223).split("\n").each_with_index.map { |l, y|
l.chars.each_with_index.filter_map { |c, x| [x, y] if c == "#" }
}.flatten(1).to_set
end
def checkdir(elves, dir, x, y)
case dir
when "N" then
return [x, y - 1] if not Set[[x, y - 1], [x - 1, y - 1], [x + 1, y - 1]].intersect?(elves)
when "S" then
return [x, y + 1] if not Set[[x, y + 1], [x - 1, y + 1], [x + 1, y + 1]].intersect?(elves)
when "W" then
return [x - 1, y] if not Set[[x - 1, y], [x - 1, y + 1], [x - 1, y - 1]].intersect?(elves)
when "E" then
return [x + 1, y] if not Set[[x + 1, y], [x + 1, y + 1], [x + 1, y - 1]].intersect?(elves)
else fail end
return nil
end
def checkadj(elves, x, y)
Set[[x - 1, y - 1], [x , y - 1], [x + 1, y - 1],
[x - 1, y ], [x + 1, y ],
[x - 1, y + 1], [x , y + 1], [x + 1, y + 1]
].intersect?(elves)
end
def elvesround(elves, n)
dirs = ["N", "S", "W", "E"].rotate(n - 1)
# first half -> propositions
props = {}
for x, y in elves do
next if not checkadj(elves, x, y)
for d in dirs do
pr = checkdir(elves, d, x, y)
next if not pr
(props[pr] ||= []) << [x, y]
break
end
end
# second half -> move
for k, v in props.filter { |_, l| l.length == 1 } do
elves.delete(v.first)
elves.add(k)
end
#
elves
end
# 4236
def d22231()
elves = getelves()
for n in (1..10) do
elves = elvesround(elves, n)
end
(xmin, xmax), (ymin, ymax) = elves.to_a.transpose.map(&:minmax)
(xmax + 1 - xmin) * (ymax + 1 - ymin) - elves.length
end
# 1023 (55s)
def d22232()
elves = getelves()
for n in (1..) do
debug(n) if (n % 100) == 0
elve2 = elves.clone
elves = elvesround(elves, n)
break n if elves == elve2
end
end
# ##############################################################################
#
# 2022 DAY 22
#
# ##############################################################################
def nextpos(board, xmax, ymax, x, y, f)
# move
nx, ny = [x + 1, y] if f == "E"
nx, ny = [x, y + 1] if f == "S"
nx, ny = [x - 1, y] if f == "W"
nx, ny = [x, y - 1] if f == "N"
# off-map -> wrap around
if not board[[nx, ny]] then
case f
when "E" then
nx = x.downto(-1).map { |xx| break (xx + 1) if not board[[xx, ny]] }
when "W" then
nx = x.upto(xmax+1).map { |xx| break (xx - 1) if not board[[xx, ny]] }
when "S" then
ny = y.downto(-1).map { |yy| break (yy + 1) if not board[[nx, yy]] }
when "N" then
ny = y.upto(ymax+1).map { |yy| break (yy - 1) if not board[[nx, yy]] }
else fail end
end
# wall -> stop
return [x, y] if board[[nx, ny]] == "#"
[nx, ny]
end
# 164014
def d22221()
board, path = input(2222).split("\n\n")
board = board.split("\n").each_with_index.map { |l, y|
l.chars.each_with_index.filter_map { |c, x| [[x + 1, y + 1], c] if c != " " }
}.flatten(1).to_h.then { _1.default = nil; _1 }
path = path.scan(/\d+|[A-Z]/)
xmax, ymax = board.keys.transpose.map(&:max)
dirs = ['E', 'S', 'W', 'N']
x, y, f = (0..).find { |x| board[[x, 1]] == '.' }, 1, 'E'
for i in path
if i == "R" then
f = dirs[(dirs.find_index(f) + 1) % 4]
elsif i == "L" then
f = dirs[(dirs.find_index(f) - 1) % 4]
else
for i in (1..i.to_i).to_a do
x, y = nextpos(board, xmax, ymax, x, y, f)
end
end
end
[4 * x, 1000 * y, dirs.find_index(f)].sum
end
# _ 2 3
# _ 5 _
# 7 8 _
# 10 _ _
def nextposp2(board, xmax, ymax, x, y, f)
# move
nx, ny, nf = [x + 1, y, f] if f == "E"
nx, ny, nf = [x, y + 1, f] if f == "S"
nx, ny, nf = [x - 1, y, f] if f == "W"
nx, ny, nf = [x, y - 1, f] if f == "N"
# off-map -> find new pos on cube and new facing
if not board[[nx, ny]] then
if false then
nil
elsif f == "N" and y == 1 and x >= 1 and x <= 100 then
nx, ny, nf = 1, 100 + nx, "E"
elsif f == "W" and x == 1 and y >= 151 and y <= 200 then
nx, ny, nf = ny - 100, 1, "S"
elsif f == "W" and x == 1 and y >= 101 and y <= 150 then
nx, ny, nf = 51, 151 - ny, "E"
elsif f == "W" and x == 51 and y >= 1 and y <= 50 then
nx, ny, nf = 1, 151 - ny, "E"
elsif f == "N" and y == 1 and x >= 101 and x <= 150 then
nx, ny, nf = nx - 100, 200, "N"
elsif f == "S" and y == 200 and x >= 1 and x <= 50 then
nx, ny, nf = nx + 100, 1, "S"
elsif f == "E" and x == 100 and y >= 51 and y <= 100 then
nx, ny, nf = 50 + ny, 50, "N"
elsif f == "N" and y == 101 and x >= 1 and x <= 50 then
nx, ny, nf = 51, 50 + nx, "E"
elsif f == "W" and x == 51 and y >= 51 and y <= 100 then
nx, ny, nf = ny - 50, 101, "S"
elsif f == "E" and x == 150 and y >= 1 and y <= 50 then
nx, ny, nf = 100, 151 - ny, "W"
elsif f == "S" and y == 50 and x >= 101 and x <= 150 then
nx, ny, nf = 100, nx - 50, "W"
elsif f == "E" and x == 100 and y >= 101 and y <= 150 then
nx, ny, nf = 150, 151 - ny, "W"
elsif f == "S" and y == 150 and x >= 51 and x <= 100 then
nx, ny, nf = 50, 100 + nx, "W"
elsif f == "E" and x == 50 and y >= 151 and y <= 200 then
nx, ny, nf = ny - 100, 150, "N"
# XXXX other transitions not needed
else debug("XXX", [x, y, f]); fail; end
end
# wall -> stop
return [x, y, f] if board[[nx, ny]] == "#"
[nx, ny, nf]
end
# 135059 -> too high
# 183022 -> too high
# 47525
def d22222()
board, path = input(2222).split("\n\n")
board = board.split("\n").each_with_index.map { |l, y|
l.chars.each_with_index.filter_map { |c, x| [[x + 1, y + 1], c] if c != " " }
}.flatten(1).to_h.then { _1.default = nil; _1 }
path = path.scan(/\d+|[A-Z]/)
xmax, ymax = board.keys.transpose.map(&:max)
dirs = ['E', 'S', 'W', 'N']
x, y, f = (0..).find { |x| board[[x, 1]] == '.' }, 1, 'E'
for i in path
if i == "R" then
f = dirs[(dirs.find_index(f) + 1) % 4]
elsif i == "L" then
f = dirs[(dirs.find_index(f) - 1) % 4]
else
for n in (1..i.to_i).to_a do
x, y, f = nextposp2(board, xmax, ymax, x, y, f)
end
end
end
debug(x, y, f)
[4 * x, 1000 * y, dirs.find_index(f)].sum
end
# ##############################################################################
#
# 2022 DAY 21
#
# ##############################################################################
# root: pppw + sjmn
# dbpl: 5
# cczh: sllz + lgvd
# zczc: 2
# ptdq: humn - dvpt
# dvpt: 3
# lfqf: 4
# humn: 5
# ljgn: 2
# sjmn: drzm * dbpl
# sllz: 4
# pppw: cczh / lfqf
# lgvd: ljgn * ptdq
# drzm: hmdt - zczc
# hmdt: 32
def getmonkeyops()
input(2221).split("\n")
.map { |line| mk, *mo = line.split(/[ :]+/); [mk, mo] }.to_h
end
def evalmonkey(monkeys, monkey)
case monkeys[monkey]
in [x] then x.to_i
in [x, "+", y] then evalmonkey(monkeys, x) + evalmonkey(monkeys, y)
in [x, "-", y] then evalmonkey(monkeys, x) - evalmonkey(monkeys, y)
in [x, "*", y] then evalmonkey(monkeys, x) * evalmonkey(monkeys, y)
in [x, "/", y] then evalmonkey(monkeys, x) / evalmonkey(monkeys, y)
else fail end
rescue
nil
end
# 85616733059734
def d22211()
monkeys = getmonkeyops()
evalmonkey(monkeys, "root")
end
# 3560324848168
def d22212()
monkeys = getmonkeyops()
monkeys["humn"] = nil # will fail eval
monkeys["root"][1] = "="
tovisit, expectd = "root", 0
while true do
debug("TOVISIT", tovisit, monkeys[tovisit], expectd)
break expectd if tovisit == "humn"
r1, op, r2 = monkeys[tovisit]
v1, v2 = evalmonkey(monkeys, r1), evalmonkey(monkeys, r2)
fail unless v1 or v2
tovisit = v1 ? r2 : r1
case [v1 , op , v2 ]
in [nil, "+", v2 ] then expectd = expectd - v2 # ? + v2 == X -> X - v2
in [v1 , "+", nil] then expectd = expectd - v1 # v1 + ? == X -> X - v1
in [nil, "-", v2 ] then expectd = expectd + v2 # ? - v2 == X -> X + v2
in [v1 , "-", nil] then expectd = v1 - expectd # v1 - ? == X -> v1 - X
in [nil, "/", v2 ] then expectd = expectd * v2 # ? / v2 == X -> X * v2
in [v1 , "/", nil] then expectd = v1 / expectd # v1 / ? == X -> v1 / X
in [nil, "*", v2 ] then expectd = expectd / v2 # ? * v2 == X -> X / v2
in [v1 , "*", nil] then expectd = expectd / v1 # v1 * ? == X -> X / v1
in [v1, "=", v2 ] then expectd = (v1 or v2) # initial root case
else fail end
end
end
# ##############################################################################
#
# 2022 DAY 20
#
# ##############################################################################
# 5962
def d22201()
list = input(2220).split.map(&:to_i)
indx = (0...list.length).to_a
for x, n in list.each_with_index do
i = indx.find_index(n)
indx.rotate!(i)
z = indx.shift
indx.rotate!(x)
indx.unshift(z)
end
list = indx.map {|i| list[i] }
list.rotate!(list.find_index(0))
[1000, 2000, 3000].map { |n| list.rotate(n).first }.sum
end
# 9862431387256
def d22202()
list = input(2220).split.map(&:to_i).map { _1 * 811589153 }
indx = (0...list.length).to_a
10.times do
for x, n in list.each_with_index do
i = indx.find_index(n)
indx.rotate!(i)
z = indx.shift
indx.rotate!(x)
indx.unshift(z)
end
end
list = indx.map {|i| list[i] }
list.rotate!(list.find_index(0))
[1000, 2000, 3000].map { |n| list.rotate(n).first }.sum
end
# ##############################################################################
#
# 2022 DAY 19
#
# ##############################################################################
# Blueprint 1:
# Each ore robot costs 4 ore.
# Each clay robot costs 2 ore.
# Each obsidian robot costs 3 ore and 14 clay.
# Each geode robot costs 2 ore and 7 obsidian.
# Blueprint 2:
# Each ore robot costs 2 ore.
# Each clay robot costs 3 ore.
# Each obsidian robot costs 3 ore and 8 clay.
# Each geode robot costs 3 ore and 12 obsidian.
# [ [1, 4, 2, 3, 14, 2, 7], [2, 2, 3, 3, 8, 3, 12] ]
def geobreak(bp, timeleft)
xore, yore, zore, zcla, wore, wobs = bp
more = [xore, yore, zore, wore].max
visited, best = Set[], 0
tovisit = [[
0, 0, 0, 0, # nore, ncla, nobs, ngeo
1, 0, 0, 0, # rore, rcla, robs, rgeo
timeleft # time
]]
while not tovisit.empty? do
current = tovisit.shift
nore, ncla, nobs, ngeo, rore, rcla, robs, rgeo, time = current
best = ngeo if ngeo > best
next if time == 0
# ############################################
# speed up exploration by simplifying similar values
# XXX there is surely something way better to do here :-(
# XXX keep best (ngeo) tovisits states for each time val ?
# XXX simplif before adding to tovisit stack ?
# XXX do not create robot if not enough time for next elt ?
# -> do not generate more elemts than 2 * max consumption ?
nobs = [nobs, wobs * 2].min
ncla = [ncla, zcla * 2].min
nore = [nore, more * 2].min
# -> do not generate more robots than max consumption
robs = [robs, wobs].min
rcla = [rcla, zcla].min
rore = [rore, more].min
#
current = nore, ncla, nobs, ngeo, rore, rcla, robs, rgeo, time
# ############################################
next if visited.include?(current)
visited << current
# create rgeo
if nore >= wore and nobs >= wobs then
tovisit << [
nore + rore - wore, ncla + rcla, nobs + robs - wobs, ngeo + rgeo,
rore, rcla, robs, rgeo + 1, time - 1 ]
next # create rgeo is always the best choice
end
# create robs
if nore >= zore and ncla >= zcla then
tovisit << [
nore + rore - zore, ncla + rcla - zcla, nobs + robs, ngeo + rgeo,
rore, rcla, robs + 1, rgeo, time - 1 ]
end
# create rcla
if nore >= yore then
tovisit << [
nore + rore - yore, ncla + rcla, nobs + robs, ngeo + rgeo,
rore, rcla + 1, robs, rgeo, time - 1 ]
end
# create rore
if nore >= xore then
tovisit << [
nore + rore - xore, ncla + rcla, nobs + robs, ngeo + rgeo,
rore + 1, rcla, robs, rgeo, time - 1 ]
end
# create nothing
tovisit << [
nore + rore, ncla + rcla, nobs + robs, ngeo + rgeo,
rore, rcla, robs, rgeo, time - 1 ]
end
best
end
# debug: 1 9 9
# debug: 2 0 0
# debug: 3 1 3
# debug: 4 9 36
# debug: 5 0 0
# debug: 6 2 12
# debug: 7 8 56
# debug: 8 1 8
# debug: 9 0 0
# debug: 10 0 0
# debug: 11 6 66
# debug: 12 2 24
# debug: 13 2 26
# debug: 14 0 0
# debug: 15 2 30
# debug: 16 0 0
# debug: 17 0 0
# debug: 18 13 234
# debug: 19 0 0
# debug: 20 7 140
# debug: 21 6 126
# debug: 22 0 0
# debug: 23 15 345
# debug: 24 1 24
# debug: 25 3 75
# debug: 26 0 0
# debug: 27 4 108
# debug: 28 13 364
# debug: 29 6 174
# debug: 30 10 300
# 2160 (20s)
def d22191()
input(2219)
.split("\n")
.map { |l| l.scan(/\d+/).map(&:to_i) }
.map { |(nb, *bp)| x = geobreak(bp, 24); debug(nb, x, nb * x); nb * x }
.sum
end
# debug: 58
# debug: 10
# debug: 23
# 13340 (25s)
def d22192()
input(2219)
.split("\n")
.take(3)
.map { |l| l.scan(/\d+/).map(&:to_i) }
.map { |(nb, *bp)| x = geobreak(bp, 32); debug(x); x }
.reduce(&:*)
end
# ##############################################################################
#
# 2022 DAY 18
#
# ##############################################################################
def neighbors((x, y, z))
[ [x - 1, y, z], [x + 1, y, z],
[x, y - 1, z], [x, y + 1, z],
[x, y, z - 1], [x, y, z + 1] ]
end
# 4450
def d22181()
cubes =
input(2218).scan(/[0-9]+/).map(&:to_i).each_slice(3).to_set
cubes.map { |cube|
6 - neighbors(cube).filter{ |n| cubes.include?(n) }.length
}.sum
end
# 2564
def d22182()
cubes =
input(2218).scan(/[0-9]+/).map(&:to_i).each_slice(3).to_set
# list non-trapped neighbors
dmax = cubes.to_a.flatten.max + 1
reachbs, tovisit = Set[], [[dmax, dmax, dmax]]
while not tovisit.empty?
cube = tovisit.shift
reachbs.add(cube)
tovisit.push(
*neighbors(cube)
.filter { |ngh| ngh.all? { |c| (-1..dmax).cover?(c) } }
.filter { |ngh| not cubes.include?(ngh) }
.filter { |ngh| not tovisit.include?(ngh) }
.filter { |ngh| not reachbs.include?(ngh) } )
end
#
cubes.map { |cube|
6 - neighbors(cube).filter{ |n|
cubes.include?(n) or not reachbs.include?(n) }.length
}.sum
end
# ##############################################################################
#
# 2022 DAY 17
#
# ##############################################################################
#
# .... .... ..#. #... ....
# .... .#.. ..#. #... ....
# .... ###. ..#. #... ##..
# #### .#.. ###. #... ##..
#
# 0 1 2 3 4
@shapes = [
[ [0, 0], [1, 0], [2, 0], [3, 0] ],
[ [1, 0], [0, 1], [1, 1], [2, 1], [1, 2] ],
[ [0, 0], [1, 0], [2, 0], [2, 1], [2, 2] ],
[ [0, 0], [0, 1], [0, 2], [0, 3] ],
[ [0, 0], [1, 0], [0, 1], [1, 1] ] ]
# |....#..|
# |....#..|
# |....#..|
# |@@.###.|
# |@@.####|
# |.###.#.|
# |..####.|
# |..#....|
# |..#...#|
# |..#...#|
# |..#####|
# |#####..|
# |##.#...|
# |.####..|
# +-------+
def showtower(tower, shape)
ymax = (tower | shape.to_set).map(&:last).max
towr = ymax.downto(-1).map { |y|
(-1).upto(7).map { |x|
(((x == -1) or (x == 7)) and (y == -1)) ? "+"
: ((x == -1) or (x == 7)) ? "|"
: (y == -1) ? "-"
: (tower.include?([x, y])) ? "#"
: (shape.include?([x, y])) ? "@"
: "." }.join
}.join("\n")
puts towr
end
# jets, tower, tmax, nbrnd, nbshp -> newtower, newtmax, newnbrnd
def fallrock(jets, tower, tmax, nrnd, nshp)
shpn = nshp % @shapes.length # shape type number
newr = @shapes[shpn].map { |x, y| # coords of new rock
[x + 2, y + tmax + 4] }
while true do
# first, apply horizontal jet move if possible
jetn = jets[nrnd % jets.length] # jet offset
nrnd += 1
new2 = newr.map { |x, y| [x + jetn, y] }
possible = new2.all? { |x, y|
(x >= 0) and (x <= 6) and not tower.include?([x, y]) }
newr = new2 if possible
# then, fall down one level if possible
new3 = newr.map { |x, y| [x, y - 1] }
possible = new3.all? { |x, y|
(y >= 0) and not tower.include?([x, y]) }
newr = new3 if possible
break if not possible
end
tower |= newr.to_set
tmax = tower.map(&:last).max
[tower, tmax, nrnd]
end
# 3133
def d22171()
jets =
input(2217).chars.map{ _1 == ">" ? 1 : -1 }
towr, tmax, nrnd, nshp = Set[], -1, 0, 0
while true do
towr, tmax, nrnd = fallrock(jets, towr, tmax, nrnd, nshp)
nshp += 1
break if nshp == 2022
end
tmax + 1
end
# wrong frontier but it probably does not matter
# -> it should work anyway if there is at least one cycle on one of these
# convex frontiers
def getfrontier(tower, tmax)
tower
.group_by { |x, y| x }
.map { |x, cells| [x, tmax - cells.map(&:last).max] }
.sort.map(&:last)
end
# 1547953216393
def d22172()
jets =
input(2217).chars.map{ _1 == ">" ? 1 : -1 }
maxshpn = 1000000000000
cache = {} # [ frontier, jetpos, shapepos ] -> [ shapenum, tmax ]
towr, tmax, nrnd, nshp = Set[], -1, 0, 0
while true do
towr, tmax, nrnd, _nshp = fallrock(jets, towr, tmax, nrnd, nshp)
# position already known ? ###################
cachekey = [
getfrontier(towr, tmax), (nshp % @shapes.length), (nrnd % jets.length)]
match = cache.fetch(cachekey, nil)
if match then
prvnshp, prvtmax = match; cycleln = nshp - prvnshp
return (prvtmax + (tmax - prvtmax) * (maxshpn - prvnshp) / cycleln) if
((maxshpn - prvnshp) % cycleln == 0)
end
cache[cachekey] = [nshp, tmax] if not match
# ############################################
nshp += 1
end
end
# ##############################################################################
#
# 2022 DAY 16
#
# ##############################################################################
# Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
# Valve BB has flow rate=13; tunnels lead to valves CC, AA
# Valve CC has flow rate=2; tunnels lead to valves DD, BB
# Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
# Valve EE has flow rate=3; tunnels lead to valves FF, DD
# Valve FF has flow rate=0; tunnels lead to valves EE, GG
# Valve GG has flow rate=0; tunnels lead to valves FF, HH
# Valve HH has flow rate=22; tunnel leads to valve GG
# Valve II has flow rate=0; tunnels lead to valves AA, JJ
# Valve JJ has flow rate=21; tunnel leads to valve II
def scantunnels()
input(2216).split("\n").map { |line|
vnum, flow, *nexts = line.scan(/[0-9]+|[A-Z][A-Z]+/)
[vnum, { flow: flow.to_i, nexts: nexts.to_set }] }.to_h
end
def valvemoveall(scan, pos, opened, timeleft, followed=false)
# now let the elephant play !!
return valvemoveall(scan, "AA", opened, 26) if timeleft == 0 and followed
# no one to follow
return 0 if timeleft == 0 and not followed
# position already known
@valvecache ||= {}
match = @valvecache.fetch([pos, opened, timeleft, followed], nil)
return match if match
results = []
if scan[pos][:flow] > 0 and not opened.include?(pos) then
results << (
((timeleft - 1) * scan[pos][:flow]) +
valvemoveall(scan, pos, (opened | Set[pos]), timeleft - 1, followed))
end
for succ in scan[pos][:nexts] do
results << valvemoveall(scan, succ, opened, timeleft - 1, followed)
end
@valvecache[[pos, opened, timeleft, followed]] = results.max
end
# 1728 - took 11s
def d22161()
valvemoveall(scantunnels(), "AA", Set[], 30)
end
# 2304 - took 440s!
def d22162()
valvemoveall(scantunnels(), "AA", Set[], 26, true)
end
# TODO : look only at pathes between interesting valves
# ##############################################################################
#
# 2022 DAY 15
#
# ##############################################################################
# ..........#.................
# .........###................
# ....S...#####...............
# .......#######........S.....
# ......#########S............
# .....###########SB..........
# ....#############...........
# ...###############..........
# ..#################.........
# .#########S#######S#........
# ..#################.........
# ...###############..........
# ....B############...........
# ..S..###########............
# ......#########.............
# .......#######..............
# ........#####.S.......S.....
# B........###................
# ..........#SB...............
# ................S..........B
# ....S.......................
# ............................
# ............S......S........
# ............................
# .......................B....
# 6275922
def d22151()
sensors =
input(2215).split("\n")
.map { |line| line.scan(/[-0-9]+/).map(&:to_i).each_slice(2).to_a }.to_h
rownum = 2000000
xbeacons = sensors.values.filter_map{ |(x, y)| x if y == rownum }
sensors
.filter_map{ |(x1, y1), (x2, y2)|
mndst = (x2 - x1).abs + (y2 - y1).abs
delta = mndst - (rownum - y1).abs
((x1 - delta)..(x1 + delta)).to_a if delta >= 0
}.flatten.then { _1 - xbeacons }.uniq.length
end
# 11747175442119
def d22152()
sensors =
input(2215).split("\n")
.map { |line| line.scan(/[-0-9]+/).map(&:to_i).each_slice(2).to_a }
.map { |(x1, y1), (x2, y2)| [[x1, y1], (x2 - x1).abs + (y2 - y1).abs] }
.to_h
maxcrd = 4000000
(0..maxcrd).each{ |row|
ranges =
sensors.filter_map{ |(x, y), md|
delta = md - (row - y).abs
((x - delta)..(x + delta)) if delta >= 0 }
col = 0
while col <= maxcrd do
rng = ranges.find { |r| r.include?(col) }
return (col * 4000000 + row) if not rng
col = rng.end + 1
end
}
end
# ##############################################################################
#
# 2022 DAY 14
#
# ##############################################################################
# 498,4 -> 498,6 -> 496,6
# 503,4 -> 502,4 -> 502,9 -> 494,9
# ......+...
# ..........
# ..........
# ..........
# ....#...##
# ....#...#.
# ..###...#.
# ........#.
# ........#.
# #########.
def showscan(scan, sand, src)
all = (scan + sand + [src])
(xmin, xmax) = all.map(&:first).minmax
(ymin, ymax) = all.map(&:last).minmax
(ymin..ymax).map { |y|
(xmin..xmax).map { |x|
scan.include?([x, y]) ? "#" :
sand.include?([x, y]) ? "o" :
[x, y] == src ? "+" : "."
}.join }.join("\n")
end
def scanrock()
input(2214)
.split("\n").map{ |line|
line.scan(/\d+/).map(&:to_i)
.each_slice(2).each_cons(2).map { |(x1, y1), (x2, y2)|
(xmin, xmax), (ymin, ymax) = [x1, x2].minmax, [y1, y2].minmax
if y1 == y2 then
(xmin..xmax).map { |x| [x, y1] }
elsif x1 == x2 then
(ymin..ymax).map { |y| [x1, y] }
else fail end
} }.flatten(2).uniq
end
def sandfall(all, ymax, srcd)
x, y = srcd
while true
if y == ymax
return [x, y] # void/floor reached
elsif not all.include?([x, y + 1])
x, y = x, y + 1
elsif not all.include?([x - 1, y + 1])
x, y = x - 1, y + 1
elsif not all.include?([x + 1, y + 1])
x, y = x + 1, y + 1
elsif [x, y] == srcd
return [x, y] # source blocked
else