From 020e322e3bdb161be5dfaaa2707a508d50f3d189 Mon Sep 17 00:00:00 2001 From: kleph Date: Fri, 8 Dec 2023 20:10:35 +0100 Subject: [PATCH] 2023 - Day 7 part 1 --- 2023/7/7.py | 70 +++ 2023/7/input.txt | 1000 ++++++++++++++++++++++++++++++++++++++ 2023/7/input_example.txt | 5 + 3 files changed, 1075 insertions(+) create mode 100644 2023/7/7.py create mode 100644 2023/7/input.txt create mode 100644 2023/7/input_example.txt diff --git a/2023/7/7.py b/2023/7/7.py new file mode 100644 index 0000000..9ac855e --- /dev/null +++ b/2023/7/7.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# 2023 - Advent Of Code 7 + +# file = 'input_example.txt' +file = 'input.txt' + + +class Hand: + def __init__(self, cards, bet): + self.cards = cards + self.bet = bet + self.dict_cards = {} + + def __repr__(self): + return repr(f'cards: {self.cards}') + + def sort_cards(self): + self.dict_cards = sorted(self.dict_cards.items(), key=lambda x: x[1], reverse=True) + + +cards_order = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] + +# pylint: disable=consider-using-with +input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")] + +hands = [Hand(line.split()[0], line.split()[1]) for line in input_lines] +print(hands) + +for h in hands: + hand_dict = {k: 0 for k in cards_order} + for card in h.cards: + hand_dict[card] += 1 + h.dict_cards = {k: v for k, v in hand_dict.items() if v != 0} + +for h in hands: + h.sort_cards() + +hands_5 = [h for h in hands if h.dict_cards[0][1] == 5] +# for h in hands_5: +# print(h) + +sorted_5 = sorted(hands_5, key=lambda x: [cards_order.index(c) for c in x.cards]) + +hands_4 = [h for h in hands if h.dict_cards[0][1] == 4] +sorted_4 = sorted(hands_4, key=lambda x: [cards_order.index(c) for c in x.cards]) + +hands_full = [h for h in hands if h.dict_cards[0][1] == 3 and h.dict_cards[1][1] == 2] +sorted_full = sorted(hands_full, key=lambda x: [cards_order.index(c) for c in x.cards]) + +hands_3 = [h for h in hands if h.dict_cards[0][1] == 3 and h.dict_cards[1][1] != 2] +sorted_3 = sorted(hands_3, key=lambda x: [cards_order.index(c) for c in x.cards]) + +hands_twopairs = [h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] == 2] +sorted_twopairs = sorted(hands_twopairs, key=lambda x: [cards_order.index(c) for c in x.cards]) + +hands_onepair = [h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] != 2] +sorted_onepair = sorted(hands_onepair, key=lambda x: [cards_order.index(c) for c in x.cards]) + +hands_high = [h for h in hands if h.dict_cards[0][1] == 1] +sorted_high = sorted(hands_high, key=lambda x: [cards_order.index(c) for c in x.cards]) + +sorted_hands = sorted_high + sorted_onepair + sorted_twopairs + sorted_3 + sorted_full + sorted_4 + sorted_5 + +acc = 0 +for rank, h in enumerate(sorted_hands): + # print(f'{rank+1} {h.bet}') + print((rank + 1) * int(h.bet)) + acc += (rank+1) * int(h.bet) + +print(acc) diff --git a/2023/7/input.txt b/2023/7/input.txt new file mode 100644 index 0000000..9f870c7 --- /dev/null +++ b/2023/7/input.txt @@ -0,0 +1,1000 @@ +424KT 464 +3J4QA 723 +94Q85 210 +25722 304 +Q4QQQ 176 +3J777 548 +37687 944 +8J6TK 274 +99TQA 623 +58389 577 +AAAQA 346 +33A66 773 +2K2K7 626 +37776 242 +ATAA5 750 +T46KA 551 +99J9T 916 +Q4Q7Q 307 +9JA4A 90 +TT2KQ 19 +TTAT8 483 +28J28 915 +55222 995 +67666 766 +99933 783 +6T6J6 823 +KKK9K 175 +A9KK9 799 +QTQ88 920 +9T9KT 777 +T63J9 159 +6JJ36 898 +229T2 17 +J9QJ5 250 +8K3Q6 42 +QQ6Q3 252 +K8JK7 320 +32523 510 +TQ33J 507 +J227J 913 +835JJ 169 +TJT22 354 +QQQ79 870 +77776 333 +78TQ2 466 +27J27 229 +A5A5A 987 +88444 135 +T7TTT 908 +AKTKK 297 +3678J 496 +Q6JQQ 742 +644T6 877 +59699 653 +A98J8 656 +48466 815 +66J85 316 +KK45K 94 +88777 968 +28565 895 +T24T6 367 +77K84 557 +5A32T 714 +QQQ28 998 +A333A 1 +373J3 144 +JQJK4 439 +9K44K 262 +KJKKJ 641 +Q6AAA 862 +492A9 18 +8Q69Q 829 +6K6KT 192 +QK5K5 865 +JA254 907 +JTK99 406 +87888 705 +553Q6 655 +95577 731 +77775 88 +A223A 369 +7J7J8 455 +48TA5 352 +3TJ5A 451 +6669Q 685 +6T6TJ 275 +77969 505 +66966 616 +55985 124 +8TTT2 337 +97777 272 +6A8T5 502 +353AT 622 +K47A2 966 +22A22 418 +675J4 665 +7A3A3 919 +6J5A2 734 +22227 707 +Q7AQA 79 +23T42 825 +6K58Q 843 +K5J69 68 +JKT4K 683 +T5T9T 628 +4J434 53 +KKAQA 982 +TTTAT 861 +T2722 757 +3A8J3 16 +K53T3 375 +T47T9 668 +73269 806 +J6AK8 443 +44544 189 +A83A8 914 +T264Q 253 +33732 539 +6K544 223 +J8TTT 933 +QQKK5 725 +AJ98T 767 +A847T 35 +J646K 98 +98999 630 +AA9JA 582 +6K3TJ 196 +77JT7 151 +K594J 125 +53K33 290 +664AA 572 +JT7AA 621 +Q8886 433 +K9599 857 +33J3T 292 +5555J 66 +AA456 69 +887AA 934 +7T5TT 639 +QQAKK 254 +98995 593 +88987 108 +T5T5T 118 +7TA2J 737 +QQ58J 996 +6T66T 972 +T88QT 314 +3T33Q 535 +9K2K7 428 +2T884 956 +33535 231 +868AK 185 +QQQQ7 47 +22TA2 924 +KT842 48 +65932 652 +KT276 21 +54554 434 +7A7A6 818 +TTJTQ 833 +J7A98 850 +AJ67Q 637 +A8A88 564 +9955J 10 +TTT4T 740 +36662 204 +2222J 15 +J8998 437 +T6T62 457 +A24J3 249 +AAAA2 474 +36566 796 +2T27T 602 +KAKTA 597 +84JQ3 592 +QTQ2T 215 +66J66 423 +T3Q47 62 +A6AAA 533 +T99TJ 868 +888TJ 485 +6J5Q2 821 +J9499 610 +39KKK 606 +QAQQA 436 +75775 584 +884AT 157 +3T636 162 +97666 509 +J5T5T 270 +332T9 246 +J4KKK 257 +96T69 900 +QQQ38 411 +74A74 309 +JJJJJ 835 +AA3A2 372 +2JA99 860 +29KK2 642 +4T4JQ 981 +944J9 604 +TQT66 939 +25295 673 +59J45 603 +62846 356 +444K4 869 +8QJQQ 371 +33Q43 416 +KK333 567 +TQQ96 46 +2A2A9 478 +4J574 784 +6A6AA 13 +4A646 385 +33575 70 +9A989 407 +42549 576 +AAA88 154 +A5AAJ 7 +J842K 99 +J99A9 305 +56J9A 357 +JT574 758 +55545 199 +55525 884 +A3KA3 497 +4J828 180 +33QJ3 804 +5QQQA 105 +Q9JJQ 452 +8256J 635 +6Q4TQ 774 +Q5AA5 549 +J8TKA 84 +75557 928 +KA2Q3 67 +44944 339 +46ATJ 697 +A8A9T 657 +3Q33A 197 +8Q88Q 454 +24433 334 +23333 531 +57Q6T 198 +KT46J 736 +84T36 554 +99333 241 +K5K97 31 +QQQ76 321 +339J6 57 +22522 993 +24KKJ 643 +6K888 413 +4Q44Q 873 +2K222 400 +83494 239 +TJ5KA 238 +T6J5K 579 +JQTQQ 283 +A5J52 461 +J75K6 575 +KT7KK 885 +7KJ7K 200 +J2KA2 985 +33343 167 +29999 3 +88882 618 +2K666 552 +7777T 724 +99966 792 +443JT 284 +K2KJQ 494 +K4T36 392 +7JQQJ 787 +555KJ 528 +7K5Q8 834 +23743 589 +KKAKA 482 +J9J29 600 +5TT55 205 +345KT 178 +5TQ28 820 +J7T6J 888 +39TJ7 710 +6QJ92 546 +A28JK 782 +44482 703 +AJ2K5 743 +94944 310 +J29A4 340 +T7932 948 +7A468 131 +ATK5T 145 +QQQJJ 287 +598JA 319 +52242 216 +J6J66 222 +KTJT2 540 +55885 511 +TTTT2 132 +3A757 947 +2A322 38 +858AJ 863 +55557 293 +AQ8J9 220 +3675Q 695 +7KQJ8 306 +85A7Q 460 +3J3J3 143 +6A6AJ 594 +84383 715 +83388 847 +6666K 104 +5728A 952 +J555J 763 +7TQQQ 515 +6TTT6 844 +Q739Q 961 +58TT7 276 +66JK6 812 +779Q9 384 +AJAJA 165 +88J39 838 +K25QA 991 +442J4 727 +9K8K9 149 +AAAA7 335 +638J6 207 +888JK 25 +444T4 129 +Q4A5K 527 +9A9A9 671 +TT5K8 570 +9999T 795 +A8A8J 692 +J9993 345 +867A9 684 +J7Q8A 359 +J37TT 932 +77297 12 +29QT4 964 +7Q767 976 +TTTJ4 689 +59555 415 +4J44J 896 +69J87 649 +99495 826 +5QT5J 86 +Q7QK6 713 +2JJ25 296 +8Q686 393 +QQ966 619 +JJTTT 537 +K9253 563 +59AQ3 702 +53482 364 +2KKK2 807 +8J6AJ 232 +48TT9 161 +Q64Q8 615 +AAAJ6 122 +2J72Q 74 +493K2 585 +4TJ86 793 +32K2K 526 +65565 659 +989JK 404 +T235K 880 +27AQ2 568 +354T7 738 +K777J 646 +8JJJJ 303 +27329 620 +69892 596 +3797J 687 +5977A 588 +T58K9 566 +TTTJT 897 +75Q46 456 +9Q999 890 +85J2A 130 +33368 744 +K7KKK 45 +T5459 285 +888J5 690 +6676Q 251 +56QJT 11 +TKQJK 722 +9QT3T 803 +55KKJ 645 +3357J 814 +KK685 308 +K4TKK 332 +33296 343 +2T4K7 836 +32232 629 +2K8A5 164 +J2Q98 578 +2T22T 810 +3Q3QJ 607 +Q393K 927 +3AAAA 440 +9J374 341 +7A777 484 +3J355 711 +9TTT6 752 +T6Q5T 121 +Q287Q 648 +8988T 472 +3KQ52 957 +7TTT7 226 +AJKK7 177 +AA555 295 +Q2QJQ 396 +68868 498 +9Q34Q 941 +588K8 946 +47J53 386 +QJ44A 348 +37977 217 +88JJ8 855 +36663 808 +T69A8 174 +47AA4 883 +36444 504 +2K22K 943 +565TJ 431 +33Q33 571 +J28JK 414 +38Q4Q 609 +T777T 662 +QKQTQ 54 +3327J 492 +2T4A9 59 +6A7K4 60 +99889 6 +K2QA8 425 +76467 141 +5JJA5 102 +QJKQ9 909 +QQQ8Q 936 +96J63 832 +624J7 36 +86666 109 +A44AA 854 +T2987 892 +64A65 230 +Q677T 841 +T476T 458 +JA6TQ 315 +KKJKK 959 +33363 631 +6Q6Q8 675 +77474 398 +J623J 134 +Q3JTT 953 +55787 779 +AAAQQ 770 +52342 247 +779T7 625 +J22J2 77 +3J3Q5 699 +9KKJK 412 +4QJ42 273 +666Q6 355 +4Q4K8 101 +T4236 183 +QJ9Q8 476 +99995 716 +28283 327 +4Q9T8 910 +968T2 983 +24444 647 +846K5 765 +37KTJ 453 +J7888 904 +K6T73 358 +TKKTT 681 +59939 116 +62222 840 +73757 382 +44677 34 +KKQJK 311 +K4AJ2 856 +7J3J7 513 +A69K5 347 +2A2JJ 388 +T34AQ 781 +3KKJ9 50 +78447 63 +72727 617 +KK535 73 +25562 184 +3Q855 227 +Q46K8 245 +9QQ99 87 +T2A47 387 +47499 544 +J4279 22 +2QQ2Q 935 +4484T 331 +666J5 450 +6QQ67 491 +76KT4 940 +357Q8 160 +7KK2Q 764 +2J662 323 +5A858 444 +955TJ 830 +K927Q 595 +8JQAT 64 +8A3QJ 138 +JA3K5 302 +55Q55 644 +48888 71 +923J2 432 +Q6Q5K 182 +29929 403 +29466 612 +TQQQQ 58 +99J82 467 +K84A2 363 +93493 420 +55J46 106 +449J4 886 +K3332 871 +JJ83K 350 +6J4T6 72 +6T655 163 +39877 424 +73AA7 613 +K6664 173 +44T93 994 +Q937K 136 +J2J4T 694 +56AQ9 517 +Q4TJQ 419 +7K774 789 +6672J 218 +J585A 894 +85J87 2 +6A3KJ 827 +45559 103 +25J44 828 +6K6K5 225 +37737 790 +78887 447 +J363K 963 +533TT 442 +497TK 81 +22992 902 +48864 80 +Q5AK6 661 +KKQKK 115 +58Q9T 771 +J4444 717 +3QQ3A 256 +23622 366 +6A497 373 +7777J 732 +92A86 719 +929Q2 899 +3JA3A 536 +TJ38J 459 +93654 967 +29K3A 353 +K48JA 864 +44Q3Q 556 +J4A39 728 +76A77 522 +52959 374 +675Q6 922 +J55T8 819 +22392 95 +33223 514 +K494J 876 +82JTQ 166 +A9J77 410 +6J8Q7 221 +KKK44 801 +22A34 322 +8JAQQ 561 +9899J 543 +Q9K99 524 +2JTQ9 51 +62642 465 +QJ55A 317 +63QA4 172 +99Q8Q 965 +4444Q 678 +QQ2J3 427 +22828 234 +3A858 788 +KQKTK 918 +A4939 96 +76J34 521 +227J5 219 +2AQ78 525 +74J4T 906 +3T33T 168 +JTT98 917 +9J749 878 +96999 590 +654K7 640 +35AJ8 1000 +72934 837 +7Q6J7 718 +77859 265 +2AJKQ 508 +2J322 282 +JJ665 569 +92222 762 +5222T 435 +T9JTA 786 +AA853 848 +8AAAJ 555 +3J29A 760 +4A5AJ 75 +5874Q 313 +49T44 542 +T8TQ4 636 +99955 140 +8QQ8A 500 +727J7 676 +48884 119 +6KA66 409 +66566 748 +Q6QJ4 611 +4A87J 78 +TT888 376 +28Q89 344 +28777 805 +35543 797 +4T522 278 +636AA 233 +26K5A 228 +2A9Q7 506 +23299 194 +J3AAJ 29 +66466 286 +AT878 43 +Q46J6 324 +63J66 721 +TK9JQ 741 +95Q5Q 960 +22T22 686 +5KK7K 381 +Q85QQ 852 +39799 701 +T38TK 730 +QQK22 28 +69QAT 633 +23663 545 +QQ6AQ 261 +A7KA7 845 +TJ7JT 351 +T5888 37 +KKK86 867 +66JJ2 581 +6686J 632 +A93AJ 243 +J23K5 739 +5599K 955 +TT323 477 +KTK9K 390 +Q757Q 258 +6699J 530 +65QQT 745 +9K2AA 520 +46J3T 441 +3828J 9 +TT3T8 733 +56A92 712 +AAJ7A 389 +22749 401 +55237 925 +46JJ3 471 +97K65 945 +Q8K8J 658 +QJ2QA 990 +99AAJ 408 +6Q665 181 +75AAA 362 +65J84 746 +76667 391 +3735K 421 +7QQ99 235 +K3333 139 +44844 759 +Q8955 395 +97972 291 +6T68T 794 +24A42 56 +8262J 706 +64442 754 +QQKJQ 289 +7J62J 394 +5T555 288 +4K669 997 +J5Q9K 378 +66663 729 +Q2233 921 +JJ557 780 +3T43T 809 +6K866 937 +52527 264 +AAKJK 516 +KKKK3 978 +92J2T 541 +T4248 753 +J746Q 726 +JJ36K 969 +2QTA4 49 +J3JKK 558 +A6J7T 405 +AAT7T 693 +J8886 688 +Q49Q4 370 +5258J 187 +4A49J 4 +KKK5K 992 +7555K 110 +289K6 638 +76J7J 853 +4JTKJ 775 +KK2QQ 958 +224Q3 41 +QAJ6A 469 +3KJJJ 866 +6666T 128 +34944 426 +55675 137 +85888 846 +J8888 462 +K6A5J 23 +KKTKK 30 +9T853 111 +77479 749 +86565 487 +6K3JK 82 +37333 156 +572AK 336 +33388 8 +6T6K6 747 +22K26 133 +K2K7K 281 +J999K 429 +Q7474 202 +AAAA8 650 +TT3TT 171 +55855 560 +TKKJT 495 +J4TA8 881 +Q2J58 203 +27767 123 +4J8QJ 499 +42KKK 709 +29T9Q 32 +J2KKT 224 +77778 587 +4K26Q 147 +6A4A4 212 +43JTQ 887 +53559 704 +4ATT4 605 +5T77Q 113 +KTTK6 33 +A6A32 380 +333J3 260 +99J9J 931 +A786K 114 +74J25 667 +98889 973 +999J9 529 +52KK6 858 +55959 720 +AA2JA 999 +Q84AT 550 +AAAQJ 660 +99T59 266 +K58K7 445 +J2525 532 +999A9 672 +2225J 926 +QKJQJ 438 +JKTQ7 24 +JQ929 468 +989Q9 97 +3JQQQ 942 +4TT34 271 +J8QQ8 475 +8666T 989 +2T83Q 422 +J8J62 397 +4369A 831 +ATAJT 680 +J3338 493 +86668 813 +36JAT 874 +KAK3K 923 +QKKA6 580 +3J339 624 +99676 152 +42K84 155 +KAA7K 682 +QAQQQ 486 +75747 669 +A37T7 851 +K6Q6K 20 +T792A 61 +33654 879 +555J2 417 +AA9A5 209 +TK65K 368 +42222 951 +QQ666 970 +6444J 44 +6JJA5 190 +4K4KT 489 +2T2TT 27 +6A666 986 +35J55 962 +AA277 817 +5K4J5 55 +93AJ9 237 +8888Q 824 +57355 39 +22QA4 150 +3572A 361 +295A8 889 +A63K7 842 +8AJ8T 244 +A8778 538 +A77A7 83 +T8559 89 +TT999 480 +K7JKK 562 +3A3A8 677 +9J6J6 523 +T2684 470 +8J6Q4 698 +83T5Q 761 +88AJ7 26 +54J43 383 +86J68 301 +TQ443 107 +8AAKA 255 +QJK7K 670 +2QQ9J 201 +34674 839 +3232J 905 +8A82A 5 +89K88 519 +83666 338 +A2382 772 +858J5 583 +8AK92 651 +K58T7 930 +55AJ5 170 +77288 891 +KA57T 14 +JQ8QJ 329 +QQ88Q 929 +48573 279 +4K4KJ 980 +46Q7A 601 +79J89 267 +9T994 402 +27T9K 800 +36363 822 +83486 769 +723QT 112 +84794 236 +2Q22Q 691 +98888 213 +AKKA9 872 +A57K5 365 +27K34 100 +555QQ 565 +5KK55 299 +6Q345 360 +7JQAA 512 +A8434 211 +TKA93 503 +93333 463 +AJAAA 146 +996Q6 263 +88838 501 +KK56K 971 +K24T2 330 +J3888 473 +KA968 127 +5466J 700 +A9893 268 +K7J78 666 +44Q54 312 +K3QTQ 984 +6JQA6 778 +Q2764 938 +A99AK 481 +A77JA 802 +38868 679 +4KK34 300 +66KK6 318 +AA626 674 +7738J 449 +JTJTJ 859 +T8TJ8 93 +96K99 574 +77339 349 +T7QAQ 534 +95384 342 +62226 950 +3JQA3 298 +9A9A7 488 +87JT7 559 +88688 518 +526KQ 188 +8K8K8 756 +7JJ77 490 +J3KAK 785 +J4733 153 +42T24 977 +2QQ5Q 280 +Q49A3 768 +98JT6 911 +T3333 193 +ATKTT 91 +2785Q 949 +QQQJQ 811 +T8K4Q 547 +K66KK 446 +22JK9 634 +224J2 776 +5QQQ4 614 +2T2KJ 755 +4Q7T9 979 +J3T77 974 +7J259 206 +A458K 627 +A89AJ 248 +JJJ99 893 +A3862 377 +4J4AJ 40 +TA7T7 663 +4K4T6 664 +TT6TT 988 +J6583 126 +TA4TT 791 +44457 696 +T72JJ 269 +A22A2 553 +J7JJ7 654 +2J7J7 903 +795KA 191 +2445T 912 +9553Q 195 +KK74J 328 +55335 399 +A5Q2J 430 +9QJJ6 85 +5QTK7 954 +Q539J 92 +4JT6T 798 +TKJT9 608 +72722 735 +84438 186 +3J3K3 179 +3Q3Q3 882 +JQQKK 975 +66AA5 158 +72J57 849 +25KAA 751 +88T88 599 +J293K 120 +85KA8 294 +J8948 598 +8679Q 52 +A639K 591 +J679T 326 +T4774 816 +7QQ4J 208 +2JQ22 76 +QA6A6 379 +Q5JA8 708 +KJ693 479 +9K25A 148 +A4A72 277 +T2QQQ 573 +72777 214 +J222K 448 +T2T6J 259 +57A83 117 +JJ8K8 325 +JT92A 65 +53288 142 +A22AA 240 +8T772 586 +QQQ5Q 875 +Q89TT 901 diff --git a/2023/7/input_example.txt b/2023/7/input_example.txt new file mode 100644 index 0000000..e3500c3 --- /dev/null +++ b/2023/7/input_example.txt @@ -0,0 +1,5 @@ +32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483