From 50f1016372ce01e843af3369c13663de17cdeee1 Mon Sep 17 00:00:00 2001 From: xuu Date: Fri, 8 Dec 2023 12:11:44 -0700 Subject: [PATCH 1/2] chore: cleanup --- day07/input2.txt | 1000 ++++++++++++++++++++++++++++++++++++++++++++ day07/main.go | 146 ++++--- day07/main_test.go | 11 +- 3 files changed, 1091 insertions(+), 66 deletions(-) create mode 100644 day07/input2.txt diff --git a/day07/input2.txt b/day07/input2.txt new file mode 100644 index 0000000..f6011eb --- /dev/null +++ b/day07/input2.txt @@ -0,0 +1,1000 @@ +TQA26 14 +66656 612 +JK3JT 442 +A6QQA 214 +Q3777 131 +J2K8J 180 +J56KK 578 +33699 532 +999K4 605 +4J445 884 +59A5A 381 +86686 419 +7T5KJ 92 +47883 178 +749K3 2 +7K6J3 675 +8J22J 869 +47463 168 +2A5K3 597 +27332 465 +4AQ9K 236 +59TJ5 883 +5TTTQ 333 +A8QAJ 208 +JAJ95 380 +8688A 265 +34K62 525 +KT7J7 709 +A5A5A 890 +7347J 935 +6TQ57 973 +888K8 345 +J5456 33 +8T8T4 456 +3J942 88 +QJJJA 727 +75333 479 +K6622 369 +T468J 608 +835A7 107 +67AA7 390 +QTJ9Q 25 +Q56Q3 579 +7K245 227 +36857 327 +8J7K9 524 +7AAAJ 277 +JKK4K 352 +43974 576 +2QJ22 482 +99JJ9 484 +AJAQQ 341 +8222K 976 +JA57Q 84 +A593T 725 +9T494 581 +TKTTT 941 +KJ55A 439 +9444J 137 +4423T 504 +K9J96 944 +6K9JQ 656 +2Q34J 48 +8T87Q 679 +5J955 27 +Q3T4T 416 +J8888 972 +5Q298 257 +64463 937 +44K4J 708 +KQQQQ 622 +TJKTT 800 +9394J 63 +TJ639 660 +K4JAQ 119 +9838Q 788 +76K4A 38 +J86KA 768 +3AQ3A 617 +J2592 40 +QAK68 761 +98999 620 +J4T34 963 +2A979 483 +95JA8 336 +A33AA 54 +TTT33 518 +QT759 212 +A7A3A 999 +KJ6AK 485 +99339 921 +TJ3A4 968 +2TKTT 573 +28626 844 +9KK8K 79 +7QT7A 57 +66777 493 +JJ355 676 +AJAAA 176 +AAQA3 522 +T4KJ4 308 +375K3 658 +J7K75 864 +JA275 669 +76T45 507 +AJT92 975 +K5434 346 +942Q2 598 +JA33A 495 +3JKK4 39 +2AAJA 399 +6555T 296 +7962T 787 +KJK2K 764 +684JJ 998 +6JJ36 52 +64K66 838 +JA8K3 901 +3Q7Q3 774 +9JK99 666 +3TK6J 355 +KKJ25 503 +86658 584 +T4T42 268 +Q4238 530 +T632K 700 +TJTTJ 571 +T7Q38 747 +AJ6KQ 150 +2QT22 885 +KJA4A 784 +87667 859 +63994 570 +Q82A6 331 +TK9T6 124 +J5K5K 140 +TKAJA 211 +59AAA 683 +89J57 694 +TQTT9 122 +59996 715 +73TQA 397 +AJT3T 420 +85882 777 +AA4AT 175 +4K4K7 670 +8JA79 19 +QKKJ8 340 +9TTTA 243 +77QQ7 314 +22237 22 +656QK 911 +8K79A 847 +4K466 734 +77979 924 +4J848 949 +J72K4 388 +77JJ7 741 +77477 724 +8558K 444 +668K9 441 +448T4 820 +AQA7A 629 +Q93J3 732 +4344J 582 +JA2JA 623 +44422 735 +3444Q 652 +5TQQQ 929 +A5T6J 216 +855JK 808 +Q7QKK 887 +3J433 297 +QJQ77 742 +6JJ99 492 +2QA2J 717 +326AA 704 +JJJJJ 194 +3QQQJ 550 +4J424 445 +64A53 201 +9J5AA 718 +A8747 438 +33788 989 +3K399 255 +T4343 400 +444J4 382 +3J3J3 12 +J7552 228 +352Q8 812 +8A833 762 +89638 940 +42AQK 577 +6TT68 326 +AAQAA 102 +4A37A 647 +TAJQ7 991 +85585 395 +66366 406 +9Q9AQ 177 +K6K6K 785 +2T228 143 +K9Q4Q 109 +T7999 462 +28294 978 +2T3J7 515 +A2AJ6 886 +AATAA 508 +KJ73J 86 +7AQ26 376 +6K97T 163 +A4AA6 537 +JK555 726 +2TTK5 356 +98AQ8 601 +2JJ4K 771 +K2386 791 +55888 254 +555JJ 690 +799KK 378 +44A62 786 +4338K 246 +338J3 688 +Q39J2 139 +22J52 338 +6KAJ5 933 +95J98 332 +67K4J 317 +4K545 592 +9Q929 852 +68872 157 +59535 548 +AAKJK 615 +36K97 758 +TQQTT 394 +QQ8QQ 926 +Q5K9Q 75 +JJAQ6 637 +JTJ32 275 +22TQA 729 +KQK3K 203 +43J86 104 +A6663 11 +9QJT6 908 +AT3A6 430 +56245 827 +6Q334 604 +77T7T 841 +TQK68 668 +22A2A 773 +62TK6 892 +8K898 912 +Q3Q26 411 +59678 26 +43924 451 +AT3K3 952 +78788 16 +24229 71 +J947T 849 +5K5Q2 897 +92332 42 +TT3T4 32 +47995 686 +99T99 749 +K8QQQ 408 +82889 846 +A6A22 93 +4A76J 590 +62222 795 +TTTT8 316 +7QJ68 696 +K9J9J 913 +944JJ 135 +77757 722 +65J66 707 +J7838 4 +2T895 802 +2AAAA 325 +Q855Q 619 +95223 148 +87989 706 +K4A79 96 +22T86 60 +A5AAA 43 +Q6666 339 +54445 320 +88JJ8 262 +36AA9 461 +6J6JQ 121 +8K7T6 114 +9A863 377 +T7TA8 126 +JJJTT 415 +64JTK 289 +K39K3 757 +54K89 737 +73377 372 +T5389 365 +4949K 304 +87787 763 +KQ33K 367 +Q75TK 721 +999QQ 934 +46647 433 +88886 803 +7J2AK 888 +KKTKT 10 +222T2 440 +JA78A 239 +J6KKK 610 +AAA8A 166 +65J62 545 +57355 455 +5AQ43 240 +KKTKJ 59 +3J337 170 +J5AK4 872 +TTQQJ 811 +TTT7T 627 +Q8877 588 +T5368 337 +A4K3T 662 +9AAAT 136 +TJ5K5 402 +332A8 89 +T6T6T 280 +T6985 603 +966Q9 681 +88444 458 +27JJ8 473 +66669 5 +4JJ48 881 +44426 335 +88A8A 85 +A5A36 323 +7QQ57 984 +464T6 745 +636QA 155 +88TAQ 879 +62874 421 +535T5 50 +4Q444 470 +83432 501 +24444 586 +47774 539 +KJKJ9 53 +A4355 920 +QTKKK 290 +333A3 540 +TTTAT 965 +28622 611 +35QA9 387 +94J48 797 +699T8 123 +66266 422 +QQ44Q 919 +8J9KK 974 +Q9Q76 685 +66J66 945 +7ATTJ 334 +66KTK 904 +TT484 932 +52557 792 +2828J 995 +JJ9JK 635 +9745J 55 +22299 549 +92QJK 222 +QAA7J 531 +7A5J8 358 +22KK2 544 +27Q23 273 +AJ88A 602 +33A6A 459 +22T26 232 +4AATT 174 +T5342 743 +55255 979 +Q88QQ 284 +222A2 680 +3QQ33 554 +K2KQ2 950 +AAA6J 650 +5Q333 62 +7J574 37 +38KK8 360 +K2K2K 760 +4T9TK 463 +6Q67J 472 +66686 778 +44J4J 990 +Q3993 655 +77J27 389 +TTQ27 35 +277K8 446 +27J42 428 +KK4J4 798 +QTK6J 199 +4344A 457 +TQTKQ 977 +8833J 237 +99TJK 147 +6Q9Q4 313 +TTJKK 98 +33833 460 +83T3A 805 +TJJ22 754 +5TAJK 632 +T293Q 191 +55757 70 +2TT8T 851 +7Q575 61 +22K4K 828 +8T288 204 +A8AA8 928 +KJ68J 697 +45AAJ 213 +8QJ75 693 +23KKK 319 +Q2Q22 969 +224J4 821 +44Q33 583 +34KJ3 342 +4JA57 687 +88T2Q 354 +989A6 51 +AAQA9 631 +5A57J 311 +62266 77 +2A84A 132 +99JKK 234 +88899 776 +K3395 6 +968JA 830 +6J444 282 +QA55Q 251 +39K99 209 +4A69Q 790 +77K57 562 +T5QT5 115 +56455 134 +T3TQQ 283 +46A6A 413 +62T67 626 +5757A 371 +98A47 642 +2T653 813 +2888A 322 +TTQ8T 454 +93333 876 +28AAA 580 +QA63Q 196 +2Q5QK 184 +48TTJ 300 +9JT79 306 +77T77 930 +6K666 957 +6J696 417 +8J588 703 +666TJ 673 +QKKJ7 233 +577AA 286 +8J775 781 +939KK 866 +34444 558 +4A4AA 221 +523QT 630 +8K259 651 +2T34K 716 +6K2T5 8 +88T83 551 +Q34AK 142 +4444T 7 +JTKT2 621 +JQJJQ 288 +38344 599 +99292 74 +Q36AA 206 +52255 871 +33J92 188 +K27QA 318 +66268 513 +29QA6 613 +74J74 815 +5KK6K 895 +Q9J9Q 511 +6J33J 158 +QAQQA 903 +KTQQQ 305 +2T759 958 +A796K 108 +63266 568 +KTT7K 691 +Q5A5A 186 +47787 267 +6T6TQ 58 +56472 891 +K7953 648 +99A9A 486 +Q78K2 249 +99799 705 +2K62A 671 +6TTJT 392 +4T44T 878 +44KKK 410 +KTAQ7 423 +222Q2 244 +4J324 401 +63636 575 +9K54J 856 +K7K77 755 +2K222 160 +3J644 711 +3355J 259 +A9995 664 +43586 909 +A4744 574 +Q7337 309 +7QQ27 161 +J8575 374 +9K99K 636 +7TJT4 674 +79888 505 +K3AJ5 730 +K4QJK 427 +99TJ9 263 +6963T 905 +3K3J5 474 +TJ4J8 806 +5TA38 437 +866J6 807 +2T454 44 +63A4J 865 +879T8 527 +A5282 217 +53737 951 +44Q9Q 634 +TTJT9 210 +QK67K 192 +33A3A 915 +88Q8J 125 +QJ2QQ 256 +2888J 20 +2786T 391 +J27J7 938 +2AAT5 521 +544Q4 528 +A4645 30 +AKAA2 526 +44535 835 +KQ33Q 585 +66JJ6 818 +9979J 138 +TKKKK 661 +KA225 278 +Q8888 794 +K27QK 916 +83323 494 +39969 684 +53359 986 +77J73 299 +5TK9J 782 +222JJ 713 +3Q383 753 +46466 298 +QQQJJ 875 +Q2394 860 +QA5QA 988 +8K5K9 877 +7Q78Q 68 +AA6A6 923 +J565K 29 +52766 301 +8598Q 159 +KKK8K 824 +8KTQ5 49 +66486 41 +45459 842 +67676 223 +44283 971 +22T5T 836 +88857 164 +8K492 250 +939J9 900 +K5J9K 779 +5Q692 145 +8T88T 907 +55535 31 +8K822 609 +9TQK9 67 +6762J 435 +T5T5T 780 +4K442 152 +657J6 202 +44494 349 +78377 925 +32858 882 +276Q5 359 +49526 509 +2J623 918 +4222A 914 +TTTTJ 939 +255TQ 179 +KJ42T 898 +79268 404 +648K7 189 +6T7J7 488 +37Q63 538 +K366J 469 +KKK67 156 +998K6 242 +QQQ7Q 560 +33223 231 +9A999 167 +28228 961 +JJK2K 141 +KKK46 633 +KT3KA 845 +A2Q2A 667 +AA9AA 471 +A663A 76 +J966Q 996 +77J76 127 +74JJ7 748 +6QQQ7 303 +8AJ44 18 +J2J34 480 +A289Q 195 +8335Q 266 +QQ2Q2 385 +45K4J 870 +48JK6 1000 +T9949 294 +2585K 272 +7J77K 809 +J53KK 657 +AAAT5 347 +72J8K 476 +TQ77T 910 +29Q7T 767 +4TTTT 663 +642AA 292 +37QQQ 171 +86Q74 97 +96J88 34 +QQ7QJ 618 +77772 546 +99559 23 +AJ666 295 +AAKAK 964 +K8Q32 120 +8QAK5 563 +A9K9K 45 +3475K 362 +95999 739 +482TQ 36 +5Q555 452 +854Q3 894 +QTQQQ 561 +55755 99 +899AJ 235 +QAQ22 502 +75353 638 +K6A87 572 +J2KT7 366 +KA39K 144 +8AAAJ 409 +66753 766 +TT626 510 +AJ999 418 +42222 983 +5J577 994 +262KK 330 +99992 701 +4K334 825 +96J96 536 +774K7 229 +A9362 351 +KA8KJ 982 +J2552 594 +TTT2T 810 +KK24K 640 +23222 162 +K36Q7 321 +QQJTJ 519 +33384 606 +5A834 500 +Q35Q2 379 +A7888 434 +595J2 47 +2K7AA 728 +AA66J 9 +AKKKA 373 +53335 328 +447J4 514 +KT7K7 21 +785TT 253 +3J783 720 +7J2AQ 405 +56Q5Q 447 +Q647K 94 +28488 207 +694T2 714 +KJKKK 78 +8T866 130 +274Q5 672 +89992 312 +J2242 350 +QQ988 899 +3T3T3 464 +Q2226 752 +55222 639 +7T9T9 271 +Q8884 832 +2Q92Q 516 +46886 817 +77J7T 829 +AAA9J 80 +35J97 555 +358K7 692 +ATT6T 678 +K2465 698 +A2A59 46 +58T24 956 +8ATA2 804 +AAK3K 751 +28897 649 +QAQKQ 854 +7T776 353 +26226 426 +5566T 13 +8KKTK 857 +TT44J 512 +QA29K 172 +A3AAA 28 +6TTQT 993 +8TTJT 922 +44TT5 942 +K67QT 699 +AA77J 927 +J8JJJ 153 +4T474 73 +4QJ8T 475 +86386 796 +AA2QK 218 +86A4K 205 +78J84 481 +27K3T 260 +AAKTQ 641 +6QQ33 467 +3K333 279 +62888 889 +8Q388 185 +Q88QK 490 +9494J 24 +5T888 783 +36633 769 +8T484 258 +82878 987 +J3336 643 +TKKAK 276 +822AJ 429 +72727 917 +254JK 113 +58888 843 +AK537 624 +QQQJ8 659 +J3366 274 +ATTJK 468 +5KKK2 72 +AAAKA 533 +5525J 962 +Q6QJ6 165 +5K424 607 +94KK9 66 +43544 487 +JJ38T 712 +6K6KJ 90 +J7788 936 +75522 625 +K6KK9 955 +AJ7J7 765 +K7QTQ 407 +37333 837 +82J83 523 +2A526 543 +99TJJ 238 +33343 997 +44446 361 +8868K 432 +9J989 82 +49999 65 +5TQ78 966 +AAA48 534 +76J5J 946 +J9J25 187 +5T973 744 +KK562 816 +486A4 324 +569TQ 269 +3AAT4 343 +TQTTT 386 +T56A9 449 +Q844Q 535 +T8T25 281 +QJ5J5 858 +AKTK8 364 +33532 315 +92959 943 +999A2 198 +TAQ6J 770 +29295 616 +AKA7Q 833 +55A5A 896 +KK49A 396 +ATJ84 197 +73K87 383 +6A56A 478 +7KQT3 775 +49A93 110 +2928A 736 +5JK35 95 +53JQA 261 +TT399 443 +57779 287 +77296 220 +3T334 436 +54K87 756 +45455 393 +647J4 868 +5555J 498 +K877K 173 +646JJ 589 +2JJAJ 960 +7T5T6 293 +T3K3T 118 +TJA63 855 +7K7AK 403 +68754 614 +J5628 822 +J93TT 245 +JQJQ9 874 +32JT2 823 +7AA77 948 +AQTQA 56 +48986 252 +9K382 116 +TJ9T9 230 +Q7632 193 +998K9 750 +KK99K 491 +Q57J2 219 +T58T9 596 +66T6T 424 +QK354 953 +66652 863 +23J33 151 +7J337 653 +3J247 215 +A7KJ5 302 +A555K 654 +KQ9TT 363 +99Q49 370 +2JJ65 587 +7TAAJ 553 +KKK68 826 +77755 850 +J74T2 959 +2TQ2Q 954 +ATJ3Q 344 +45448 557 +5JJ5J 738 +T3278 595 +465AQ 520 +97Q82 665 +99779 183 +KQJAA 91 +KQTJT 746 +Q2542 453 +99J99 414 +39T99 368 +AA4AJ 906 +8TK28 529 +5A55J 819 +TT4T4 853 +JQAQQ 100 +57T4A 970 +AJAA3 834 +9JTT7 291 +9AK6A 902 +96J94 181 +3J944 593 +94KQ4 375 +KKKK2 556 +88J99 567 +JAAAJ 840 +9J9Q9 128 +74447 81 +28288 719 +32K88 225 +53535 87 +74555 224 +6972Q 628 +7JQJ8 893 +2T25J 190 +92226 552 +Q3QTQ 591 +A666A 814 +26247 103 +4KTKK 981 +J22K2 133 +3KKKK 873 +KJK7K 689 +6A6K2 985 +3AAAT 425 +22J27 839 +8888T 677 +6A666 569 +8887Q 497 +696Q3 799 +99646 695 +KKK5K 69 +3A332 106 +Q7TTT 831 +96622 566 +4JAA6 564 +22572 600 +92AKJ 931 +TA8AA 848 +73373 112 +6666T 182 +AJKJ7 645 +7A7AA 496 +T5555 740 +J22JJ 967 +24AAA 146 +956AQ 412 +66A9J 329 +46644 477 +A9939 105 +Q5Q5Q 702 +588Q5 384 +63AAJ 466 +254KA 947 +8KK88 565 +KQ29T 541 +36K66 759 +7JAQQ 431 +8QTT4 992 +3333J 731 +44447 793 +T355T 489 +39723 862 +828QQ 270 +96999 15 +333Q2 733 +T774T 559 +55K55 1 +9J955 264 +K2KQK 682 +QKQKQ 801 +222J2 247 +97J79 3 +42TT3 499 +2KQ69 129 +4764T 450 +72K2K 83 +3Q334 644 +TTT88 348 +2Q223 310 +J43A3 880 +J4229 64 +4QJ8A 17 +43434 285 +92222 772 +T59K6 448 +66674 248 +34334 241 +JK529 547 +T7TT3 517 +22QJQ 117 +96J6J 861 +QTJT4 101 +Q444J 307 +T2Q8K 357 +93T24 789 +T7K57 398 +2K94A 111 +9J942 200 +T7J8K 646 +55665 980 +56555 169 +79777 149 +82A93 710 +Q989Q 226 +J2T55 542 +763K2 723 +QQQJQ 867 +QQQQA 506 +J7777 154 \ No newline at end of file diff --git a/day07/main.go b/day07/main.go index f3d3697..d7d481f 100644 --- a/day07/main.go +++ b/day07/main.go @@ -26,7 +26,7 @@ func main() { } func run(scan *bufio.Scanner) (uint64, uint64) { - var game Game + var game1, game2 Game for scan.Scan() { var cards string @@ -37,15 +37,18 @@ func run(scan *bufio.Scanner) (uint64, uint64) { } fmt.Println("cards", cards, "bid", bid) - game.plays = append(game.plays, Play{bid, []rune(cards), &game}) + game1.Append(cards, bid) + game2.Append(cards, bid) } - game.cardOrder = getOrder(cardTypes1) - product1 := calcProduct(game) + game1.cardTypes = cardTypes1 + game1.cardOrder = getOrder(cardTypes1) + product1 := calcProduct(game1) - game.cardOrder = getOrder(cardTypes2) - game.wildCard = 'J' - product2 := calcProduct(game) + game2.cardTypes = cardTypes2 + game2.cardOrder = getOrder(cardTypes2) + game2.wildCard = 'J' + product2 := calcProduct(game2) return product1, product2 } @@ -53,7 +56,7 @@ func run(scan *bufio.Scanner) (uint64, uint64) { var cardTypes1 = []rune{'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'} var cardTypes2 = []rune{'A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J'} -func calcProduct(game Game) uint64 { +func calcProduct(game Game) uint64 { sort.Sort(game.plays) var product uint64 @@ -76,92 +79,108 @@ func getOrder(cardTypes []rune) map[rune]int { type Game struct { plays Plays cardOrder map[rune]int + + cardTypes []rune wildCard rune } +func (g *Game) Append(cards string, bid int) { + p := Play{bid: bid, hand: []rune(cards), game: g} + g.plays = append(g.plays, p) +} + type Play struct { bid int hand Hand + cardCounts map[rune]int + strength int + game *Game } type Hand []rune -func (h Play) HandType() string { - hc, _ := h.HighCard() - switch { - case h.IsFiveOfKind(): +func (h *Play) HandType() string { + hs := h.HandStrength() + kind := hs& 0xf00000 + hc := h.game.cardTypes[13-hs&0xf0000>>16] + switch kind { + case 0x700000: return "5K-" + string(hc) - case h.IsFourOfKind(): + case 0x600000: return "4K-" + string(hc) - case h.IsFullHouse(): + case 0x500000: return "FH-" + string(hc) - case h.IsThreeOfKind(): + case 0x400000: return "3K-" + string(hc) - case h.IsTwoPair(): + case 0x300000: return "2P-" + string(hc) - case h.IsOnePair(): + case 0x200000: return "1P-" + string(hc) - case h.IsHighCard(): + case 0x100000: return "HC-" + string(hc) } return "Uno" } -func (h Play) HandStrength() int { - _, v := h.HighCard() +func (p *Play) HandStrength() int { + _, v := p.HighCard() + switch { - case h.IsFiveOfKind(): - return 0x700000 | v - case h.IsFourOfKind(): - return 0x600000 | v - case h.IsFullHouse(): - return 0x500000 | v - case h.IsThreeOfKind(): - return 0x400000 | v - case h.IsTwoPair(): - return 0x300000 | v - case h.IsOnePair(): - return 0x200000 | v - case h.IsHighCard(): - return 0x100000 | v + case p.IsFiveOfKind(): + p.strength = 0x700000 | v + case p.IsFourOfKind(): + p.strength = 0x600000 | v + case p.IsFullHouse(): + p.strength = 0x500000 | v + case p.IsThreeOfKind(): + p.strength = 0x400000 | v + case p.IsTwoPair(): + p.strength = 0x300000 | v + case p.IsOnePair(): + p.strength = 0x200000 | v + case p.IsHighCard(): + p.strength = 0x100000 | v } - return 0 + return p.strength } func (h Play) IsFiveOfKind() bool { - _, _, _, _, has5 := h.game.hasSame(h.hand) + _, _, _, _, has5 := h.game.hasSame(h.cardCounts) return has5 } func (h Play) IsFourOfKind() bool { - _, _, _, has4, _ := h.game.hasSame(h.hand) + _, _, _, has4, _ := h.game.hasSame(h.cardCounts) return has4 } func (h Play) IsFullHouse() bool { - _, has2, has3, _, _ := h.game.hasSame(h.hand) + _, has2, has3, _, _ := h.game.hasSame(h.cardCounts) return has3 && has2 } func (h Play) IsThreeOfKind() bool { - has1, _, has3, _, _ := h.game.hasSame(h.hand) + has1, _, has3, _, _ := h.game.hasSame(h.cardCounts) return has3 && has1 } func (h Play) IsTwoPair() bool { - _, has2, has3, _, _ := h.game.hasSame(h.hand) - return !has3 && has2 && h.game.pairs(h.hand) == 2 + _, has2, has3, _, _ := h.game.hasSame(h.cardCounts) + return !has3 && has2 && h.game.pairs(h.cardCounts) == 2 } func (h Play) IsOnePair() bool { - _, has2, has3, _, _ := h.game.hasSame(h.hand) - return !has3 && has2 && h.game.pairs(h.hand) == 1 + _, has2, has3, _, _ := h.game.hasSame(h.cardCounts) + return !has3 && has2 && h.game.pairs(h.cardCounts) == 1 } func (h Play) IsHighCard() bool { - has1, has2, has3, has4, _ := h.game.hasSame(h.hand) + has1, has2, has3, has4, _ := h.game.hasSame(h.cardCounts) return has1 && !has2 && !has3 && !has4 } -func (h Play) HighCard() (rune, int) { +func (h *Play) HighCard() (rune, int) { + if h.cardCounts == nil { + h.generateCounts() + } + var i int pairs := make(Pairs, 5) - cnt := h.game.Counts(h.hand) - for r, c := range cnt { + for r, c := range h.cardCounts { pairs[i].c = c pairs[i].r = r pairs[i].o = h.game.cardOrder[r] @@ -204,29 +223,32 @@ func (p Plays) Len() int { return len(p) } func (p Plays) Less(i, j int) bool { return p[i].HandStrength() < p[j].HandStrength() } func (p Plays) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -func (g *Game) Counts(cards []rune) map[rune]int { - m := make(map[rune]int, len(g.cardOrder)) - for _, c := range cards { - m[c]++ +func (p *Play) generateCounts() { + cardOrder := p.game.cardOrder + wildCard := p.game.wildCard + + p.cardCounts = make(map[rune]int, len(cardOrder)) + for _, c := range p.hand { + p.cardCounts[c]++ } - if g.wildCard != 0 && m[g.wildCard] > 0 { + + if wildCard != 0 && p.cardCounts[wildCard] > 0 { var maxK rune var maxV int - for k, v := range m { - if k != g.wildCard && v > maxV { + for k, v := range p.cardCounts { + if k != wildCard && v > maxV { maxK, maxV = k, v } } + if maxK != 0 { - m[maxK] += m[g.wildCard] - delete(m, g.wildCard) + p.cardCounts[maxK] += p.cardCounts[wildCard] + delete(p.cardCounts, wildCard) } } - return m } -func (g *Game) hasSame(cards []rune) (has1, has2, has3, has4, has5 bool) { - cnt := g.Counts(cards) - for _, c := range cnt { +func (g *Game) hasSame(counts map[rune]int) (has1, has2, has3, has4, has5 bool) { + for _, c := range counts { switch c { case 1: has1 = true @@ -246,9 +268,9 @@ func (g *Game) hasSame(cards []rune) (has1, has2, has3, has4, has5 bool) { } return } -func (g *Game) pairs(cards []rune) int { +func (g *Game) pairs(counts map[rune]int) int { pairs := 0 - for _, n := range g.Counts(cards) { + for _, n := range counts { if n == 2 { pairs++ } diff --git a/day07/main_test.go b/day07/main_test.go index 19f84ee..6f0526e 100644 --- a/day07/main_test.go +++ b/day07/main_test.go @@ -26,18 +26,21 @@ func TestHands(t *testing.T) { var game Game game.cardOrder = getOrder(cardTypes1) - h := Play{0, []rune("AAA23"), &game} + h := Play{hand: []rune("AAA23"), game: &game} + // h.generateCounts() is.Equal(h.HandType(), "3K-A") - h = Play{0, []rune("JJJJJ"), &game} + h = Play{hand: []rune("JJJJJ"), game:&game} + h.generateCounts() + is.Equal(h.HandType(), "5K-J") is.Equal(fmt.Sprintf("%x", h.HandStrength()), "7aaaaa") - h = Play{0, []rune("KKKKJ"), &game} + h = Play{hand: []rune("KKKKJ"), game: &game} is.Equal(h.HandType(), "4K-K") is.Equal(fmt.Sprintf("%x", h.HandStrength()), "6cccca") - h = Play{0, []rune("QQQJA"), &game} + h = Play{hand: []rune("QQQJA"), game: &game} is.Equal(h.HandType(), "3K-Q") is.Equal(fmt.Sprintf("%x", h.HandStrength()), "4bbbad") } -- 2.45.1 From f1ac3ea35f8a64761ef62f6101ea63950bed3660 Mon Sep 17 00:00:00 2001 From: xuu Date: Tue, 26 Dec 2023 13:17:07 -0700 Subject: [PATCH 2/2] chore: fix day 07 --- day07/main.go | 172 ++++++++------------ day07/main_test.go | 29 ++-- go.mod | 2 +- tools.go | 395 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 480 insertions(+), 118 deletions(-) create mode 100644 tools.go diff --git a/day07/main.go b/day07/main.go index d7d481f..c8db8f8 100644 --- a/day07/main.go +++ b/day07/main.go @@ -3,30 +3,20 @@ package main import ( "bufio" "fmt" - "os" "sort" + + aoc "go.sour.is/advent-of-code" ) -func main() { - if len(os.Args) != 2 { - fmt.Fprintln(os.Stderr, "Usage: day07 FILE") - } +func main() { aoc.MustResult(aoc.Runner(run)) } - input, err := os.Open(os.Args[1]) - if err != nil { - fmt.Fprintln(os.Stderr, err) - } - - scan := bufio.NewScanner(input) - - score1, score2 := run(scan) - - fmt.Println("score 1", score1) - fmt.Println("score 2", score2) +type result struct { + valuePT1 uint64 + valuePT2 uint64 } -func run(scan *bufio.Scanner) (uint64, uint64) { - var game1, game2 Game +func run(scan *bufio.Scanner) (result, error) { + var game Game for scan.Scan() { var cards string @@ -37,26 +27,23 @@ func run(scan *bufio.Scanner) (uint64, uint64) { } fmt.Println("cards", cards, "bid", bid) - game1.Append(cards, bid) - game2.Append(cards, bid) + game.plays = append(game.plays, Play{bid, []rune(cards), &game}) } - game1.cardTypes = cardTypes1 - game1.cardOrder = getOrder(cardTypes1) - product1 := calcProduct(game1) + game.cardOrder = getOrder(cardTypes1) + product1 := calcProduct(game) - game2.cardTypes = cardTypes2 - game2.cardOrder = getOrder(cardTypes2) - game2.wildCard = 'J' - product2 := calcProduct(game2) + game.cardOrder = getOrder(cardTypes2) + game.wildCard = 'J' + product2 := calcProduct(game) - return product1, product2 + return result{product1, product2}, nil } var cardTypes1 = []rune{'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'} var cardTypes2 = []rune{'A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J'} -func calcProduct(game Game) uint64 { +func calcProduct(game Game) uint64 { sort.Sort(game.plays) var product uint64 @@ -79,108 +66,92 @@ func getOrder(cardTypes []rune) map[rune]int { type Game struct { plays Plays cardOrder map[rune]int - - cardTypes []rune wildCard rune } -func (g *Game) Append(cards string, bid int) { - p := Play{bid: bid, hand: []rune(cards), game: g} - g.plays = append(g.plays, p) -} - type Play struct { bid int hand Hand - cardCounts map[rune]int - strength int - game *Game } type Hand []rune -func (h *Play) HandType() string { - hs := h.HandStrength() - kind := hs& 0xf00000 - hc := h.game.cardTypes[13-hs&0xf0000>>16] - switch kind { - case 0x700000: +func (h Play) HandType() string { + hc, _ := h.HighCard() + switch { + case h.IsFiveOfKind(): return "5K-" + string(hc) - case 0x600000: + case h.IsFourOfKind(): return "4K-" + string(hc) - case 0x500000: + case h.IsFullHouse(): return "FH-" + string(hc) - case 0x400000: + case h.IsThreeOfKind(): return "3K-" + string(hc) - case 0x300000: + case h.IsTwoPair(): return "2P-" + string(hc) - case 0x200000: + case h.IsOnePair(): return "1P-" + string(hc) - case 0x100000: + case h.IsHighCard(): return "HC-" + string(hc) } return "Uno" } -func (p *Play) HandStrength() int { - _, v := p.HighCard() - +func (h Play) HandStrength() int { + _, v := h.HighCard() switch { - case p.IsFiveOfKind(): - p.strength = 0x700000 | v - case p.IsFourOfKind(): - p.strength = 0x600000 | v - case p.IsFullHouse(): - p.strength = 0x500000 | v - case p.IsThreeOfKind(): - p.strength = 0x400000 | v - case p.IsTwoPair(): - p.strength = 0x300000 | v - case p.IsOnePair(): - p.strength = 0x200000 | v - case p.IsHighCard(): - p.strength = 0x100000 | v + case h.IsFiveOfKind(): + return 0x700000 | v + case h.IsFourOfKind(): + return 0x600000 | v + case h.IsFullHouse(): + return 0x500000 | v + case h.IsThreeOfKind(): + return 0x400000 | v + case h.IsTwoPair(): + return 0x300000 | v + case h.IsOnePair(): + return 0x200000 | v + case h.IsHighCard(): + return 0x100000 | v } - return p.strength + return 0 } func (h Play) IsFiveOfKind() bool { - _, _, _, _, has5 := h.game.hasSame(h.cardCounts) + _, _, _, _, has5 := h.game.hasSame(h.hand) return has5 } func (h Play) IsFourOfKind() bool { - _, _, _, has4, _ := h.game.hasSame(h.cardCounts) + _, _, _, has4, _ := h.game.hasSame(h.hand) return has4 } func (h Play) IsFullHouse() bool { - _, has2, has3, _, _ := h.game.hasSame(h.cardCounts) + _, has2, has3, _, _ := h.game.hasSame(h.hand) return has3 && has2 } func (h Play) IsThreeOfKind() bool { - has1, _, has3, _, _ := h.game.hasSame(h.cardCounts) + has1, _, has3, _, _ := h.game.hasSame(h.hand) return has3 && has1 } func (h Play) IsTwoPair() bool { - _, has2, has3, _, _ := h.game.hasSame(h.cardCounts) - return !has3 && has2 && h.game.pairs(h.cardCounts) == 2 + _, has2, has3, _, _ := h.game.hasSame(h.hand) + return !has3 && has2 && h.game.pairs(h.hand) == 2 } func (h Play) IsOnePair() bool { - _, has2, has3, _, _ := h.game.hasSame(h.cardCounts) - return !has3 && has2 && h.game.pairs(h.cardCounts) == 1 + _, has2, has3, _, _ := h.game.hasSame(h.hand) + return !has3 && has2 && h.game.pairs(h.hand) == 1 } func (h Play) IsHighCard() bool { - has1, has2, has3, has4, _ := h.game.hasSame(h.cardCounts) + has1, has2, has3, has4, _ := h.game.hasSame(h.hand) return has1 && !has2 && !has3 && !has4 } -func (h *Play) HighCard() (rune, int) { - if h.cardCounts == nil { - h.generateCounts() - } - +func (h Play) HighCard() (rune, int) { var i int pairs := make(Pairs, 5) - for r, c := range h.cardCounts { + cnt := h.game.Counts(h.hand) + for r, c := range cnt { pairs[i].c = c pairs[i].r = r pairs[i].o = h.game.cardOrder[r] @@ -223,32 +194,29 @@ func (p Plays) Len() int { return len(p) } func (p Plays) Less(i, j int) bool { return p[i].HandStrength() < p[j].HandStrength() } func (p Plays) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -func (p *Play) generateCounts() { - cardOrder := p.game.cardOrder - wildCard := p.game.wildCard - - p.cardCounts = make(map[rune]int, len(cardOrder)) - for _, c := range p.hand { - p.cardCounts[c]++ +func (g *Game) Counts(cards []rune) map[rune]int { + m := make(map[rune]int, len(g.cardOrder)) + for _, c := range cards { + m[c]++ } - - if wildCard != 0 && p.cardCounts[wildCard] > 0 { + if g.wildCard != 0 && m[g.wildCard] > 0 { var maxK rune var maxV int - for k, v := range p.cardCounts { - if k != wildCard && v > maxV { + for k, v := range m { + if k != g.wildCard && v > maxV { maxK, maxV = k, v } } - if maxK != 0 { - p.cardCounts[maxK] += p.cardCounts[wildCard] - delete(p.cardCounts, wildCard) + m[maxK] += m[g.wildCard] + delete(m, g.wildCard) } } + return m } -func (g *Game) hasSame(counts map[rune]int) (has1, has2, has3, has4, has5 bool) { - for _, c := range counts { +func (g *Game) hasSame(cards []rune) (has1, has2, has3, has4, has5 bool) { + cnt := g.Counts(cards) + for _, c := range cnt { switch c { case 1: has1 = true @@ -268,9 +236,9 @@ func (g *Game) hasSame(counts map[rune]int) (has1, has2, has3, has4, has5 bool) } return } -func (g *Game) pairs(counts map[rune]int) int { +func (g *Game) pairs(cards []rune) int { pairs := 0 - for _, n := range counts { + for _, n := range g.Counts(cards) { if n == 2 { pairs++ } diff --git a/day07/main_test.go b/day07/main_test.go index 6f0526e..a17aec4 100644 --- a/day07/main_test.go +++ b/day07/main_test.go @@ -26,21 +26,18 @@ func TestHands(t *testing.T) { var game Game game.cardOrder = getOrder(cardTypes1) - h := Play{hand: []rune("AAA23"), game: &game} - // h.generateCounts() + h := Play{0, []rune("AAA23"), &game} is.Equal(h.HandType(), "3K-A") - h = Play{hand: []rune("JJJJJ"), game:&game} - h.generateCounts() - + h = Play{0, []rune("JJJJJ"), &game} is.Equal(h.HandType(), "5K-J") is.Equal(fmt.Sprintf("%x", h.HandStrength()), "7aaaaa") - h = Play{hand: []rune("KKKKJ"), game: &game} + h = Play{0, []rune("KKKKJ"), &game} is.Equal(h.HandType(), "4K-K") is.Equal(fmt.Sprintf("%x", h.HandStrength()), "6cccca") - h = Play{hand: []rune("QQQJA"), game: &game} + h = Play{0, []rune("QQQJA"), &game} is.Equal(h.HandType(), "3K-Q") is.Equal(fmt.Sprintf("%x", h.HandStrength()), "4bbbad") } @@ -57,19 +54,21 @@ func TestExample(t *testing.T) { is := is.New(t) scan := bufio.NewScanner(bytes.NewReader(example)) - score1, score2 := run(scan) - is.Equal(score1, uint64(6440)) - is.Equal(score2, uint64(5905)) + r, err := run(scan) + is.NoErr(err) + is.Equal(r.valuePT1, uint64(6440)) + is.Equal(r.valuePT2, uint64(5905)) } func TestSolution(t *testing.T) { is := is.New(t) scan := bufio.NewScanner(bytes.NewReader(input)) - score1, score2 := run(scan) - t.Log("score1", score1) - is.Equal(score1, uint64(248559379)) + r, err := run(scan) + is.NoErr(err) + t.Log("score1", r.valuePT1) + is.Equal(r.valuePT1, uint64(248559379)) - t.Log("score2", score2) - is.Equal(score2, uint64(249631254)) + t.Log("score2", r.valuePT2) + is.Equal(r.valuePT2, uint64(249631254)) } diff --git a/go.mod b/go.mod index b8b60b4..4ed7424 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module go.sour.is/advent-of-code-2023 +module go.sour.is/advent-of-code go 1.21.3 diff --git a/tools.go b/tools.go new file mode 100644 index 0000000..367d79a --- /dev/null +++ b/tools.go @@ -0,0 +1,395 @@ +package aoc + +import ( + "bufio" + "cmp" + "fmt" + "os" + "path/filepath" + "sort" + "strconv" + "strings" +) + +func Runner[R any, F func(*bufio.Scanner) (R, error)](run F) (R, error) { + if len(os.Args) != 2 { + Log("Usage:", filepath.Base(os.Args[0]), "FILE") + os.Exit(22) + } + + input, err := os.Open(os.Args[1]) + if err != nil { + Log(err) + os.Exit(1) + } + + scan := bufio.NewScanner(input) + return run(scan) +} + +func MustResult[T any](result T, err error) { + if err != nil { + fmt.Println("ERR", err) + os.Exit(1) + } + + Log("result", result) +} + +func Log(v ...any) { fmt.Fprintln(os.Stderr, v...) } +func Logf(format string, v ...any) { + if !strings.HasSuffix(format, "\n") { + format += "\n" + } + fmt.Fprintf(os.Stderr, format, v...) +} + +func Reverse[T any](arr []T) []T { + for i := 0; i < len(arr)/2; i++ { + arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i] + } + return arr +} + +type uinteger interface { + uint | uint8 | uint16 | uint32 | uint64 +} +type sinteger interface { + int | int8 | int16 | int32 | int64 +} +type integer interface { + sinteger | uinteger +} + +// type float interface { +// complex64 | complex128 | float32 | float64 +// } +// type number interface{ integer | float } + +// greatest common divisor (GCD) via Euclidean algorithm +func GCD[T integer](a, b T) T { + for b != 0 { + t := b + b = a % b + a = t + } + return a +} + +// find Least Common Multiple (LCM) via GCD +func LCM[T integer](integers ...T) T { + if len(integers) == 0 { + return 0 + } + if len(integers) == 1 { + return integers[0] + } + + a, b := integers[0], integers[1] + result := a * b / GCD(a, b) + + for _, c := range integers[2:] { + result = LCM(result, c) + } + + return result +} + +func ReadStringToInts(fields []string) []int { + return SliceMap(Atoi, fields...) +} + +type Node[T any] struct { + value T + pos int + left *Node[T] +} + +func (n *Node[T]) add(a *Node[T]) *Node[T] { + if a == nil { + return n + } + + if n == nil { + return a + } + + n.left = a + return a +} + +func (n *Node[T]) Value() (value T, ok bool) { + if n == nil { + return + } + return n.value, true +} + +func (n *Node[T]) Position() int { + if n == nil { + return -1 + } + return n.pos +} +func (n *Node[T]) SetPosition(i int) { + if n == nil { + return + } + n.pos = i +} +func (n *Node[T]) Next() *Node[T] { + if n == nil { + return nil + } + return n.left +} + +func (n *Node[T]) String() string { + if n == nil { + return "EOL" + } + return fmt.Sprintf("node %v", n.value) +} + +type List[T any] struct { + head *Node[T] + n *Node[T] + p map[int]*Node[T] +} + +func NewList[T any](a *Node[T]) *List[T] { + lis := &List[T]{ + head: a, + n: a, + p: make(map[int]*Node[T]), + } + lis.add(a) + + return lis +} +func (l *List[T]) Add(value T, pos int) { + a := &Node[T]{value: value, pos: pos} + l.add(a) +} +func (l *List[T]) add(a *Node[T]) { + if l.head == nil { + l.head = a + } + if a == nil { + return + } + + l.n = l.n.add(a) + l.p[a.pos] = a +} +func (l *List[T]) Get(pos int) *Node[T] { + return l.p[pos] +} +func (l *List[T]) GetN(pos ...int) []*Node[T] { + lis := make([]*Node[T], len(pos)) + for i, p := range pos { + lis[i] = l.p[p] + } + return lis +} +func (l *List[T]) Head() *Node[T] { + return l.head +} + +func SliceMap[T, U any](fn func(T) U, in ...T) []U { + lis := make([]U, len(in)) + for i := range lis { + lis[i] = fn(in[i]) + } + return lis +} +func SliceIMap[T, U any](fn func(int, T) U, in ...T) []U { + lis := make([]U, len(in)) + for i := range lis { + lis[i] = fn(i, in[i]) + } + return lis +} + +func Atoi(s string) int { + i, _ := strconv.Atoi(s) + return i +} + +func Repeat[T any](s T, i int) []T { + lis := make([]T, i) + for i := range lis { + lis[i] = s + } + return lis +} + +func Sum[T integer](arr ...T) T { + var acc T + for _, a := range arr { + acc += a + } + return acc +} +func SumFunc[T any, U integer](fn func(T) U, input ...T) U { + return Sum(SliceMap(fn, input...)...) +} +func SumIFunc[T any, U integer](fn func(int, T) U, input ...T) U { + return Sum(SliceIMap(fn, input...)...) +} + +func Power2(n int) int { + if n == 0 { + return 1 + } + p := 2 + for ; n > 1; n-- { + p *= 2 + } + return p +} + +func ABS(i int) int { + if i < 0 { + return -i + } + return i +} + +func Transpose[T any](matrix [][]T) [][]T { + rows, cols := len(matrix), len(matrix[0]) + + m := make([][]T, cols) + for i := range m { + m[i] = make([]T, rows) + } + + for i := 0; i < cols; i++ { + for j := 0; j < rows; j++ { + m[i][j] = matrix[j][i] + } + } + return m +} + +func Reduce[T, U any](fn func(int, T, U) U, u U, list ...T) U { + for i, t := range list { + u = fn(i, t, u) + } + return u +} + +func Max[T cmp.Ordered](a T, v ...T) T { + for _, b := range v { + if b > a { + a = b + } + } + return a +} +func Min[T cmp.Ordered](a T, v ...T) T { + for _, b := range v { + if b < a { + a = b + } + } + return a +} + +type PQElem[T any, I integer] struct { + Value T + Priority I +} +type PQList[T any, I integer] []PQElem[T, I] + +func (pq PQList[T, I]) Len() int { + return len(pq) +} +func (pq PQList[T, I]) Less(i int, j int) bool { + return pq[i].Priority < pq[j].Priority +} +func (pq PQList[T, I]) Swap(i int, j int) { + pq[i], pq[j] = pq[j], pq[i] +} + +var _ sort.Interface = (*PQList[rune, int])(nil) + +type PriorityQueue[T any, I integer] struct { + elem PQList[T, I] +} + +func (pq *PriorityQueue[T, I]) Enqueue(elem T, priority I) { + pq.elem = append(pq.elem, PQElem[T, I]{elem, priority}) + sort.Sort(pq.elem) +} +func (pq *PriorityQueue[T, I]) IsEmpty() bool { + return len(pq.elem) == 0 +} +func (pq *PriorityQueue[T, I]) Dequeue() (T, bool) { + var elem T + if pq.IsEmpty() { + return elem, false + } + + elem, pq.elem = pq.elem[0].Value, pq.elem[1:] + return elem, true +} + +type Vertex[V comparable, I integer] struct { + to V + score I +} +type graph[V comparable, I uinteger] struct { + adj map[V][]Vertex[V, I] +} + +func Graph[V comparable, I uinteger](size int) *graph[V, I] { + return &graph[V, I]{ + adj: make(map[V][]Vertex[V, I], size), + } +} +func (g *graph[V, I]) AddEdge(u, v V, w I) { + g.adj[u] = append(g.adj[u], Vertex[V, I]{to: v, score: w}) + g.adj[v] = append(g.adj[v], Vertex[V, I]{to: u, score: w}) +} +func (g *graph[V, I]) Dijkstra(m interface{Get()}, src V) map[V]I { + pq := PriorityQueue[V, I]{} + dist := make(map[V]I, len(g.adj)) + visited := make(map[V]bool, len(g.adj)) + var INF I + INF = ^INF + + pq.Enqueue(src, 0) + dist[src] = 0 + + for !pq.IsEmpty() { + u, _ := pq.Dequeue() + + if _, ok := visited[u]; ok { + continue + } + visited[u] = true + + for _, v := range g.adj[u] { + _, ok := visited[v.to] + var du, dv I + if d, inf := dist[u]; !inf { + du = INF + } else { + du = d + } + if d, inf := dist[v.to]; !inf { + dv = INF + } else { + dv = d + } + + if !ok && du+v.score < dv { + dist[v.to] = du + v.score + pq.Enqueue(v.to, du+v.score) + } + } + } + + return dist +} -- 2.45.1