diff --git a/day7/day7.py b/day7/day7.py new file mode 100644 index 0000000..887d8ed --- /dev/null +++ b/day7/day7.py @@ -0,0 +1,49 @@ +from collections import Counter + +def calculate_rank(hand): + card_ranks = {'2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12, 'A': 13} + # substitute cards with their ranks + hand = sorted(card_ranks.get(c, None) or c for c in hand)[::-1] + cnt = Counter(hand) + cnt_values = sorted(cnt.values()) + rank = { + "5": 10, + "14": 9, + "23": 8, + "113": 7, + "122": 6, + "1112": 5, + "11111": 4, + }["".join(map(str, cnt_values))] + # return rank and sorted hand as a tiebreaker + return (rank, hand) + + +def part1(inp): + total = 0 + hands = [l.strip().split() for l in inp.strip().split("\n")] + print(hands[-10: -1]) + breakpoint() + hands = sorted(hands, key=lambda hb: calculate_rank(hb[0])) + for rank, hand in enumerate(hands): + hand, bid = hand + total += (rank + 1) * int(bid) + return total + + +if __name__ == "__main__": + sample_input = """ + 32T3K 765 + T55J5 684 + KK677 28 + KTJJT 220 + QQQJA 483 + """ + assert part1(sample_input) == 6440 + + import sys + if len(sys.argv) == 2: + with open(sys.argv[1]) as f: + res = part1(f.read()) + print(f"Part 1, res={res}") + diff --git a/day7/input.txt b/day7/input.txt new file mode 100644 index 0000000..3cbfc90 --- /dev/null +++ b/day7/input.txt @@ -0,0 +1,1000 @@ +77776 507 +26J35 214 +9999Q 314 +3JKQK 630 +AA77K 94 +55A55 372 +925J4 622 +788T2 783 +A6A9A 763 +K588K 2 +KA6JA 601 +22KQ7 574 +59T95 281 +4K973 658 +A7TJT 413 +6K2J2 859 +KAKAK 528 +84ATT 657 +T4792 376 +57J6Q 414 +23Q2Q 466 +7TQQQ 718 +88588 858 +396A7 213 +93339 843 +3KKTK 221 +QQTQQ 323 +ATT85 608 +2643K 989 +J888J 439 +J66J6 754 +95777 353 +497Q3 576 +77AKJ 829 +9KKK9 516 +55585 98 +3J3JQ 675 +87727 780 +J6T2T 223 +5353A 826 +47TKJ 285 +6A66J 840 +KQKQQ 112 +J2T2T 697 +7J268 172 +88J44 385 +468JQ 350 +A75JT 973 +6T69T 489 +4Q222 705 +T869K 173 +6T677 43 +Q7963 562 +6JA33 322 +J7579 274 +4T9A6 455 +TT7T9 671 +226KK 679 +2KQQJ 957 +AJT9A 494 +Q55AT 365 +6Q33Q 201 +49KTA 293 +2257A 438 +JJJAA 116 +A6985 644 +2777A 397 +T6AJ5 912 +46TAJ 459 +77547 674 +JKKJK 779 +7Q324 998 +645J7 977 +34K2K 232 +8KA97 187 +55554 470 +5A66K 282 +88882 632 +75777 321 +94555 163 +478J8 967 +46264 884 +TTT7T 165 +3J8TT 828 +72868 915 +95K55 368 +AQ5T9 686 +JJ2AA 568 +AA7Q2 838 +77AJJ 896 +83333 345 +6T897 54 +337Q7 95 +29A99 317 +222QQ 539 +98534 359 +2J27J 834 +A5K5K 72 +Q5K57 641 +46744 307 +99J92 653 +534K9 706 +27J72 342 +T77T7 745 +6676J 635 +6TTTT 243 +44222 555 +A5T55 426 +29924 596 +Q8J96 683 +QK366 792 +JK542 146 +TKK68 302 +28T4J 817 +2Q2JQ 597 +444KQ 388 +K8838 20 +45AJ5 131 +3T3A3 869 +8J98K 577 +49K88 224 +2AAAA 719 +2222K 31 +JA255 194 +QQJ88 467 +TA33T 910 +96Q96 944 +Q7778 150 +AQ4JT 291 +43464 381 +72J85 448 +7J3A5 363 +3A53J 259 +5A5Q5 303 +3KKK9 935 +AA45Q 807 +66999 664 +4QKQQ 49 +282J5 6 +J6JJ6 777 +T5TJ7 11 +344T7 184 +4QT3A 717 +67788 354 +9T6T9 360 +73773 17 +675Q4 225 +KK7KK 588 +66KKK 711 +J5285 136 +72932 425 +99T9T 430 +TTTT2 920 +58858 9 +5K55K 749 +5T77T 147 +58333 325 +9T7JJ 79 +83J56 245 +4AJA4 250 +66296 708 +5K9K9 304 +3478A 526 +JQ828 769 +J447Q 485 +ATAAT 107 +T6A78 685 +45T9Q 386 +T9QTK 71 +QQ2QQ 73 +8T3J3 280 +43434 157 +37777 549 +846J8 586 +5A55J 444 +TA6T6 242 +88846 518 +253A7 500 +K2K22 349 +59T4J 809 +A6AAA 797 +9J2Q7 346 +55595 934 +A94JA 416 +666KA 129 +A4A24 271 +J2722 278 +63336 92 +JK366 811 +AJA76 591 +Q8Q7Q 873 +AJA5T 853 +K5T6Q 599 +J4AJ6 971 +T2TJT 390 +QJQ92 669 +T9T42 961 +8K98A 402 +TK936 923 +K83KJ 230 +JA99A 953 +8QQJJ 964 +75K37 24 +AJT22 398 +47675 695 +TJT55 737 +AJ56Q 514 +54J9K 212 +TTTJ7 446 +J94J2 988 +KA42K 235 +5AQ45 696 +7K772 236 +3977K 435 +8596T 332 +99969 594 +3J84Q 721 +4678Q 729 +A55AA 445 +84T77 839 +A9QQ8 904 +TTT4T 498 +T9762 60 +79K24 678 +K7KA3 462 +J5444 480 +QJ9Q5 289 +66KJ6 978 +55655 647 +AK9K8 930 +2T8AJ 623 +KJKKK 340 +55J58 370 +777K8 389 +28T2T 55 +66JKK 482 +5K3KK 99 +33393 384 +J8443 151 +8AAAA 379 +79799 247 +TJ787 571 +A226K 472 +66KK6 367 +J5762 453 +7K7K7 393 +3366K 355 +QQQ9Q 688 +AAK56 638 +3T53K 872 +TTTKQ 581 +A2A5J 210 +2A2KT 890 +62466 122 +4A5KA 724 +22662 521 +262JA 373 +3K37J 898 +JJAT8 7 +94646 643 +9Q38T 709 +J9Q26 857 +44464 50 +66566 554 +7998K 774 +2KQ62 814 +Q4648 103 +2QJ22 690 +946Q6 411 +44A7J 557 +29A5K 931 +8J248 968 +99773 762 +483K7 962 +22242 567 +49999 527 +885JJ 297 +Q88J3 983 +28T44 195 +48544 909 +T4J2J 570 +545AA 864 +5333K 648 +KK677 566 +2244Q 100 +26229 142 +K5AT4 356 +3J56T 996 +2J655 645 +88Q8J 473 +88733 403 +T4JJK 895 +AA3J3 491 +8JQQ7 188 +AJAAA 652 +T6T64 479 +53363 589 +JQK2A 248 +A9986 324 +AKAAA 879 +K46QJ 41 +AT4AA 63 +557J9 318 +42989 155 +46785 741 +J4TA2 37 +JAQQQ 620 +8KT8K 126 +Q928T 759 +5J557 714 +47JT9 991 +J5AAA 756 +KJ27A 44 +8T598 747 +737K7 506 +48383 701 +K9J99 832 +TAK85 460 +Q83J2 358 +72QQ5 362 +6QQQ5 938 +QT9T9 5 +9A8K5 766 +QJTTQ 45 +JKK3T 933 +A5QAQ 419 +38TA9 299 +55TKT 703 +822K2 927 +AAAA7 357 +5TT9T 192 +9JAKT 56 +935Q7 650 +KK224 465 +TKJKT 208 +3TTTT 226 +QAJ6Q 913 +3T867 715 +3AT92 160 +77378 400 +A9QAJ 875 +88488 298 +2K8K8 452 +JJJJJ 90 +K383K 612 +7A368 326 +6JA2T 255 +22722 955 +5K85K 119 +AA7TT 477 +4J49T 320 +A9QK3 127 +AA5A7 375 +48579 605 +54465 975 +9494J 8 +KAA5A 883 +KT9T2 626 +74433 481 +47JQQ 534 +2222Q 541 +92K55 229 +J8JT6 932 +4AAAJ 668 +3J222 167 +444TJ 614 +44449 217 +Q4982 629 +533J5 176 +937J5 504 +44997 108 +Q38TK 148 +49342 46 +9J98Q 659 +59A7Q 175 +KQ488 851 +66662 945 +75775 295 +96Q6Q 997 +44JK2 241 +929J2 531 +AJ82A 682 +66A45 694 +4T26Q 886 +63393 204 +J3333 424 +99666 310 +TTJTT 341 +72J77 409 +8TTTJ 785 +TJ472 408 +TT333 240 +4A3J3 182 +3K6TJ 922 +5JQA2 816 +AA46A 806 +54397 881 +664J4 238 +98K88 976 +Q9345 768 +T4442 275 +K9995 76 +95K83 841 +K7496 940 +9899T 87 +93949 463 +9Q362 993 +6T576 509 +88AK8 14 +99J99 78 +4JT9J 263 +57755 970 +J66K7 288 +TATTA 908 +577KQ 53 +AA33T 144 +444J7 950 +727A6 984 +2AT7K 517 +TTT22 128 +6J669 511 +5QA6K 929 +856J7 987 +953K7 947 +6628A 611 +9Q9Q9 82 +ATTT8 860 +9K77K 819 +87558 469 +96A2A 124 +6466J 610 +A22J2 171 +77724 958 +959T8 52 +T48TT 573 +23Q82 710 +37393 617 +8878Q 96 +477A7 844 +QQQQ8 267 +Q27Q9 969 +5966T 980 +KK6TK 637 +57555 924 +J826A 162 +J5T5J 639 +A9K6J 156 +5525T 114 +8K79K 982 +JJJJ8 451 +T7625 26 +62222 395 +QQQ33 67 +22977 25 +TK349 556 +52222 450 +44994 575 +3K7K9 728 +77T65 906 +2JQQQ 572 +2K69K 347 +37K8T 137 +99399 776 +JAA66 663 +5266J 787 +96JJ9 692 +JA699 704 +K5K7K 132 +8J679 633 +3TJ68 732 +4TTTK 428 +T9J8K 928 +6J566 123 +38383 782 +Q5QQQ 40 +288TT 237 +T5555 475 +867T2 804 +JTKTQ 866 +AQT42 543 +A3647 894 +22666 405 +67757 74 +9A492 330 +AA444 621 +QQQ3Q 874 +2JJJ9 32 +43343 595 +Q7377 761 +449QQ 607 +9Q7TJ 429 +3Q294 911 +J56K3 547 +KTTA6 348 +92A5Q 246 +JA3J8 12 +QQQQ4 164 +T24TT 478 +72T6K 598 +484QQ 265 +3KJT5 454 +K6A6A 535 +5JJ55 461 +5555J 770 +QQ3J3 283 +TA7A9 994 +KT3TJ 432 +QTQQT 846 +K3948 627 +JJ777 143 +8J376 565 +A7Q96 209 +78JQT 508 +AJTTQ 544 +9979T 251 +2AJKT 981 +T997T 139 +Q6QK7 748 +AT327 361 +97J7J 713 +AAA44 609 +9A43T 837 +JJ222 218 +K33K5 582 +38Q3J 152 +8QK45 186 +777Q7 672 +J2792 887 +QKKQK 778 +677Q7 758 +KKK5K 744 +2255K 956 +53A55 233 +28966 765 +82Q88 29 +Q8A54 343 +9J7J2 104 +785K2 1 +TT2A5 946 +78592 488 +A3333 118 +QK264 538 +J9QTQ 339 +AKA6A 284 +T539A 974 +99JTK 662 +J8333 730 +93QT2 899 +AJJ9A 550 +87878 83 +46643 135 +AAAKK 130 +J4KAK 651 +5748T 673 +96Q43 824 +8K88J 81 +K9566 185 +T44TT 38 +63T7J 311 +9J362 966 +33KJK 422 +77789 33 +8239T 661 +5639T 734 +9JKJ9 889 +TK8Q9 600 +45A7A 888 +96J98 110 +TJ9KT 279 +95999 992 +4J444 457 +J5559 786 +AQK85 919 +29KTQ 69 +25223 781 +Q6235 585 +99398 262 +559A5 333 +Q9845 812 +4K58J 66 +445KK 590 +AA77A 27 +J7977 753 +26526 10 +45AQ2 646 +Q83TT 371 +KKTKT 900 +K6KJK 750 +KKK43 583 +333T3 406 +4424A 290 +A882T 378 +44484 979 +QT3K3 986 +J747K 960 +29K3T 447 +46465 707 +55J25 505 +AK2KA 788 +5TK3A 941 +3JT44 892 +44232 963 +5QJT7 77 +3AK85 926 +QTTQA 296 +783Q4 606 +QQQ99 624 +4JQAQ 551 +3KT28 228 +69A28 334 +77779 905 +5655T 312 +7A7A7 65 +AKAA8 524 +K4J4K 264 +K777J 698 +QQ6KJ 561 +A77J7 487 +4T77Q 443 +3QAJJ 468 +A49KJ 825 +45445 760 +6Q667 799 +J8Q9Q 856 +5T5T2 689 +58TJ2 366 +2574A 580 +K43Q9 495 +9666K 15 +JQ4JK 738 +5J3J3 773 +6Q378 328 +J2JTA 22 +28362 97 +8K3J4 616 +QQQQJ 105 +3K562 196 +34494 85 +K4823 954 +A4744 219 +K6TJ6 999 +73372 712 +T5TT7 227 +KKJ99 631 +6A6TA 959 +2J846 102 +95T9T 757 +73TTJ 421 +56566 640 +38388 316 +33AA3 854 +95K75 93 +8J488 415 +75K52 948 +4JJKK 292 +54883 560 +855Q5 990 +886K6 642 +32232 407 +96K9T 193 +54885 684 +AJQJQ 755 +TTJJT 39 +4QJKK 716 +9K4JK 423 +57522 810 +TA33J 937 +T2J23 863 +A46A6 30 +K888K 61 +67AT3 84 +47424 272 +JT5JA 166 +Q4KAA 893 +9Q368 731 +55TAA 273 +AA822 751 +55445 916 +8J777 313 +8T8TT 772 +8TTT2 300 +22455 972 +6QQQQ 723 +98885 19 +QKT4Q 519 +99T66 336 +J7777 57 +443J4 337 +JA238 70 +65TQT 775 +9K65A 180 +5QK28 942 +7J82T 501 +7Q3T5 532 +8895J 211 +T737Q 437 +9336K 91 +2J757 699 +TT9TT 764 +8K9KK 191 +4AK4J 308 +44384 602 +77Q8J 159 +56T53 739 +22T5Q 499 +T7664 51 +66QQQ 925 +A9A9A 329 +AA33A 59 +KKKAJ 257 +KK8K7 113 +KKKK9 47 +33J37 503 +25T2T 902 +K59KK 121 +49T99 287 +4A69Q 625 +JK2J2 740 +J34KK 338 +K5Q76 569 +AAAA5 75 +AJ4A8 28 +A68T6 464 +Q22QA 965 +KJ632 901 +JA572 391 +4TAK8 871 +3T57K 634 +A427J 855 +59J99 177 +52256 767 +KTKAA 133 +94593 294 +9AA99 676 +6JJ62 13 +T7323 80 +2KQ33 878 +44424 862 +A4646 301 +8888A 200 +55752 35 +J93Q9 529 +8383A 120 +K5787 680 +2272Q 258 +KKJK3 510 +288J3 835 +KAA44 885 +T7TT7 523 +44774 216 +J6666 548 +Q78K5 456 +73A33 252 +AJAJA 670 +QQ777 306 +4JA88 725 +87778 693 +K67K6 179 +7A76A 823 +T6A8T 170 +A5628 427 +K8888 115 +37733 434 +J8888 276 +6Q792 327 +6772Q 800 +22KKK 891 +55552 545 +4T6A8 401 +KKK55 205 +93993 476 +48859 842 +64666 579 +44J42 36 +2QA67 394 +937K9 649 +K99K9 559 +553JT 512 +8959J 790 +69TK5 269 +833K9 18 +777A3 914 +J6662 64 +JA95T 220 +KKKQK 319 +KKKK6 836 +495TA 603 +Q3K96 833 +AK435 553 +Q7744 660 +AA2A2 244 +TT9T9 305 +298K9 733 +2JJ33 736 +5AAA3 190 +KKKK2 134 +TT34K 471 +44248 655 +55J9A 949 +76677 702 +2J662 111 +K94Q9 830 +TTJQT 268 +T2TKJ 48 +J242Q 943 +3QQ9Q 231 +Q34K3 870 +T7Q7Q 58 +6222J 850 +7JT97 377 +33KAJ 215 +3985T 813 +38663 936 +J2623 952 +A2333 145 +74477 202 +333JJ 677 +88788 593 +477K7 618 +53533 656 +7526K 820 +2A36T 497 +TKKKK 726 +J3362 158 +TKKTT 619 +66869 868 +98888 613 +58835 558 +55225 493 +ATAAA 178 +4T3J3 154 +23K59 604 +99932 803 +6KKQK 254 +992KK 138 +855JK 382 +QQQJ3 125 +29Q38 865 +J5455 490 +K397Q 861 +74J76 536 +T3TT3 315 +66JQ6 831 +KK638 203 +3J27K 449 +4JA44 821 +848Q4 628 +33336 530 +AA592 266 +85353 520 +2TTT9 882 +ATJQ3 654 +3726K 552 +22343 845 +A6A4J 199 +K56TA 794 +6QQQ9 665 +36A95 805 +QQJ6Q 847 +TQ454 249 +9999A 687 +5K336 877 +55835 169 +J6T39 222 +4T94K 742 +JQJQQ 564 +2A368 261 +Q6QQA 852 +98899 23 +4KK44 793 +QAA96 380 +8T5A4 801 +JJTJT 667 +288J8 522 +J99JJ 344 +792J4 1000 +Q5266 352 +JAA92 86 +A2989 89 +J4863 636 +JQ84K 533 +5QQ8Q 546 +66A6A 101 +25J86 410 +8A842 592 +22922 720 +A7JA7 183 +7QJ2A 109 +556J6 802 +67379 995 +23QAK 174 +44QQ4 903 +Q5K83 666 +9J5J9 876 +8Q276 42 +7TK42 351 +26999 848 +86844 727 +9A9J3 897 +7T7KT 140 +AAQTK 722 +96K59 106 +Q6Q6J 492 +AAJA3 484 +5Q238 743 +3AQ52 796 +A8A88 789 +63666 181 +A8AAJ 515 +96747 198 +55588 420 +T4222 849 +99899 795 +3K9Q5 784 +27727 691 +TTQQ9 563 +458KK 149 +2J78A 917 +224A2 496 +99K99 387 +44884 808 +26TTT 418 +Q45K7 798 +433AT 951 +QQTKJ 771 +49T8J 615 +85TQK 234 +9Q4A7 270 +6928K 752 +T4TK9 513 +4226T 207 +A7666 3 +3Q865 197 +A7777 331 +77T67 153 +22T2T 417 +AAAJ2 16 +6Q3Q5 867 +33K38 168 +KATAJ 921 +66668 189 +Q3KK2 746 +AA7KA 431 +94KQJ 260 +K9TQ6 4 +8J8QJ 458 +Q9AK9 486 +7JJ3Q 141 +77764 286 +A9KA6 474 +6T63T 309 +K4A67 815 +228J2 918 +3J939 62 +68858 985 +35555 88 +AAA9A 399 +A9495 433 +Q55J2 540 +J9Q92 681 +93737 700 +T55J5 404 +K8922 584 +JA492 277 +TJATT 117 +4A444 239 +47922 441 +24A92 436 +5JA99 21 +AKTJ8 502 +Q6KQQ 822 +79797 68 +7Q9AT 939 +6Q268 525 +8469T 442 +Q5Q5Q 335 +44474 587 +48222 392 +KK333 735 +KKK3K 483 +265TJ 161 +T6TKJ 383 +K7664 34 +T9TTJ 880 +66339 827 +A298J 818 +KA4T7 542 +KKK44 253 +JT744 907 +AQAAQ 369 +222J2 396 +AAJA7 206 +3K4J4 537 +9JQQ3 791 +J4J44 440 +Q6J83 412 +8822J 578 +A555A 256 +T9643 374 +77787 364