-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.lisp
1127 lines (1113 loc) · 13.4 KB
/
day07.lisp
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
(in-package #:advent-of-code-2023.day07)
(defun parse-data (data)
"Parse input DATA into a convenient list of hand-bid pairs where hands
are represented as 5-character strings."
(let (pairs)
(ppcre:do-register-groups (cards bid) ("([A-Z0-9]+) ([0-9]+)" data pairs)
(push (cons cards (parse-integer bid)) pairs))))
(defvar *card-hierarchy-1*
(loop
for i from 0
for c across "23456789TJQKA"
collect (cons c i))
"Alist of relative card importance for the first puzzle")
(defvar *card-hierarchy-2*
(loop
for i from 0
for c across "J23456789TQKA"
collect (cons c i))
"Alist of relative card importance for the second puzzle")
(defvar *hand-hierarchy*
(loop
for i from 0
for h in '(:high-card :one-pair :two-pair :three-of-a-kind :full-house :four-of-a-kind :five-of-a-kind)
collect (cons h i))
"Alist of relative hand-type importance")
(defun classify-hand (hand)
"Classify HAND as one of the types based on character occurrence analysis."
(loop
with h = (make-hash-table)
for c across hand
do (incf (gethash c h 0))
finally (let ((hh (sort (alexandria:hash-table-alist h) #'> :key #'cdr)))
(return (values
(cond
((= 1 (length hh))
:five-of-a-kind)
((and (= 2 (length hh))
(= 4 (cdar hh)))
:four-of-a-kind)
((and (= 2 (length hh))
(= 3 (cdar hh)))
:full-house)
((and (= 3 (length hh))
(= 3 (cdar hh)))
:three-of-a-kind)
((and (= 3 (length hh))
(= 2 (cdar hh)))
:two-pair)
((and (= 4 (length hh))
(= 2 (cdar hh)))
:one-pair)
(t :high-card))
hh)))))
(defun classify-hand-with-jokers (hand)
"Classify HAND as the highest possible type, given its jokers. If HAND
has no jokers, use ordinary CLASSIFY-HAND. Otherwise, iterate through
all possible substitutes and pick the highest classification."
(if (find #\J hand)
(loop
for (c . _) in (rest *card-hierarchy-2*)
for candidate = (classify-hand (substitute c #\J hand))
collect (cons candidate (cdr (assoc candidate *hand-hierarchy*))) into hands
finally (return (car (first (sort hands #'> :key #'cdr)))))
(classify-hand hand)))
(defun hand< (h1 h2 &key (classifier #'classify-hand)
(card-hierarchy *card-hierarchy-1*))
"Compare whether hand H1 represents a lower rank than H2. First,
compare their classifications. If there is a tie, compare hands on a
card-by-card basis, going in order. Use :CLASSIFIER to provide custom
classifier function. Use :CARD-HIERARCHY to specify which card
hierarchy to use for comparisons."
(let* ((h1-type (funcall classifier h1))
(h1-hier (cdr (assoc h1-type *hand-hierarchy*)))
(h2-type (funcall classifier h2))
(h2-hier (cdr (assoc h2-type *hand-hierarchy*))))
(cond
((< h1-hier h2-hier) t)
((> h1-hier h2-hier) nil)
(t (loop
for c1 across h1
for c2 across h2
if (< (cdr (assoc c1 card-hierarchy))
(cdr (assoc c2 card-hierarchy)))
return t
if (> (cdr (assoc c1 card-hierarchy))
(cdr (assoc c2 card-hierarchy)))
return nil
finally (return t))))))
(defun puzzle-1 (&key (input *example-input-1*))
(loop
for rank from 1
for (hand . bet) in (sort (parse-data input) #'hand< :key #'car)
sum (* rank bet)))
(defun puzzle-2 (&key (input *example-input-2*))
(loop
for rank from 1
for (hand . bet) in (sort (parse-data input)
(alexandria:rcurry #'hand<
:classifier #'classify-hand-with-jokers
:card-hierarchy *card-hierarchy-2*)
:key #'car)
sum (* rank bet)))
;;;; Data
(defvar *example-input-1*
"32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483")
(defvar *input-1*
"AATKJ 840
27A83 251
6TT8T 113
Q6T6T 693
99K89 553
777Q7 136
7227J 782
TJ2J9 837
55578 524
24Q49 919
AAA2A 709
KT972 907
85282 886
4432J 255
K6K27 119
J9Q77 820
33K3K 48
T8887 500
28272 77
38Q46 287
KQQAQ 481
5T2T3 280
KQQQQ 458
K82Q3 712
Q5552 758
4K87K 174
7QQJQ 331
29233 96
79KA3 65
J9893 546
Q5A39 842
K4K7K 437
T555T 215
9A7AA 162
32AA2 374
8J222 134
38376 583
J5222 962
J65A9 883
36338 956
Q654T 107
K4KKK 169
A9A76 710
9KQ78 183
88753 577
K997J 539
4QA88 914
K5T3A 401
8K77K 619
8888J 400
TATAT 58
8T436 279
3Q28A 659
3KK4J 54
QQJ59 903
44777 779
53336 477
22J33 930
TQ6T2 923
475TK 633
5KJ45 980
47TK6 50
K8KKK 453
2TJKA 39
TJ6K6 380
4J888 311
27277 317
2A2A5 213
TT7T3 365
22555 858
789QQ 421
937KT 791
89KT6 732
3KKKJ 703
A2T22 896
6467J 489
72878 863
46J5J 283
TATA6 71
Q4967 941
7JT55 232
9TQQQ 785
55445 835
K68KK 256
TT33T 72
2A2AQ 435
9643K 436
J3282 95
88TQQ 767
Q8788 250
QQ949 62
4T552 448
JJ8QQ 476
2JT22 900
73T7T 648
QQQJJ 795
98A35 589
94444 479
44486 465
7TK86 521
2JK45 761
556KT 492
94K99 144
2QJ32 637
66226 538
933TA 248
T2234 419
QAA26 35
K63K3 853
5KJK9 970
KK4QK 636
8TTTT 69
5T7J8 161
239A4 91
3T823 182
24A8K 407
K5A92 330
KK9K3 599
2244J 284
AAJ5Q 794
44A84 472
KK2J3 597
6QQJ6 547
TAAT4 668
J4444 827
QAJ22 200
KT5J5 80
7K3J9 517
3A33T 762
TT66T 154
6K972 569
6378Q 593
2AQA9 460
2KKAA 516
AK45A 978
QAAA8 568
83478 234
33737 491
Q9Q43 977
QKQQ7 523
9T24A 609
88997 355
KAKAA 651
87Q77 665
JK7A4 268
68586 44
4345J 931
Q29A9 871
3479K 774
3TK65 189
9249Q 319
27J2Q 656
333J9 667
6TJ66 681
82T96 738
K3K39 14
74JK9 297
8A88A 798
AT869 987
9TAT9 996
33939 684
4424J 117
44AJA 422
646AA 358
K3T83 218
KK7K7 495
A6A6J 731
3QQ97 381
43ATK 484
K9965 982
9J9JJ 392
Q2QQQ 420
58588 433
9Q955 862
88777 246
669J7 438
9AA2A 733
8A9K6 175
Q25JK 459
JJ2K8 439
T8K9T 195
45444 719
A22A2 403
6366Q 36
35366 452
293J3 576
JKK3J 402
425AT 338
344J4 975
7K7A8 490
7QQ7Q 242
T9T9K 542
2JJT6 440
76676 541
AKA66 361
7557Q 431
5K552 511
997K7 179
79779 885
85585 961
TQQQ8 751
9TAT6 504
84248 654
222JJ 865
6AKK8 573
AQ3T8 30
77676 670
T44J2 808
J7J5Q 897
44343 57
TJ7T5 945
JKTTK 60
3333Q 894
6886J 473
8J7J3 990
77797 915
77J98 155
KKQT8 736
8A8AJ 219
AQQJA 75
75J58 349
78978 898
48TAA 726
56A6A 45
888JJ 170
Q7KK3 294
QQA7A 199
A4653 193
44545 602
622K7 612
T5TJ9 860
8T888 548
8TQJ4 410
TQ578 446
98988 367
5989T 151
QQ35J 679
73677 416
J33Q5 137
6564J 913
4847Q 807
39292 874
JQA45 810
97975 943
Q9839 296
9599T 661
J9699 723
44J72 875
T29A9 889
QQ53K 1000
88988 20
QJQK5 769
KAKKK 132
7327J 177
QQQAQ 704
2KTK6 257
474A4 928
9A748 501
5A535 469
2T426 190
53353 318
Q7Q78 694
92738 796
25995 772
QQQ22 877
9K999 238
JJQ5A 424
4QA4J 7
KQT2A 347
27787 754
823T5 159
54347 702
ATA55 166
9Q8T7 887
K8338 631
945AJ 658
TJTTJ 16
7A478 313
93998 2
A5A93 342
KKQQK 921
3K242 52
9AAJT 43
TT33Q 337
22452 139
2AQK8 594
6AQ58 503
KJ5JQ 122
9Q787 988
7626T 228
J99J9 377
6J727 378
45J33 216
A555K 360
6J62K 959
Q7Q47 141
95644 116
5K755 56
7A7QQ 773
27K92 663
A4A77 841
763J4 872
3493J 478
42K9K 103
43433 973
K6A4K 38
T257Q 307
2KQKK 992
TT553 627
66664 508
88A82 243
QJQQQ 804
2T99T 150
2JQT3 245
44644 786
675TK 194
45KKA 1
JJJJJ 543
T4TTT 530
K6KK6 336
TT8Q3 888
T999Q 350
36643 176
95KKK 832
J7386 375
K8KK8 954
82JK3 411
AAAAT 571
3677J 854
9998J 604
5JQT4 94
JAKKQ 635
TT986 15
5855Q 756
243Q8 127
T69A3 821
63A7T 902
QQ8Q8 805
AQAAK 371
83J33 981
AQ525 549
72486 147
36664 715
QQTTQ 312
686A9 920
J63J7 486
4AAAQ 451
Q5452 84
66688 191
3QAQ9 285
925K6 66
824Q8 5
JTQQT 806
J5J33 344
A2A35 428
72943 262
K9966 581
66J69 991
788J7 728
AA7AA 471
5555K 167
J4A62 488
JJ699 258
36386 600
A4AA4 405
53Q9T 211
3377A 985
K7Q83 404
J3TT7 265
Q9QQQ 482
82736 273
92K4A 771
68888 417
A7AA7 625
29333 630
558QK 536
7J9J7 247
66T66 586
AKA9A 11
73J3J 67
Q6226 570
885J5 457
7447J 869
TA2A2 688
J9685 254
4JKK4 275
8QQQ7 442
82383 244
43333 19
573JT 701
A666A 777
999J9 984
JJ444 822
KJ3A4 505
22494 387
TJT77 301
K2K2K 112
88338 844
5AA5A 388
33J63 233
9658Q 432
797K7 291
AJ3TT 935
K2255 483
66966 286
33555 101
AQ4QQ 944
77K77 575
4T4QQ 650
32J6T 948
AK32Q 666
57455 685
9823A 708
8TTTK 474
22575 102
77577 368
88J28 90
A4343 610
24826 82
QQ8QQ 158
99QQ9 892
77468 99
3T773 4
4QQ3Q 620
85437 212
53655 745
Q6664 156
7447Q 320
673K5 502
4TJ7Q 917
4TJ44 281
53545 186
TQ54A 957
8J242 818
4J75A 938
95T8A 499
KJ6KK 657
T5TTT 12
AAA4A 878
6Q66K 788
7TT22 450
882QA 880
5KT99 397
3955T 263
J68Q8 939
6T395 742
3KQ69 345
Q9333 425
J7777 695
722Q7 130
T7TT6 775
TTT88 78
AQQ22 815
J8837 616
223Q3 271
4KKK4 28
95999 879
ATTT6 34
4J4JA 603
Q5972 37
798K6 963
29J4Q 217
2AAA3 830
99J9T 893
5J2K2 235
TTT55 937
9587T 906
4T3JK 968
K88K8 734
JT223 801
9QJ2Q 418
K8866 528
887AA 487
6733J 426
82887 17
54TQK 643
44J94 639
58K6A 470
3TTTT 223
T4TT4 760
82435 942
K494Q 114
77792 556
5J82J 118
2T8AA 559
9494A 706
J4666 295
33J7K 749
8JQ88 717
TJTTT 672
4AAKK 557
Q77QJ 629
KKJK5 535
492JT 834
9QK74 876
7QQQQ 308
622J2 946
J489Q 160
A2A2A 647
845T2 362
Q7TT4 104
T7KQA 513
8T8T8 891
25J82 6
3333J 53
J4544 443
3377Q 551
KKTQQ 655
22822 59
Q9Q9Q 278
94JTK 363
66699 206
7A499 882
T3663 677
4K44Q 21
82A42 197
4824Q 566
T8TT4 624
KQJ7Q 951
TJT89 163
65A5A 884
3T63A 92
A3A34 434
T7J88 202
4K9A3 969
52333 814
J3977 615
86666 697
8A987 866
5434K 464
75888 974
4444K 115
62K32 143
5J5TT 140
T4QTQ 324
K2282 314
39993 518
2AJ4J 83
K96K9 952
Q683J 851
JJ4J8 724
2222T 611
7J676 595
3T333 540
88848 3
2Q679 463
J68J6 97
AAA5A 276
22272 288
3353J 171
KK67J 207
34QJ7 617
TTAKK 714
AA6JA 529
A8299 373
5Q222 79
666J6 953
KQ29Q 799
533TT 735
JQAQQ 901
995T3 716
K2AK2 531
7776Q 68
94Q4Q 87
AAA88 999
T2434 149
TQTQ7 385
255T5 302
99T99 449
798TA 496
7T6Q8 746
Q2QKQ 873
75776 300
28588 303
5KT5A 922
KJT64 409
AJAAA 924
595JA 867
44J93 27
2K384 690
JKKKK 744
K57KQ 124
T4464 109
29AKJ 705
8JK8K 261
K6T3J 947
T5588 125
6A8K4 316
35555 305
63AAJ 560
66J67 578
KQ77K 646
T654J 33
38AKK 230
AJ99A 838
T6TTK 252
6666K 689
99A33 965
88QAJ 764
T9Q23 899
8A22T 829
478Q9 366
TT2TT 971
34T2A 259
AAQAJ 23
J4T52 776
9999A 277
68896 994
55585 18
88999 131
77389 514
75AQJ 787
88Q88 51
3J4JQ 678
4J643 394
6634J 757
74496 249
555J5 226
T3TT4 881
93739 157
377K8 328
JJ6J3 81
8JA9K 720
T3TT5 828
33A33 106
K6KKK 413
86868 895
6J3QQ 323
4JQ93 192
939J9 729
7Q77Q 671
Q2222 676
8A83Q 823
TTT3J 74
94969 816
5TK77 766
QKA87 467
JQ736 427
JT374 870
J949J 456
8AT8A 299
K9KK8 395
932TA 669
J3AAT 105
474KK 441
7J738 713
742AT 793
K387J 145
4457A 264
5T5J5 908
99996 121
AAJ4A 817
J4JJJ 25
99J59 63
5TKKA 983
J454K 396
K222J 290
99992 393
A2JQQ 321
58AAA 790
39333 165
4264T 737
69969 327
AQAQA 537
KT9J8 49
4QT86 126
KKAJK 304
84448 414
Q2425 444
6558A 675
A9QAJ 770
333QQ 607
93983 73
929T9 856
JJAAA 352
67TQT 382
6579K 55
TT7TT 544
9AAK6 784
65A82 855
48KJ9 111
388AK 272
6T677 890
2AA92 632
663J3 623
TTJQT 386
49475 180
4TKTT 461
88333 722
Q97T4 740
7T624 315
66K6K 86
33398 826
922J5 912
93875 89
KK3AK 447
32T2T 383
77KKJ 699
9T5A7 622
3J823 750
8TKJ6 224
3464T 730
54555 138
526J7 389
A5794 497
779JK 916
T333T 545
74253 743
29222 227
TKTJT 552
9AJ89 574
637J6 236
KKQKK 868
9QQJ4 683
K42Q6 267
9243Q 934
8T98T 210
T6K58 831
29J99 29
JQ4QT 353
56QTQ 692
335J9 825
24J84 208
A2924 859
A8888 933
577K2 148
5AKKK 429
23KK3 201
29229 811
44T4T 455
9QT2J 755
39QAT 172
A2548 423
AK692 270
A44Q4 698
A4443 510
69527 415
4T444 638
AKJ36 927
K355A 561
32Q3Q 220
22A22 93
JTQTA 850
2Q2Q2 188
77787 135
68T6T 813
AAQA7 168
T4554 967
K6888 240
2AA8A 642
AA333 13
8KTKK 70
99K93 686
89588 949
K4565 372
8T9K4 601
33TJJ 763
23J33 565
KQK65 819
K534J 721
43385 563
J5AA5 379
88222 979
5JTTJ 515
2QQ8A 196
8T756 849
38288 454
386QT 310
Q5TT5 936
4QQTQ 852
QQ446 46
QT6JQ 926
242Q2 605
44999 26
93929 10
A23A3 925
66366 564
88A8Q 809
93222 356
45AQ6 800
QKKAK 359
7A645 960
J2KQQ 289
KKK5K 203
7QA44 485
JQQ69 128
Q2QT5 22
K84KK 993
J32A4 430
999QK 778
68Q3K 753
4884J 221
J44JJ 340
JTT93 339
63TT8 348
4TTT6 725
6KA2Q 335
QTKKT 592
4325K 229
Q447Q 606
78TTT 506
66626 406
27444 621
27Q3Q 584
28T27 964
QTTTT 598
8JJA8 649
Q773K 847
2AT8Q 231
39574 241
34QJ8 239
J66KK 98
54J3A 164
77A87 582
KJJKK 768
J6892 846
77TT7 911
44834 184
AJKJT 329
TTTA4 596
5453A 792
5666J 493
TA3A8 333
7TA7A 585
7JJ77 645
K6KK7 843
KKK9K 555
33KT6 123
5AA84 958
93798 341
555AA 783
TAT33 266
Q6QQ6 940
7AJQ6 205
A6656 572
J8JJJ 660
77477 861
QJ666 173
Q344J 955
6699A 332
529A9 976
QK6KQ 640
44QQQ 298
J399K 9
7J294 966
9TAT7 613
2K3Q7 680
999Q5 532
T899T 198
Q95QK 42
97947 509
K777K 682
K45K4 554
82333 153
2J7KK 802
Q6999 187
27732 41
57865 628
62226 47
QQKA2 398
77772 129
67499 727
3J8K8 752
5J27A 833
84444 741
8KJKK 567
AAJJK 904
87768 780
66766 534
AA66A 989
76868 32
47TTT 512
55J53 748
49249 550
2566K 812
5775A 376
J9K99 399
9J66J 325