Browse Source

catch up

master
Hazel Levine 1 year ago
parent
commit
61a0710f3c
Signed by: hazel
GPG Key ID: 1884029A28789A62
  1. 748
      data/day13.txt
  2. 18
      data/day14.test.txt
  3. 102
      data/day14.txt
  4. 100
      data/day15.txt
  5. 84
      day12.rkt
  6. 61
      day13.rkt
  7. 62
      day14.rkt
  8. 115
      day15.rkt

748
data/day13.txt

@ -0,0 +1,748 @@
1284,359
47,25
699,764
55,204
552,247
731,877
765,12
619,806
437,864
1202,152
105,456
159,165
648,563
701,143
609,861
972,414
1195,379
1097,439
358,95
1126,288
773,332
863,813
1250,63
1280,95
1237,672
224,343
1222,488
748,842
554,208
1086,215
1136,479
1308,826
371,367
972,722
766,719
80,366
920,22
765,882
1208,354
994,526
512,430
1007,507
773,695
909,672
370,231
38,812
766,495
1078,534
633,408
233,770
251,429
874,315
649,666
1140,868
1138,75
808,441
864,150
1066,814
440,674
477,238
1290,739
786,434
263,42
306,891
1086,652
248,621
515,387
378,730
1255,189
769,187
70,473
408,773
1039,581
373,312
537,562
1078,360
1036,775
925,565
1022,530
132,207
1027,815
869,7
631,281
725,404
945,305
1173,509
678,338
788,207
447,753
166,740
745,199
49,383
1151,729
1255,705
351,507
648,331
1,208
728,299
174,479
224,215
179,372
497,541
445,84
952,95
1136,422
1253,341
251,877
269,150
227,154
597,341
72,343
657,656
900,473
117,562
990,474
1096,326
524,147
1228,473
990,420
360,434
493,47
582,299
843,266
1220,850
370,103
176,26
1192,430
890,252
1235,809
1220,318
666,332
711,481
1083,154
825,2
365,305
566,854
1278,658
213,887
26,7
766,399
448,47
892,549
349,38
605,168
303,42
592,114
433,219
7,560
10,526
227,614
186,836
323,109
32,658
1061,802
544,271
289,589
227,280
713,553
997,574
577,221
502,696
497,801
431,486
42,250
68,866
1290,372
736,773
676,3
1208,663
1004,891
202,775
1116,655
1057,670
840,16
160,320
172,371
480,32
666,441
83,880
100,816
813,821
1265,868
976,420
372,822
1284,7
110,495
348,451
1210,526
467,852
170,474
788,339
390,872
119,703
940,663
577,764
316,646
1,460
1027,79
137,509
565,247
838,395
321,93
763,712
268,332
355,42
217,590
410,473
213,439
433,456
733,221
1099,739
1211,602
221,438
371,92
579,205
207,529
487,813
1124,58
1097,455
213,7
1099,67
1149,847
208,744
633,486
1156,813
239,735
174,78
370,663
438,348
599,690
1061,92
987,337
333,93
1126,291
239,541
1029,760
1283,637
78,273
318,186
441,103
1036,119
1074,793
867,827
75,17
415,541
488,154
644,441
64,91
554,816
976,474
676,43
239,353
271,133
92,842
544,719
1097,887
157,801
1309,131
900,421
731,689
597,822
388,175
599,637
385,296
987,785
57,374
1158,889
167,365
731,205
316,722
830,530
239,159
905,92
663,726
927,581
530,483
107,522
455,889
1267,316
502,866
45,26
842,58
833,238
70,638
803,36
211,515
562,52
20,851
480,332
1148,64
679,281
1248,200
666,562
385,744
1093,864
1260,164
888,364
547,74
373,582
480,479
551,486
1021,589
120,476
467,266
1061,753
1298,644
728,103
544,658
561,44
1149,47
1233,316
1195,827
443,155
124,35
88,488
1203,182
1248,305
266,787
249,589
110,770
1019,341
565,199
863,753
1267,255
549,438
612,616
552,551
1208,231
577,130
883,733
43,578
274,551
766,623
584,642
1078,812
410,80
658,68
627,369
216,236
775,540
850,239
366,530
720,803
1252,338
869,551
830,332
1237,448
274,882
110,627
60,130
698,26
758,23
213,791
92,52
808,812
445,114
50,400
758,540
62,200
1066,473
683,628
1097,103
448,418
1247,68
770,789
816,154
1089,456
817,47
502,562
927,294
1267,764
1275,33
338,172
541,187
383,600
539,155
30,799
749,850
981,847
1077,770
808,5
609,420
354,175
383,742
950,434
535,703
1036,12
415,353
264,332
594,588
184,288
415,735
1086,231
1241,490
961,38
974,156
758,354
591,826
1222,406
574,366
502,620
1211,733
927,742
38,530
864,430
644,696
564,402
577,578
415,93
612,644
955,852
1044,130
864,296
1248,835
763,522
1248,694
1124,836
410,814
622,882
662,563
119,191
769,775
90,318
895,541
579,877
822,154
686,351
349,856
473,133
390,22
90,850
418,148
666,696
1136,368
701,644
510,843
1099,379
733,764
825,450
427,733
730,376
883,280
877,456
867,67
217,752
830,252
146,147
1034,46
714,338
619,444
771,620
724,866
403,429
57,822
27,369
1039,152
1129,133
1195,739
959,507
207,753
764,294
564,598
837,740
1257,304
661,271
1208,652
160,574
1083,609
1029,565
274,119
609,644
443,827
1011,42
211,739
580,518
895,801
401,672
815,165
502,396
624,351
445,536
842,338
766,658
1123,266
401,670
113,119
1036,460
85,589
661,623
60,175
249,802
1097,791
45,537
224,802
89,647
236,793
726,256
211,67
80,528
1283,705
537,332
338,816
385,150
221,456
227,609
400,299
248,173
502,498
649,623
1303,560
592,332
316,248
1086,92
117,114
622,658
136,63
748,52
907,465
1230,773
579,689
274,460
107,182
1077,124
1237,446
355,852
174,422
691,603
1283,369
653,656
109,640
433,675
873,30
1225,589
698,616
522,207
540,789
488,546
1186,343
102,231
169,373
354,719
907,429
217,864
249,354
713,822
1059,429
1293,56
488,98
1208,204
60,63
637,56
448,847
1236,152
1161,880
1267,130
545,12
584,252
1191,533
919,516
268,338
410,30
274,12
418,549
634,43
1044,175
544,399
383,294
1193,88
212,553
527,677
224,679
937,312
1066,25
427,280
1300,780
895,735
254,595
706,282
441,392
813,73
700,72
415,159
1171,628
644,889
900,80
209,114
837,133
537,695
716,271
822,740
1143,365
599,413
115,379
1158,476
769,119
373,196
60,287
1022,364
877,219
1265,250
1,763
910,595
937,751
35,33
1066,421
26,359
825,521
1033,231
880,802
49,831
830,642
75,809
80,773
1178,351
142,464
1300,114
2,68
70,256
354,215
813,801
867,739
266,287
480,562
371,815
1241,852
539,620
524,434
1111,5
85,305
174,32
800,395
1046,562
1134,418
1136,78
93,850
107,372
416,152
925,329
194,787
1250,130
705,511
657,238
1225,876
274,775
1129,761
574,773
1242,28
937,143
32,236
758,551
564,850
174,368
1220,150
1101,114
843,8
594,306
545,814
118,38
160,740
1241,42
378,220
994,646
698,278
73,446
552,343
925,296
174,498
1089,438
855,668
609,117
544,627
1265,26
1099,515
100,368
611,130
23,556
1056,708
1250,831
570,497
544,684
682,887
1027,527
43,130
731,533
1200,627
248,684
62,305
740,497
1092,315
1240,638
1086,343
701,582
989,93
830,396
124,411
186,338
1071,353
1158,722
284,663
758,343
152,190
7,334
266,607
706,494
691,88
1261,63
766,175
610,72
373,329
1263,25
174,480
666,364
446,744
1211,161
581,222
790,600
93,178
1141,373
120,215
93,322
597,789
744,854
1010,311
761,149
1019,441
224,231
900,30
472,499
706,730
480,172
688,658
371,540
174,396
825,444
830,172
fold along x=655
fold along y=447
fold along x=327
fold along y=223
fold along x=163
fold along y=111
fold along x=81
fold along y=55
fold along x=40
fold along y=27
fold along y=13
fold along y=6

18
data/day14.test.txt

@ -0,0 +1,18 @@
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

102
data/day14.txt

@ -0,0 +1,102 @@
OFSVVSFOCBNONHKFHNPK
HN -> C
VB -> K
PF -> C
BO -> F
PB -> F
OH -> H
OB -> N
PN -> O
KO -> V
CK -> V
FP -> H
PC -> V
PP -> N
FN -> N
CC -> F
FC -> N
BP -> N
SH -> F
NS -> V
KK -> B
HS -> C
NV -> N
FO -> B
VO -> S
KN -> F
SC -> V
NB -> H
CH -> B
SF -> V
NP -> V
FB -> P
CV -> B
PO -> P
SV -> P
OO -> V
PS -> C
CO -> N
SP -> B
KP -> H
KH -> S
KS -> S
NH -> K
SS -> P
PV -> P
KV -> V
ON -> N
BS -> C
HP -> K
SB -> P
VC -> B
HB -> N
FS -> V
VP -> K
BB -> N
FK -> S
CS -> P
SO -> F
HF -> F
VV -> C
BC -> S
SN -> K
KB -> H
BN -> H
HO -> S
KC -> F
CP -> S
HC -> S
OS -> K
NK -> N
BF -> S
VN -> B
SK -> K
HV -> B
KF -> H
FV -> B
VF -> H
BH -> S
NN -> O
HH -> K
CN -> H
PH -> V
NF -> S
OV -> P
OC -> V
OK -> H
OF -> H
HK -> N
FH -> P
BK -> N
VS -> H
NO -> V
VK -> K
CF -> N
CB -> N
NC -> K
PK -> B
VH -> F
FF -> C
BV -> P
OP -> K

100
data/day15.txt

@ -0,0 +1,100 @@
9899623167955799382567812944379969888862225189992795696599917679656768699999959517619965244969693964
9299799754769995528964879691987994394279962991817261288292875969792662487997119818181869584684876846
7797846998192515817979298977311989795237984959677986552466183891499117966741289581161899999893535995
2746871998587973835699889998392494767471869998599394899949381694296896989189962932964957933899542279
9758993391987992656989818498486988599883838318197866615597799585988981991747229849497999993243298395
9399859277754624991914794999879918389948983892536995414838519589749997992375963822994963898129816891
4988761796994698798992294593199869969449299299888928699598878616594466846698991331991384879288286649
6681812758921325927688654155918195422521578475935721834774323989692982969497917655999285958895411188
9294959984991949829739979782757486496875798879567285829265899568198989661156887948394893429849894344
1989218789489289118884889794782687999839837887113234849779927951925453711933428829589699775283299939
7113936997884496159763714189899331495417669273479968229199411999655789958743868829834719469784979991
9937892998524945938896699224293782778973971162817489767755178489985773997289738882981958789689969738
9893667719871253431939794519776593881899999984999962999886168599984689197789256958928814719171836987
7696995999998819843579887897889999959818767599838619996753857949223958869799888996941198418198283783
1289497758397697991165997178769891934583779487914994292296484372375549278795469999576912852999577146
2145499618574685969944916191819694176889517862891987992286738596322515719885117867136329394989169918
7838949788839218366923347957991884889313781679758899928466899961477194699875195737897469919119172964
8969814999876973195999999664518987675889217897686288588198473581989799964747919586577197883617842912
3646447111298979939618644396846388979979632986751561976254775749577761761684199897989861979978818249
3693987887785199696529192391881158366749834784416121923891965298714355149487439797789695651691994849
4399691971845787891217793292838938799899841989958969696922899969166555789939469791978193767969589599
1597452938979863187994895272216937677557565817137799589361998819892494688832787778742593847599654928
4998869993788952899573655981676899457612878235987545139618869168599744167461768978559866999931887219
9918547918588952892628699998876576788789978665681743292193488577137979718219286915399767218557788869
9998367769929846173918588599919843778167888739193129964797338846491478969759283689171367669868696599
9829571337865973928146726168773595121993396595989823959918677438974595858859326775998195658642797956
5793777994687577296639276199298961337939999276974564922373996999998896861236943745399984915955692759
8789948788688489312693916698488579198763994399199437915419489358888884961886351789858973958786889999
5879997993698997672824288898985156878882726578969987158868171318286218527399415884599865885525859388
8379442598616924549819587897983867986988889799152599316616759976993492798923293246979593289985791628
3968859989982898979586988795551294599557648419698923978899722987994894199989792982774967868858646279
9829239289228887898997945462929342499998978292194388196249998889225899862864989191819638899998777615
9967999499299697596478854784557388578189997996598585389567638994254119539615798295959637616748119532
7847779999158579987835924358969787977597752337899972679968795498957999379878979811614915695186989695
8787986185978797993569967519247299584691389899837387899257389889997699847936381982679869948118949973
8892258536995523797193975239126969921916799491982514589138849539992988167819349194725937978282692648
1878811997841889827197299198979379979548789491823826898798938518762468896781974994769838768796981188
6687799987379984289919668178999996996259698999986889991848599999617587193985177478619758877988414119
9796589979989554195357786964468849181957799166899932768913891979449599511641935582792967896999669568
9997594681917699997971899851259914198996778488197217597149189838338395538993679994299957334548182789
9796766148448919429994826289895989965276959822999699996172999933998459348899957977144691995889895635
9933948715818236524533482296321646865771299998466692471768748749714113978548698174997268924649799377
1115792324352764449879949619583961556985945166299981779898828999849818899727394899987459918996791614
9999526461777598464699652921794786889797998584659354994685156891439971996969999759889814757629997878
6911834446491822919437989977369664983425959695491256751688945588669787213118199192181885639885788986
7193244156988889749827796999989172687638671791497992997829991156878759879964242959949842255399978886
8298127588799726879818241977767996679143687999194453896536629967662999355465118498999587781693887799
6159947745481178284839686663914258897928952293468359997988879622844586789396881149175166787919571489
7991294191117923999487293655288588353955581996927271977626982898577919799999782836662238781847999946
9999599756999399989749995889585696786827989887519995188912372378767849516399824963999418698929977812
5486736694189936967838185919999718893983154998987189478918393217519949289989328318899858999328984293
8949995919564717698297691783566233929798588899688918378766963916729833899587746499989898586989499812
3524193826987847299996184999799677595943978496911914269385247587789997827759828889367399941458756212
9696539817993691789966498932397742679824297799995798526139889399952988489928897934487195818678859583
2599943991191886966399997328964855991777927342867919884377589116899789189839698983799277993881888486
2817973579994999182718675472898896996529319459887399672919562861299341996661878975837749458982915958
3656841349489982595888934776772918197948998751789698339968959593759899927872797969479296179583462987
9157743798478765988169591329911428979929841499826518846685978789116659589136914999191949998749889195
5891119177956587927342315968196825692999686655731452887488589969889877497991875774999261196439919378
8971296237126599216899243157987983864699752899669959868888389188533951967961279963997198991967893968
9331713863971946599958118713939498189384492897577739798867798995264539518793295279888619399895643957
7299934981419753716499426161576357197977647997936887657763137838872996757748991898428989999246799711
2927598992939912676749895911999919121999689897797689859793677699896948178757849758699468979256775936
4996298679798776526899726187198594777667938181452251897585766569856979161831949999359379795567464978
6749697994899278119299119998411491197271389997591477879181293778862959568794255919916431991587389199
7917599295856995448398478815799177895955994437998988469939759999482637996873958891797158899568959974
3971493629788179694678156991899297599876983796147322799885496923998788465997492876993998775555847491
7589697756587167589199199931779484295478977888887947996911879683944986888123519869913756279999718376
9681891297599815159559839675522495189461498893954599819393873379999979518672477477171896578897588233
7218994464751199958895319996657748594987395382849437679243645291991819981869236327979599689979494557
3669579587841491999981887963937888932999949969968697361799957867798969849977367993858899197339573594
9938999157929999391948399198949897746612998265971677274753997966219884369519491683899788999296718841
2792979659919192951318997999298763787689864349785448519148893482199639421688567998269185598719848999
2956993919948418575296753192419975619989681488774889855393285898997153735745938167798389785972737993
8277731747919861157877398119967988787181989184148964479699868665199169958899862971952346887939119495
7957189873684459991539999918145326838946811314819664298566584999664824932481229968691232955488996359
5689899439928318983291862946713578981598998372769991897899964571991679961789499851669889897998923876
3992921155599616339882973837995998618999992927962947899864889685918698869875995491967919874765214249
5928994589588896418678769788982982467546658264292739286852797455119997956825919952139759634939363929
6466919491711156749275961775919918642899696899138799477759812969836989931761564516794132259978999975
6818715183889871995339229997318691989488799797672239864373984231579812138977183167796579991289844199
8299834651294894848996986998229879799792129998996896572993779468116894286897914723111995996593932918
9898961999279872995195669299467897939166178995288998948411783889832837489765636216821255797239972994
9294128842689878939853174458439783985991881697485769291768896997918381963397479848198981137733977259
9938693875599496191998786643916999989789656293298174199414987899951694925461891182769753418339819588
9299771958199974769247739386371956958922337417899967982645986299869439179889938853297748885816258959
5789218997367742787939849896229539979929943216698841986289892459919159297757929647191726176799837458
9997998899972949122469892269164697788597419659972681773616787817194787989924999971988949166393587685
8697819586862976298894966154799259953999979498492485569382199995858671376959237316117353968898987789
5833788449272889999938592989788917857997366887287599569391116987816957492646638194951369148466995865
8797392875679992576397998611946721199191997929616189898185668118656285951889998279779591569993782497
1243998587194789478969895899976929999998981784112911287566869897178888467393969798949922993999722783
6898491932163517498563145499993597998279478596617759269779837313979394939989887994797759687495749698
5918852999916934988579946896318848447798929874928938578765844728128792997498716499896334799391962229
1445281593898246791614118399847688799965474888492979999898593475998999989517878579949888291967993779
8819479319669561859977789779885299979994739846376789967696231939168985397398886199361944119969942269
8384597187994673998976978685877959945984938992895779118788869382587282961593144198939311957191999898
6891958879895544489956999491779892727268265764998397998885889467849955382531433982795569362982971997
4719927539178224292254897986253889381997819878927876771217358792678752779988195867984854987879869761
5496179226499867986599862998778132568199955188683452991319684698887391958959379992599899599281999798

84
day12.rkt

@ -1,75 +1,37 @@
#lang racket
(require "lib/common.rkt"
fancy-app
graph)
(define (string-upcase? str)
(for/and ([ch (in-string str)])
(define (big-cave? vtx)
(for/and ([ch (in-string vtx)])
(char-upper-case? ch)))
(define (day12a lines)
(define G
(undirected-graph
(for/list ([str (in-list lines)])
(string-split str "-"))))
(define-vertex-property G size)
(for ([vertex (in-vertices G)])
(size-set!
vertex
(cond [(string=? vertex "start") 'start]
[(string=? vertex "end") 'end]
[(string-upcase? vertex) 'big]
[else 'small])))
(define (parse lines)
(undirected-graph (map (string-split _ "-") lines)))
(define (run-dfs vertex [seen (set "start")])
(cond [(string=? vertex "end") 1]
[else
(for/sum ([adj (in-neighbors G vertex)]
#:when (not (set-member? seen adj)))
(run-dfs adj
(if (equal? (size adj) 'small)
(set-add seen adj)
seen)))]))
(define (walk G did-twice? [vertex "start"] [seen (set "start")])
(for*/sum ([adj (in-neighbors G vertex)]
[big? (in-value (big-cave? adj))]
[seen? (in-value (set-member? seen adj))]
#:when (or big? (not seen?) (not did-twice?))
#:unless (equal? adj "start"))
(cond [(string=? adj "end") 1]
[else (walk G
(or did-twice? (and (not big?) seen?))
adj
(set-add seen adj))])))
(run-dfs "start"))
(define (day12a G)
(walk G #t))
(define (day12b lines)
(define G
(undirected-graph
(for/list ([str (in-list lines)])
(string-split str "-"))))
(define-vertex-property G size)
(for ([vertex (in-vertices G)])
(size-set!
vertex
(cond [(string=? vertex "start") 'start]
[(string=? vertex "end") 'end]
[(string-upcase? vertex) 'big]
[else 'small])))
(define (run-dfs vertex [seen (set "start")] [special #f])
(cond [(string=? vertex "end") 1]
[else
(for/sum ([adj (in-neighbors G vertex)]
#:when (not (set-member? seen adj)))
(define reg
(run-dfs adj
(if (equal? (size adj) 'small)
(set-add seen adj)
seen)
special))
(define (spc)
(run-dfs adj seen #t))
(if (and (not special)
(equal? (size adj) 'small))
(+ (spc) reg)
reg))]))
(run-dfs "start"))
(define (day12b G)
(walk G #f))
(module+ main
(call-with-input-file "data/day12.txt"
(λ (prt)
(define lines (port->lines prt))
(define lines (parse (port->lines prt)))
(answer 12 1 (day12a lines))
(answer 12 2 (day12b lines)))))
@ -86,5 +48,5 @@ A-end
b-end
EOD
)
(check-equal? (day12a (string-split lines)) 10)
(check-equal? (day12b (string-split lines)) 36))
(check-equal? (day12a (parse (string-split lines))) 10)
(check-equal? (day12b (parse (string-split lines))) 36))

61
day13.rkt

@ -0,0 +1,61 @@
#lang racket
(require "lib/common.rkt")
(struct posn (x y) #:transparent)
(struct fold (axis n) #:transparent)
(define (parse input)
(match-define `(,paper ,instrs) (string-split input "\n\n"))
(values (for/set ([p (in-list (string-split paper))])
(match-define (pregexp #px"([0-9]+),([0-9]+)"
(list _
(app string->number x)
(app string->number y)))
p)
(posn x y))
(for/list ([instr (in-list (string-split instrs "\n"))])
(match-define (pregexp #px"fold along (x|y)=([0-9]+)"
(list _
(app string->symbol axis)
(app string->number n)))
instr)
(fold axis n))))
(define (fold-along paper instr)
(for/set ([p (in-set paper)])
(match-define (posn x y) p)
(match instr
[(fold 'x n) #:when (> x n) (posn (- (* 2 n) x) y)]
[(fold 'y n) #:when (> y n) (posn x (- (* 2 n) y))]
[_ p])))
(define (day13a paper instrs)
(set-count (fold-along paper (first instrs))))
(define (max-on-axis paper axis)
(for/max ([pt (in-set paper)])
(match axis
['x (posn-x pt)]
['y (posn-y pt)])))
(define (print-paper paper)
(for ([y (in-inclusive-range 0 (max-on-axis paper 'y))])
(displayln
(apply string
(for/list ([x (in-inclusive-range 0 (max-on-axis paper 'x))])
(cond [(set-member? paper (posn x y)) #\█]
[else #\space]))))))
(define (day13b paper instrs)
(print-paper
(for/fold ([cur-paper paper])
([instr (in-list instrs)])
(fold-along cur-paper instr))))
(module+ main
(call-with-input-file "data/day13.txt"
(λ (prt)
(define-values (paper instrs) (parse (port->string prt)))
(answer 13 1 (day13a paper instrs))
(displayln "answer 13.2:")
(day13b paper instrs))))

62
day14.rkt

@ -0,0 +1,62 @@
#lang racket
(require "lib/common.rkt"
fancy-app
threading)
(define (parse input)
(match-define `(,poly-str ,rules) (string-split input "\n\n"))
(values (for/fold ([hsh (hash)])
([tortoise (in-string poly-str)]
[hare (in-list (rest (string->list poly-str)))])
(hash-update hsh (cons (string tortoise) (string hare)) add1 0))
(for/hash ([rule (in-list (string-split rules "\n"))])
(match-define (pregexp "(.)(.) -> (.)" (list _ ch1 ch2 ch3)) rule)
(values (cons ch1 ch2) ch3))))
(define (step poly rules)
(for/fold ([hsh (hash)])
([(pair num) (in-hash poly)])
(match-define (cons l r) pair)
(define i (hash-ref rules pair))
(~> hsh
(hash-update (cons l i) (+ _ num) 0)
(hash-update (cons i r) (+ _ num) 0))))
(define (score poly)
(define freqs
(for/fold ([hsh (hash)])
([(pair num) (in-hash poly)])
(match-define (cons l r) pair)
(~> hsh
(hash-update l (+ _ num) 0)
(hash-update r (+ _ num) 0))))
(floor
(- (/ (apply max (hash-values freqs)) 2)
(/ (apply min (hash-values freqs)) 2))))
(define (day14a poly rules)
(score
(for/fold ([cur-poly poly])
([_ (in-range 10)])
(step cur-poly rules))))
(define (day14b poly rules)
(score
(for/fold ([cur-poly poly])
([_ (in-range 40)])
(step cur-poly rules))))
(module+ main
(call-with-input-file "data/day14.txt"
(λ (prt)
(define-values (poly rules) (parse (port->string prt)))
#;(answer 14 1 (day14a poly rules))
(answer 14 2 (day14b poly rules)))))
(module+ test
(require rackunit)
(call-with-input-file "data/day14.test.txt"
(λ (prt)
(define-values (poly rules) (parse (port->string prt)))
(check-equal? (day14a poly rules) 1588))))

115
day15.rkt

@ -0,0 +1,115 @@
#lang racket
(require "lib/common.rkt"
fancy-app
graph
threading)
(struct posn (x y) #:transparent)
(define (posn+ p1 p2)
(posn (+ (posn-x p1) (posn-x p2))
(+ (posn-y p1) (posn-y p2))))
(define (1norm x y)
(+ (abs x) (abs y)))
(define ((repeat fn n) val)
(for/fold ([acc val])
([_ (in-range n)])
(fn acc)))
(define (explode str)
(map string (string->list str)))
(define (parse lines)
(~>> lines
(map explode)
(map (λ~>> (map string->number)
(apply vector)))
(apply vector)))
(define (board-dim board)
(cons (vector-length (vector-ref board 0))
(vector-length board)))
(define (board-ref board x y)
(vector-ref (vector-ref board y) x))
(define (adjacents board p)
(match-define (cons m n) (board-dim board))
(define (valid? p)
(match-define (posn x y) p)
(not (or (< x 0) (< y 0) (>= x m) (>= y n))))
(for/list ([path (in-list (list (posn 0 1) (posn 1 0) (posn 0 -1) (posn -1 0)))]
#:when (valid? (posn+ path p)))
(posn+ path p)))
(define (generate-graph board)
(match-define (cons m n) (board-dim board))
(define edges
(for*/list ([i (in-range 0 m)]
[j (in-range 0 n)]
[adj (in-list (adjacents board (posn i j)))])
(list (board-ref board (posn-x adj) (posn-y adj))
(posn i j) adj)))
(weighted-graph/directed edges))
(define (day15a board)
(match-define (cons m n) (board-dim board))
(define G (generate-graph board))
(define-values (distances _) (dijkstra G (posn 0 0)))
(hash-ref distances (posn (sub1 m) (sub1 n))))
(define (tesselate starting-board)
(match-define (cons m n) (board-dim starting-board))
(define (increment board)
;; annoyingly not modulo
(vector-map (vector-map (λ (x) (if (= (add1 x) 10) 1 (add1 x))) _) board))
(define boards
(for*/vector ([i (in-range 5)]
[j (in-range 5)])
((repeat increment (1norm i j)) starting-board)))
(define (bboard-ref vec i j)
(~> vec
(vector-ref (+ (floor (/ j n)) (* 5 (floor (/ i m)))))
(board-ref (modulo i m) (modulo j n))))
(for/vector ([j (in-range (* 5 n))])
(for/vector ([i (in-range (* 5 m))])
(bboard-ref boards i j))))
(define (day15b board)
(day15a (tesselate board)))
(module+ main
(call-with-input-file "data/day15.txt"
(λ (prt)
(define board (parse (port->lines prt)))
(answer 15 1 (time (day15a board)))
(answer 15 2 (time (day15b board))))))
(module+ test
(require rackunit)
(define test-input #<<EOD
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
EOD
)
(define board (parse (string-split test-input)))
(check-equal? (day15a board) 40)
(check-equal? (day15b board) 315))
Loading…
Cancel
Save