commit 5d92e811379726c6622a99576a2407cdddf8ec61 parent 5ade9b08107717f9ddd2222eadf5ff5393113d1b Author: Lukas Henkel <lh@entf.net> Date: Thu, 7 Dec 2023 06:45:37 +0100 Day 7 task 1 Diffstat:
A | input/7.txt | | | 1000 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/day-7.lisp | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | t/day-7.lisp | | | 13 | +++++++++++++ |
3 files changed, 1085 insertions(+), 0 deletions(-)
diff --git a/input/7.txt b/input/7.txt @@ -0,0 +1,1000 @@ +486AA 252 +24544 330 +33TTT 540 +242K2 966 +334A4 877 +3632J 651 +22K55 378 +9J656 695 +Q7478 933 +5K266 279 +AK9J8 840 +6JQ9J 697 +47JT6 545 +777K2 400 +QTTTT 115 +6J554 121 +KJAKA 677 +KKK8Q 372 +3K8A6 428 +6JTJJ 249 +2626J 909 +Q47Q9 859 +QQQKT 116 +AQAA6 200 +K88Q8 542 +TTT4T 826 +8K56A 619 +55J27 637 +TTT37 513 +62362 254 +J383K 329 +4Q89J 682 +6TQ75 222 +Q4298 774 +A3AA3 263 +86672 866 +5A5AJ 606 +K6K79 27 +88AJJ 514 +QJ725 961 +77727 509 +8T9JT 582 +6Q464 98 +75887 995 +69TT9 624 +8327K 377 +TT444 561 +9K89K 204 +JQAQ5 393 +TTTJT 331 +3T8Q2 588 +JKKKK 572 +AT5TT 810 +AJ552 162 +Q2A23 258 +688QA 816 +4T555 183 +K4342 461 +A9929 369 +7J969 730 +8KAQ9 825 +KK47K 170 +4Q4JJ 150 +444Q4 964 +A7A77 251 +999JT 186 +8A88A 325 +2623K 135 +3Q767 913 +99989 855 +4444A 556 +6J757 383 +77788 75 +57A24 185 +Q3QK3 627 +6A855 982 +6KQ23 399 +TT49T 630 +6AAAJ 443 +76K8K 940 +Q8888 747 +AK48T 250 +22A22 506 +TT547 538 +Q5J2A 11 +546A3 137 +A38Q2 753 +TK3K2 881 +22627 771 +Q67AK 298 +J2262 782 +2QAQ2 5 +JTK7K 625 +A3337 766 +6JQ45 924 +A9999 544 +45444 716 +K7777 673 +6QT4T 541 +T4222 811 +T8888 799 +QQQJQ 426 +5858J 886 +77A77 897 +QJ866 448 +48J43 73 +2Q4J7 432 +66K66 787 +69A4K 114 +T6792 685 +79A99 306 +KK9K9 860 +96666 992 +85858 558 +Q2AT4 308 +Q9QQ5 496 +8AAJA 271 +T3TJK 919 +2836J 662 +78878 431 +88887 184 +J3QQ8 609 +T4JTT 778 +AJ533 30 +3TT4J 349 +J6282 925 +6Q874 82 +AKK24 832 +3QJ33 65 +65522 288 +22Q22 749 +J8K84 497 +A343Q 794 +38J53 314 +886Q8 613 +7Q568 899 +AQAKK 990 +3978J 438 +39K6J 468 +8428J 466 +TJ4T7 776 +73825 726 +AK53Q 683 +6J434 206 +68Q84 64 +8KTTJ 505 +JJ77J 773 +73355 13 +QQJ36 441 +J6KA5 980 +Q7A88 718 +46646 449 +JQ994 460 +Q54A7 675 +A7TQT 195 +3887K 219 +J277T 16 +98J29 486 +TA777 375 +687QQ 462 +KQKQK 687 +KTT28 322 +TT3TT 181 +K4444 125 +Q8QTT 286 +JT5TQ 410 +5AA65 935 +3QTJ3 638 +5T4Q9 10 +JK3T3 595 +2A9AA 362 +A58AA 145 +94KK9 310 +88Q2Q 482 +3985A 244 +6AA96 711 +Q7337 34 +8828J 356 +7379J 903 +28288 83 +K22K2 923 +K5J25 767 +22992 500 +66787 786 +9746J 661 +765T9 31 +9J9J6 285 +6348T 264 +562J9 280 +998QQ 192 +99559 751 +999T9 817 +J5TT5 715 +37722 820 +595K5 435 +8A5A8 187 +59858 139 +42Q5Q 212 +KQQQQ 15 +5QQQQ 929 +Q777J 631 +66TTT 455 +56226 689 +73373 887 +K32T3 665 +3A36A 447 +QAAJ8 143 +84448 324 +T4344 759 +9J99Q 371 +98989 309 +72T46 717 +QQ5KT 821 +8T64A 236 +A2AA2 918 +52775 907 +7T7T7 211 +6Q782 535 +75A36 12 +Q9Q62 274 +T2463 752 +797J9 471 +K7KK7 414 +444K3 917 +4QQAQ 586 +59J64 895 +6K6J6 142 +6T464 366 +3K5AA 465 +3QQ4Q 769 +K5533 941 +T2299 39 +333TJ 597 +AA8AA 975 +9J4KK 198 +6QQ44 554 +44469 433 +264AQ 628 +9986J 178 +786J7 894 +86866 656 +5J555 156 +QTJQK 858 +A84AA 526 +32J37 132 +4666Q 427 +894QT 472 +7K469 1000 +9J77J 888 +95559 478 +44553 600 +K9J9K 37 +96979 814 +99393 148 +A99AA 88 +953TQ 708 +6KTQ3 437 +KKAJK 358 +72292 439 +63362 599 +5J88J 639 +74444 604 +6Q6J3 698 +TAA66 843 +9QQJ9 168 +TT8TT 381 +JJTA2 960 +36363 423 +455J8 602 +8A543 223 +Q8T42 884 +8A888 227 +AQAA5 872 +6K525 364 +58KA9 62 +33277 296 +KAKKA 386 +665T8 259 +KJKQK 593 +36355 85 +5KK47 926 +35539 17 +66466 19 +A979Q 589 +54888 218 +477JT 574 +66634 105 +76T25 161 +2739J 883 +J3433 808 +7J7J3 436 +7252T 721 +6QA2J 18 +2T373 334 +39393 770 +39969 262 +79T99 833 +2794Q 14 +9AK36 996 +JQ4QQ 579 +TJ53A 757 +JA7AA 302 +K3333 159 +4K4KA 424 +26T62 988 +A4582 571 +59999 841 +9999J 959 +882Q2 220 +74777 484 +48T53 179 +9T947 269 +8J62A 113 +88KAA 837 +5J2JQ 319 +9K499 904 +282A2 756 +78K63 801 +2A5A5 539 +Q6QJK 171 +55535 492 +7J775 323 +62626 291 +88883 459 +69336 196 +TTA4T 921 +6236T 667 +58K58 475 +68KK8 201 +55559 875 +26AAA 616 +4JT59 90 +T3333 831 +4A359 394 +53325 914 +T5886 268 +22T44 729 +34443 21 +TA72K 71 +KTJQJ 979 +4TT5T 790 +38983 949 +975T2 493 +AJ2KK 303 +85J75 367 +T44A4 694 +6346T 368 +83383 134 +639J2 126 +2435A 77 +6K972 741 +5TA55 622 +5TT66 567 +99949 193 +KJ973 357 +5QT38 225 +73A7A 748 +43555 233 +3A46J 335 +72272 154 +A3832 660 +74774 997 +96QAK 405 +4A844 305 +3J3J3 517 +K5KKK 720 +QQT7T 727 +55282 487 +523Q4 99 +55585 912 +QJK65 796 +222AJ 734 +TTKTT 235 +QK67J 245 +4A4JA 182 +9TAAT 84 +79K43 353 +AAQAJ 149 +8A8AA 587 +TA5J9 952 +58888 237 +79Q25 658 +258KT 512 +424T4 502 +6J666 485 +5555A 457 +7A523 809 +K5K66 188 +ATKKT 981 +33Q66 594 +75A88 987 +6J677 91 +68688 864 +QQ33Q 635 +22427 755 +AJK83 153 +42Q8K 802 +AJT8J 650 +5AK6A 691 +979K5 253 +5K45K 653 +AA459 93 +T2T8J 838 +49947 479 +7Q799 647 +J4524 208 +66K6K 943 +53232 902 +JJQQQ 869 +64K4K 534 +77799 406 +77J7J 844 +A75QK 971 +865A2 26 +KJ4AA 885 +TA856 835 +84A78 703 +J9696 92 +J28K2 891 +KKAAT 546 +K2592 531 +AA96A 676 +73KT7 793 +66JJ6 719 +99379 578 +8Q685 739 +QQQT9 282 +JJ4J4 420 +66969 106 +A7AQQ 850 +9K526 311 +AQ8TJ 246 +462K2 706 +656J6 504 +TQQ57 172 +77Q37 416 +5857Q 857 +QQQ3J 265 +44J84 354 +TT537 991 +QAT29 213 +8A9JA 823 +684Q2 275 +54T92 946 +5K55K 283 +94732 986 +55565 700 +6JA66 893 +3K33K 865 +JKKTK 641 +99636 984 +333T9 396 +24J25 828 +33AJ3 939 +J7J87 347 +84884 577 +3J366 666 +QQ4Q4 70 +KQ562 806 +95275 9 +86KKA 95 +7K67K 868 +TAATA 668 +7JK9J 614 +855J3 138 +59496 360 +66272 422 +7927A 255 +644J4 970 +3K33Q 284 +2T44J 346 +66626 775 +622AA 450 +JJJ8J 97 +T73T2 848 +26686 480 +8K58A 710 +5A55A 852 +34683 701 +2J827 834 +ATAAA 772 +9444J 853 +J2828 805 +JJ369 215 +J2962 849 +T7TTT 216 +9T23K 788 +T8759 38 +9777J 768 +934KQ 141 +K7A7J 738 +QQAAQ 446 +9J93T 524 +JAJAA 272 +22322 445 +6T699 340 +99939 203 +A5525 528 +7QKA2 754 +5J558 873 +JJJJJ 147 +24244 626 +JJJ2J 972 +JAAAA 740 +55QQQ 607 +44K45 804 +KKQKT 822 +J9A5A 905 +4A85T 892 +J3333 199 +KTA9T 798 +33393 294 +TJ986 692 +56J4T 510 +967J7 160 +JQQ33 59 +Q744Q 779 +2K8AK 87 +JJ852 189 +4K77Q 365 +42222 702 +A2272 792 +88949 874 +TKT9K 879 +Q59Q7 41 +TAT9T 477 +K4J4T 533 +2T2Q2 861 +K6JQ6 569 +JTT22 947 +2J64A 555 +K6TTQ 898 +AJJQ7 967 +QQ444 566 +TTT7Q 313 +K488T 977 +45992 723 +38Q33 495 +6T75J 927 +J323A 100 +QQQ77 395 +88799 746 +KK9KK 295 +JJ833 781 +87A2K 384 +AQ66A 61 +856Q2 191 +5765K 167 +J24TJ 591 +362QJ 640 +2JT43 654 +438J5 217 +73299 999 +33K2K 915 +2952T 300 +8666T 764 +22332 644 +3T9AA 307 +79877 575 +TJ6T7 516 +T6TJ6 163 +6T6J6 659 +4K426 511 +777A4 131 +44494 289 +QJ82J 690 +Q22Q6 760 +55527 529 +559A5 791 +KKQAK 704 +KA938 621 +KAK9A 934 +2J992 123 +QQ2QQ 994 +3TT5T 763 +6KKKK 3 +4254K 398 +J2442 522 +39J37 549 +64444 714 +KQ7Q9 226 +79T79 328 +TK3J6 20 +J5T95 762 +83882 474 +97777 916 +AT673 570 +2A333 998 +7J77K 146 +686QQ 518 +5T77T 671 +TQ2QQ 281 +J4444 784 +JK46T 118 +797QT 379 +48776 130 +484K4 173 +JK794 72 +7QQ6Q 725 +3KT24 209 +4J545 581 +63T6Q 339 +88AKQ 812 +99A89 101 +QQ799 55 +4Q8QK 610 +864J9 370 +4QJQT 107 +644QJ 669 +A3AAA 780 +9A8AT 950 +54342 25 +43433 164 +58554 559 +84887 50 +K9KKT 341 +T3JA9 104 +57T57 453 +J4JQ5 592 +KJJA5 688 +J8898 374 +59772 646 +K66QK 696 +T2T44 440 +Q7AJ3 120 +Q7777 532 +997KQ 292 +A4AAA 931 +K4KTJ 451 +4JAT8 655 +Q77Q3 169 +QTTQQ 618 +68ATK 257 +AJ4AA 948 +26636 603 +3J656 382 +4A6AT 936 +54555 846 +J3289 338 +66556 900 +J5A9J 112 +55T5T 735 +K6484 634 +444KK 862 +K6K6K 205 +K55KJ 842 +Q27QT 652 +73777 901 +949J9 4 +J26K3 889 +2AJK9 79 +22227 584 +54T9A 42 +995A5 736 +6QT48 452 +K6324 363 +7Q832 807 +6K633 951 +JKK7K 387 +2TK73 678 +T4JJT 552 +66665 908 +77877 232 +J88J8 789 +A8AA2 320 +92832 490 +44399 355 +AT953 750 +5A755 388 +KJ945 312 +JAA88 119 +QK9QK 521 +7T2T7 871 +6K4T9 583 +5AAAA 978 +T2JT4 174 +7J447 361 +8QJ8A 470 +T5263 43 +3K3KK 63 +88284 57 +48A72 722 +4T562 190 +6TT96 2 +3Q5QQ 221 +42744 401 +88442 345 +Q3Q22 686 +69Q28 177 +J33JQ 839 +833QA 392 +J8838 598 +QQK75 376 +8888J 413 +QQQQ6 488 +Q6Q6Q 127 +9T9JJ 158 +355TK 827 +4QAAA 765 +T7QJ7 498 +445T4 911 +AA2AA 337 +33733 672 +92KK4 958 +KJ746 231 +3632A 22 +3K45A 348 +T7673 938 +39J99 803 +Q8T72 58 +77444 49 +44AQ5 7 +657A4 663 +QAQQQ 580 +974TK 643 +2TJJ2 273 +89993 342 +TT333 590 +J88JT 920 +AQ2K6 240 +68644 229 +92A9J 391 +444AA 962 +48Q4Q 473 +Q3Q33 133 +7Q233 373 +2J2J2 878 +9J488 954 +AJ979 944 +8T3J8 352 +J87Q3 56 +QQ3KQ 224 +3K597 267 +9J2TQ 830 +J7444 36 +55757 605 +56556 732 +KA6QJ 290 +7T95A 955 +39344 434 +72T39 256 +3A333 102 +4K8A6 507 +55K9T 657 +622JT 24 +6J766 151 +9J9A9 238 +866J6 464 +6KJK6 851 +4T4T8 543 +KJA3K 248 +A4J86 537 +T8QJ8 454 +A93AA 421 +JJJQQ 863 +284K4 620 +9J978 67 +K7675 536 +TT7TA 287 +TJ92A 343 +4QT44 276 +7777T 78 +2QT26 444 +85558 230 +9A9A9 731 +Q5225 501 +22TQT 425 +555J6 930 +Q333A 563 +45844 202 +QA472 679 +7TJ77 442 +Q7222 122 +JK6JK 397 +99K9J 417 +44434 615 +J75A7 633 +AA2KA 728 +2Q22Q 68 +J55J5 359 +J4555 415 +66823 649 +JK3K3 896 +773J5 74 +T6777 28 +K9925 53 +K2KTK 611 +KKK44 89 +J3KKK 129 +TK8JA 109 +K3339 525 +Q59QJ 928 +J8TTT 402 +77757 612 +KKKK7 155 +85A59 197 +KQQK5 350 +6776K 301 +KKK55 910 +AQAQA 407 +92299 693 +9A779 175 +Q6A27 968 +85QQ8 761 +TA536 483 +66363 278 +Q53Q3 234 +K4A28 596 +7QA8T 565 +33355 503 +AAA67 40 +JA66A 351 +634KA 96 +JA555 489 +TA457 813 +A87JK 297 +7777J 527 +8J8T8 664 +6TK87 194 +88688 568 +QA8A6 867 +TT99T 228 +J5955 890 +52777 385 +AQ44Q 737 +9867T 299 +QTQTT 94 +QQTQ7 876 +T3TQ6 573 +5Q555 44 +A73T4 69 +QAT72 629 +K777K 636 +2222J 404 +Q4J4Q 103 +JT2A2 783 +J55JA 315 +585T5 744 +J3773 645 +5625A 608 +TA632 327 +4J4J4 523 +53535 824 +636K5 976 +4T278 318 +9Q77J 815 +64T87 758 +7QJQ7 547 +A7A8A 945 +Q6573 29 +2QQ2T 266 +AK427 152 +22592 304 +69AJ6 261 +T5T52 180 +TQ59J 705 +99669 648 +6TQJ4 785 +6AQQ5 430 +TQTQ5 389 +J7AQ5 176 +33A4A 463 +T8383 344 +Q636K 963 +Q3AAQ 1 +K29Q3 870 +JQ8TT 157 +2Q96J 491 +5KK94 709 +K44J5 856 +J233T 476 +K5555 326 +283K9 965 +TQAAA 23 +4T5JA 165 +6477A 81 +82855 836 +8T88T 973 +A3JA3 110 +49449 983 +92K2K 124 +8J777 336 +A8555 519 +JJQQA 409 +55865 617 +T4484 481 +K729T 777 +55A44 111 +667Q9 989 +A6J55 906 +3A44J 128 +K4A65 108 +Q8A88 680 +Q8K63 411 +4TT4Q 733 +893A4 937 +7374T 458 +659JK 144 +JQ642 601 +52333 712 +JQA3K 277 +QQ555 140 +5377A 117 +98T89 46 +T8T8T 674 +KQT88 243 +J7333 317 +KKQ7Q 560 +66636 33 +5AT6T 942 +24224 52 +A6276 797 +J99J9 60 +Q777A 80 +87TQJ 241 +56QT3 553 +KK9KJ 745 +AJQA9 707 +6JA99 408 +6385K 403 +K5JQQ 829 +6K644 985 +5K4Q3 530 +QQTQQ 51 +255K4 316 +T555J 499 +66Q66 882 +8K89J 166 +766T6 214 +A6AJQ 845 +886A9 969 +97264 974 +69Q69 551 +95529 642 +K569T 47 +K2222 333 +TKTKK 520 +J7JT7 632 +22822 242 +5282J 684 +9Q8T9 548 +9KJ75 957 +JKJKK 854 +6T6T6 270 +6Q6Q6 412 +AJKQ5 380 +J3335 508 +77677 623 +698K6 207 +QTT78 795 +AA5K4 332 +38838 932 +TTTJJ 956 +99TT8 418 +7454J 800 +Q7AAA 880 +TT5T5 494 +432AJ 429 +QK9AT 293 +6668Q 6 +6TT2J 847 +4A4A3 713 +4J9K2 469 +23333 136 +T4J28 819 +KA923 681 +A6256 210 +QAA33 953 +4JJ48 48 +9K5JK 576 +6Q5J3 390 +6AAAA 564 +62252 54 +AJT6J 993 +66JKJ 724 +44554 45 +992JK 260 +8T8Q4 239 +97A34 699 +KK85K 419 +8J638 557 +49K88 76 +8K99T 321 +Q9993 8 +57744 922 +24KKK 35 +TATAT 742 +QQ988 550 +5AK55 456 +5QK58 467 +T8TK5 743 +4Q66J 818 +3449J 585 +JA428 66 +8J957 562 +Q7KQQ 515 +2Q9QT 247 +8QQ88 670 +38783 86 +6646T 32 diff --git a/src/day-7.lisp b/src/day-7.lisp @@ -0,0 +1,72 @@ +(defpackage #:aoc/day-7 + (:use #:cl #:aoc/utils) + (:export #:day-7)) +(in-package #:aoc/day-7) + +(defun parse-hand-bid (line) + (let ((pos (position #\Space line))) + (list (coerce (subseq line 0 pos) 'list) + (parse-integer line :start (1+ pos))))) + +(defun parse-input (input) + (loop for line = (read-line input nil) + while line + collect (parse-hand-bid line))) + +(defparameter *cards* (nconc (list #\A #\K #\Q #\J #\T) + (loop for i from 9 downto 2 collect (code-char (+ i 48))))) + +(defun card-strength (card) + (abs (- (position card *cards*) 13))) + +(defun make-pair-detector (length) + (lambda (group) + (= (length (cdr group)) length))) + +(defparameter *pair-detector-five* (make-pair-detector 5)) +(defparameter *pair-detector-four* (make-pair-detector 4)) +(defparameter *pair-detector-three* (make-pair-detector 3)) +(defparameter *pair-detector-two* (make-pair-detector 2)) + +(defun hand-strength (hand) + (let* ((groups (group-by hand + :key #'identity + :value #'identity + :test #'char=)) + (three (find-if *pair-detector-three* groups)) + (n-pairs (length (remove-if-not *pair-detector-two* groups)))) + (cond + ((some *pair-detector-five* groups) 70) + ((some *pair-detector-four* groups) 60) + ((and three + (= n-pairs 1)) + 50) + (three 40) + ((= n-pairs 2) 30) + ((= n-pairs 1) 20) + (t (card-strength (first hand)))))) + +(defun calculate-strengths (hands) + (loop for (hand bid) in hands + collect (list (hand-strength hand) + hand + bid))) + +(defun hand-strength< (hand-1 hand-2) + (let* ((strength-1 (car hand-1)) + (strength-2 (car hand-2))) + (if (= strength-1 strength-2) + (loop for card-1 in (second hand-1) + for card-2 in (second hand-2) + while (char= card-1 card-2) + finally (return (< (card-strength card-1) + (card-strength card-2)))) + (< strength-1 strength-2)))) + +(defun day-7 (input) + (let* ((hands-bids (parse-input input)) + (strength-hands-bids (calculate-strengths hands-bids)) + (strength-hands-bids (sort strength-hands-bids #'hand-strength<))) + (loop for (hand-strength hand bid) in strength-hands-bids + for rank from 1 + sum (* bid rank)))) diff --git a/t/day-7.lisp b/t/day-7.lisp @@ -0,0 +1,13 @@ +(defpackage #:aoc-test/day-7 + (:use #:cl #:lisp-unit2)) +(in-package #:aoc-test/day-7) + +(define-test test-day-7 + () + (multiple-value-bind (task-1) + (aoc:run-day 7 "32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483") + (assert= 6440 task-1)))