diff --git a/aoc_test.go b/aoc_test.go index e12e7a9..b617447 100644 --- a/aoc_test.go +++ b/aoc_test.go @@ -78,3 +78,16 @@ func TestList(t *testing.T) { is.Equal(a, 5) } + +func TestGraph(t *testing.T) { + g := aoc.Graph[int, uint](7) + g.AddEdge(0, 1, 2) + g.AddEdge(0, 2, 6) + g.AddEdge(1, 3, 5) + g.AddEdge(2, 3, 8) + g.AddEdge(3, 4, 10) + g.AddEdge(3, 5, 15) + g.AddEdge(4, 6, 2) + g.AddEdge(5, 6, 6) + // g.Dijkstra(0) +} 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/day17/example.txt b/day17/example.txt new file mode 100644 index 0000000..3c85086 --- /dev/null +++ b/day17/example.txt @@ -0,0 +1,13 @@ +2413432311323 +3215453535623 +3255245654254 +3446585845452 +4546657867536 +1438598798454 +4457876987766 +3637877979653 +4654967986887 +4564679986453 +1224686865563 +2546548887735 +4322674655533 \ No newline at end of file diff --git a/day17/input.txt b/day17/input.txt new file mode 100644 index 0000000..04b77c2 --- /dev/null +++ b/day17/input.txt @@ -0,0 +1,141 @@ +222343122415245553243251316124223234134666332224124211645376775672443247264263475261513546522316162532215534453661231232515522214351143451211 +422125443253354142511545636246664666565355152246453231651172747635424163415626462753445445312334225221624336126244661514234445544425223533122 +131212112421423414125516356342555542626661163526211536677376523712262371162153644117776261645533664544366146314432523222515425314231311544521 +131334344235153415214541223454455223563154114267331524774136127715554416125273543276346471735741663351266324144422511534542234341513351345352 +314541545523112111131565114333211153312641614321771374726154434476767125613311254537363422511721275135434164664442151263234241114111121141334 +435153335153513534132112326653656626231351436457153446433363235565156156723233715473535244413451723176561153261562445512312413413513135513241 +235243535523134543445216214455445352435554624255171271225755313626173154112144673146273747537614771377613244145436355342536323123434425211113 +132552152551514515631323412533333446136243376366556365574745474652757277632216317677316551253673465513776336664225626313216456312542531241352 +311415311541314143614162642634556464574541744355655753776632763662153656721435137347422375344242225276244673434333351454464536453212424421425 +453351344131342126143165264626543453632747531417235617257564537542611276314542732241535726557435722271523761452243145343543365433423153132344 +424354321333252642533513531241416236636427325176724425161362563217173773414665312244155777552374513335344162564462411154332652126231133233331 +433224533551436134314641523151361154615353522622137441214134673454347752343853221756746664765556521762762464233333523123643145626644231443435 +243311313556533564523443331214721572253144611715156456321767676655457736255732864573164467712433421256325731135656541255561161224441152415141 +511211214252345356344156642436611545463224342452742717585325436325574336455336582448256836717741726221177352616311422112513255165441212221131 +244253443163544142511413446172632624762126756145146775348573476557644782564438437852836725866452746165474553473737534665123425113435563332453 +232155346345514313654314235557517112176174776332136456448427547478426265844263882743533766568854547675464477171376662424251312612162632514355 +424114441642564135264345176163444365144163124128832285838626688224834432458556377773645427653638562736676174576737574642156232566466636441124 +543244331213246512421551417132132647763461226245235583478358556478588445828784366325778587856624776125647332126411416416253326543252561355453 +513411632124243263566153173255453461117356682265475822242236525286334354628536868726586284535383867467645552277715213631624352612331534144225 +234513324414264131211345613416736552211115685767773523645885727834464346536233245827443237753783724626566246761647646475456131431415434413214 +132312323255146664114745115275443572754326543474673247542856435836784453584863735676475556838547655484787272216413226712176641452341133361621 +324445311115236332563161165371142354332783687877726543524347444677384655338335874866474624655543543466338566764524432633543363631622335214314 +452146561262254136627644671525514257523265444635773245326674352228844844533536247443486645863647324742823822411266212747246263222143515531123 +323443456261326564347757145611113632534754323645837762327643383533554795688333775647234446628482555868382485343642524356327477353226234131236 +323566623463524644662155345214734554448367587534458465333783778836737748873896436675564776876254472473382773351746477537426351164321556552155 +411624314463555226413151162214157826642348553426375774773937688356445778864665387877397647688288645648488526683635462551221145364643422311353 +632541423444624336526576464457777686224586376437338832948677343497666833369685846994446858346385546773453274862246234173462724342631561154224 +353552612514333121215745416727247272652423544685753368594647394695658845883548858535734584333432476287445274435882257755366724515216151451335 +532166314441574113623175262414284358754786632434337878589943898388748395969493754638344944356356855723827482463322173565111471477266643531656 +116644351623354144151425116542234543365536844644456554366885339853375594997998864974865377578863643354482658652565456654732562467462642222633 +425265565534525427323521564886284475834357765873953575557676864375449896735695696945686539933897587748627756658646775714547636543542445454453 +535223341313647443711526456446656284686644344893747447894898636684595668686646634379736944587367734766868686525582828566224141647377452234555 +363661154543263115262561162483434662346246798873478436638737549697754638834495375733735333645433574877375276735684662372751455123234415466135 +233425541263155564422777566657463284532222644739657858356574964435535676435594635788954898867344838753677578547362236357265747553176223645334 +516113313346251471245336736766463235445577574763999559496983357388779634999695985559489635746544658453383775634426265623615715313624324613363 +325162436525537742416463226466422582452664444569687695737766869564846698477563438574369658749388443473974527623665566388541124766765465414216 +246532424466452274271137247462733834328375758733755764656833968984956876697799766884997394499744585594587876625677723447244217267426213343664 +164356325132241765153442235356485242287535595375398869579646888545895757975449659944533834737579865398575662388572336243423321763313272155166 +563615555622724634724833866443433524894838958833799835363644977657658578544664475868679458747866349983337933858733558557637772576116524351364 +415635136113175137578553536586564768643559866697595733497557879468449998976698759797676436636689587877974934738258785642847435574452213713115 +611145153512463742256332755487858466583796497738685664885658748948569545664445888744947857868589779795933867483673776857358564657524755764663 +235565633417713142257732254688636564985938796475899556649457697557496557466787887559566976984578789695478656638728764846466352277661663613361 +621414363456434251274757622526573747939964946885796964745598945469487687577499977988878866574434454755956575894225366755752223145132155653231 +345131227736331632753528624737877993467633874573544566489488585879587798745544677645759486797999989373758553748378762622865225676664655255241 +211147374365347566347683538644229836375367399779966469889799674657475784657559849567645885568589949973844765649576234674566626355732735375222 +123377577423217135757464227844458664877347575684876765589445958984848645558957589999985649678755757487567997496322362828232445742466637677526 +664165716524474573468574845478963396633746335989586478477469677874488696466969855457589699479795777786993453995866655558838835473455711456363 +645354217411347423778226725287476938984748595969986859957845897656645797969945967774756979786597495489759753559534884668577746471267563715146 +641123731653617153523878786635997676839634584755798896844786989789997886975976877949768456676679545757849946967975863276534664727276625714772 +115172313432542554343534322666684547383747359496846665858866986576677885977798965677465955688547658777937483437698822255673227856674321437423 +412316124234335666538374784464654983653484498444688865778477997658875656978599866558669858496479969969883747883595375635748886535531163731666 +126215534552633672772444476653659865338587448788645985679486758768567978966789759768556688875958549895757653789978826784367383554244325537527 +252471722164728674273366346869733763887789597776444684764998985757556667899857586887855996664484555498483585754587755247628423684671244526734 +616643241174113524686752733973994433637977478579974486858559967566675596878985789668957757945476696698659695977366876442282382768473657366141 +433715625621658365787576378559966694694594565764577974766866977599956779776859687775695787565694987685963787977966448785772368444344613132341 +542654365337336826434765643879738646353964647955974749776876686976656857777555965796959755779797949695874357763646457888473355582731254116326 +461216754736354442526774233456439439344649448885449769679668978899596596779679669865776767595846454969558697477357393642534444663773547743151 +242515737157657333842228548349875463457585648494866999577597668698587656558567788999587689857767876885764484449548964773473277753231345166735 +654521345226446835568566235799768737539786475876888887965595966585598966967969975867759665599758868667576445864433979732538253832712175534524 +115512724255847238822523769738898864344596868468594857859588665686578579798878998656799555798598889485694534558457599364226342568753677472213 +111524524557584843753648338688446565566879945758747566968678858898688798787995576956865896565768766698768659698554344826255288878683415552615 +775523534424885336584535598468769386864885649976596679699998877966986778776767559667869889767766556949956568378399476985828735885425764761532 +617234235723887272453554398456849533986998847969886896697596899979688786876777696866697569685659688446949473894494359678374775655237573674227 +731315731775542663287374348536374735499748575465485866966768699899697997797776666566989778755777686999847946948853933693544267753841326646546 +722426246522232587267567746363433367778759685487558796666966958776698867986668698666876866775886659846776955633755765593638285378256357434712 +174725351666826552875547497865663863549888789874668876958895676696978768897788786788566955869997548858768874638777786742243328886625132571731 +611354237262464432722652894576534365759676599966965877965957687768769888968769696957679665987557457444944459839769348686383583662783465636276 +435775645554577458385385799454458446877466964957585556758766679978876698869886969697899756896864788684659949467367797467452464686586746747527 +326715157438852348356648795697795987947557758498877979757678677886679987996677668886868888557795864545877549896579885386328386465786533157726 +453273273424255488663856445483976736667859995777897779896656697978997888787987898778776958957796758748894869487957534497554376546868722122324 +224573216435873475783673336556553645879964994546857977556976898696968678989979687688777875655579994576659674597887987885384834758677414425113 +356241135726344637684746374354974769468887978766569659956588999669799779786678667886579699675678884889486587434679643859352582448655434457222 +466565323122567376356874397989745956794895966548765989989778688799697678896789997978696969665976575677558444868875986457762338452225134143337 +362264215456463347238855743779549969969487765646768855768895876877976886666866799968989767688568764586578687569679978449782326532643416155546 +671362724447463788282378857469553747694544454845755975585656699878677799678797776968799697957896898878765444867363549353272748888443452166364 +632542654733754847425787585687488755596748847964799999775985798869786789699666867675565568797969676665954954838875438688564548862244145146171 +333723417316447367476286584684499973896998895898557855668598997699996977697796768788767776689759949696664864687338847464274357245662617625121 +732722335132788573566373398994377893654688989578766978658865988697879899789898877758688978567997675756877983739477395738266566365866553224517 +147177514747474242267782783587784747967985778647866665897959877679967669667689876857669669767556998597597497483549887695587764775634662343617 +152744472376652374322667687839455953897495766449899756875956679668789688868676978978876787578985556849965685833354883453248267842384616432657 +217227543612352346346455856939488455758985798868598588765697888677977868879697796686766696678556576857569777547694747435272652536672167123712 +744625215616674868776537785459394998859647888485449778786556769796876987889777989756758976977758496584958578978633843394583557326773745211217 +353354332722486863626455784779737558677796677557648786868756998567779997999757769989968679778794444858495938377534839655447657354377534537617 +351162251452745473452566496983484689397949849568679798775655588857855767769795678968695766898846549555848585493436735966444566758545364773274 +546334341147183722785558798496869978864895677895756565879578585766957665575757696669676677769768545948765446463585354842584745473612425647573 +235615763364478535767825263879546994497978874484569465769576969857869669765659777668868977865946966479776587987999955433355672374557554754214 +162177325222163583735436483646438894396659587698644456886655596959879587966867768858997789444759867989565846639353845278562446263633245724416 +324535255452483383635664425784958857579867676497445879975595969977679879855779697798889657664476857564676937793853344868547244368834643131142 +121347276513238722442368557665693779499584456766988769868757758795788956896585869778678687556788794995887699638544336543625582864246234111541 +357771157266317334828437783465549356369747964978875778866775965679685977988796799568586768676664767564576835757749675276226376757244531177246 +336664616327762746632336856333359783983467896468668468658857596888666655696975568657694759566847599666396674475446532283582787232143364371132 +644533212754142233635788744897443835653636699755644785889878558985567878858696858779788657475547995487649399794638387758824355331366214575725 +222217676717612667687783367437478679946969645869469768494864887557985785659965856994457654994548995554598868743539788752855264561637271324324 +356452154463414577848785374339498366535638544946846765677495769785557579699659788879447959897745467466459849543743454246822537753427335371562 +343351256333451586862433643627656985753774749546555768998795848687576559887698545798748988498774696693748665338953758884323743215516455337651 +511156433162323673647444877388393898578477588665844949845684789786485757669794987664595874795599974455755347864958728824643583546532455523751 +156124537652656126446766564883685565845949468648655489495444579455874554846945778967786444756587898964737436487344256846452377111452125151424 +515264356113762652852852632686537484556764597765774578755556886965448899978985694964457598494775673883444869546887836668355228143455736361562 +263512646525146141226625365446528679473694599878945468469659775468694555669548954648468546665655847884688365888823562232347546127534712221733 +631415241711525626678448673422265459959576695493999586674865475767744965494946497644686744965594334353963643655244478563353652375272512755651 +233341711151632262167275682837682337594668593834657957999678754665457846558698875995554875885564465558544593484682334467458266753354353656363 +222614621174264456548568855775763264556684446457483755666594465554896878549965797747587445795345639937745754468637877328233727712145444373316 +551136576466563613374865837744634548656596546959875864946578986489646467467887584454698786369755639588797883757583882864432762672266561742114 +311433545766254471655428367633528758869667674337353886355568568977559794557448545869856978986385835684993879575657778365536445625371464633155 +223612464456771261577683747465843843667648368983574634838577897765496687876598844667977633334687854753563787465237347324763236171472235426346 +411245121677351625555347323585443444334598696449339998397379788744556985684748696646376458494657966586849537253887746577841254462522431254352 +344346233632636772414765673728476885836598589955558748979779588656646877576889768598337348786569379345443684386546372358847445714773517622512 +155323536362462251246768675874228887274856495665585569666835536867574987794867838438565638693854398377677783434776265675564455312663443423541 +331566446775752674417132753235465665833378447847639386878845593445734945386694688999635777683567968539426228468486664337643254264256612362121 +243564654224225645722672382353864676463257496747548887493585987546678658735985743376384577953533946647357564447863573451724364472125432653566 +342315614225414552353574355435638346877654437484463854837898965395385547754648364866393337683989446882326338262227448314144441743477561422165 +513316635431624236671443348277886572254832458877847668477593794644585888563857755634797589885668862754772785358487877315135613177367144545513 +463143244643262172563515774358878556748352525757567987986693359853994963575439443448643467386743366524235883462535432614464455735443452664134 +365641135114444646623465131227245648484522527682468766639838853356859443957586854538395483886846572325432336737857272623363152162131551135414 +416313341334271754356764361744638674522648825426627539586358437534848469663986377869797354638474474353788632454247175546246417444136416435644 +224353155431264253763746646441542886447853826453463553364576374757797585985553484899859547773742768633328427884282476754133441524461616242643 +141663652631255115673535171111246265842334873322667887864398959494389595436394448689564435578232373254536888873517235136312276614212451451513 +124131511566566612435515461237554545678463532355284383577958338477334574565899548354995646534573786226335788675417311262514713363312364535154 +423124641651141434731445642547213258886735546328578275442562699767675575457466479427885838355373878586875834224154546773247146645646322335264 +412644326632526437735316412561445417468882886666655743427476665574794946797787288666754744722668682487625266217525212752446411411125122652443 +533453646465125514654252753512273714342227373732733663723654553522224855828427587263828376724486786422622551345652174733731235533661345526462 +431416626556561415256423726364711356216462533453778624228776264745488556248346558664685886255267664468734267241413666377354236365315112133322 +115242144546551242654657115464153653733572255453735867582743877872748475375858445224247557346684865447435434415136572343667245436453541641214 +211445545143351463615353774344215444543156828652686467572385233427763332873666832447256386848435536242337137777716372765516254142315334335233 +345124663224645321264314114673647256557756426376735374628552375257437773764536467386624358238642865332573246663566211211655655563524242452555 +314351264524343426135362454521352527263736457447533445374485277766767738682474542365754582457568722614424111744153725635141565436222616112315 +525142531331665442641251651451342365334554162254374267466577736465886625773232626738363773743786773241666727445735231415436342625513255235151 +543245142256442454155334231734217734761146755176638778856523266374354672783363252424683478557527343237227211135754547531342136551132351221533 +241355115114515145351235241342577311663523713245571552423844478483444456674576776374774635321762611146175633416417526461446534624524525232351 +413212231435125464562416642131514721163172662635774761555772466528432746222877628357558463622434577676277711461224526611415645224221334321153 +424114552242412235261232556412366663124614547557116436567672766285253724382874567666431356536715216512355113511563132462412461555112225422135 +322324544333135432235125224253374616723165212337472223432464246434784434268261665524555163346167575177573432151165141342526636661664333152245 +533322454514225135644666511564116113743567665217446774461763723457213436114546254524754413534661176555531123216654634336153535555615455221351 +345114245113253556125335351622625171466165175642167753751336711273734343723253343654164414153211641676412352766561245653215262351455523141451 +142421541514212154334545211465656444644552463442357616615277117635256363621723512341776351424463622726131565431516465664346151153212233245553 +444314343434312135211364665242441535527647475416223551347736416724264751235567161762631557147415445546211321423221136662344161541525343455425 +513343214231252255165411335621632642146147645756642561211667322537371252122672641421372561555336143123644446534414131135624333443543522413531 +151133414553422411155141354122545416221132657162133367556711163132611352466177573117122527412137673543111622364234433423115531533515415532534 +344341433553215433332561124312211661162315615567541645475225431414323527474442612165214546553775576346352241352363416414626442233143123431324 +312334123334252355135521123323245125516132154636463653576675247514555746177477533534522123547665145656414436131454466514531422525141331441324 +221234522113214232242154165523542235561622643243233434644217742623556626525264626677654256257462621433131425222466152534235213545112335523443 diff --git a/day17/main.go b/day17/main.go new file mode 100644 index 0000000..57e6617 --- /dev/null +++ b/day17/main.go @@ -0,0 +1,214 @@ +package main + +import ( + "bufio" + _ "embed" + "fmt" + "sort" + + aoc "go.sour.is/advent-of-code" +) + +// var log = aoc.Log + +func main() { aoc.MustResult(aoc.Runner(run)) } + +type result struct { + valuePT1 int + valuePT2 int +} + +func (r result) String() string { return fmt.Sprintf("%#v", r) } + +func run(scan *bufio.Scanner) (*result, error) { + var m Map + + for scan.Scan() { + text := scan.Text() + m = append(m, []rune(text)) + } + + result := result{} + result.valuePT1 = search(m, 1, 3) + result.valuePT2 = search(m, 4, 10) + + return &result, nil +} + +var ( + ZERO = point{0, 0} + + UP = point{-1, 0} + DN = point{1, 0} + LF = point{0, -1} + RT = point{0, 1} + + INF = int(^uint(0) >> 1) +) + +type Map [][]rune + +func (m *Map) Get(p point) (point, int, bool) { + if !m.Valid(p) { + return [2]int{0, 0}, 0, false + } + + return p, int((*m)[p[0]][p[1]] - '0'), true +} +func (m *Map) GetNeighbor(p point, d point) (point, int, bool) { + return m.Get(p.add(d)) +} +func (m *Map) Size() (int, int) { + if m == nil || len(*m) == 0 { + return 0, 0 + } + return len(*m), len((*m)[0]) +} +func (m *Map) Neighbors(p point) []point { + var lis []point + for _, d := range []point{UP, DN, LF, RT} { + if p, _, ok := m.GetNeighbor(p, d); ok { + lis = append(lis, p) + } + } + return lis +} +func (m *Map) NeighborDirections(p point) []point { + var lis []point + for _, d := range []point{UP, DN, LF, RT} { + if m.Valid(p.add(d)) { + lis = append(lis, d) + } + } + return lis +} +func (m *Map) Valid(p point) bool { + rows, cols := m.Size() + return p[0] >= 0 && p[0] < rows && p[1] >= 0 && p[1] < cols +} + +type memo struct { + h int + s int + p point + d point +} + +func (memo) sort(a, b memo) bool { + if a.h != b.h { + return a.h < b.h + } + + if a.s != b.s { + return a.s < b.s + } + + if a.p != b.p { + return a.p.less(b.p) + } + + return a.d.less(b.d) +} + +type priorityQueue[T any, U []T] struct { + elems U + sort func(a, b T) bool +} + +func PriorityQueue[T any, U []T](sort func(a, b T) bool) *priorityQueue[T, U] { + return &priorityQueue[T, U]{sort: sort} +} +func (pq *priorityQueue[T, U]) Enqueue(elem T) { + pq.elems = append(pq.elems, elem) + sort.Slice(pq.elems, func(i, j int) bool { return pq.sort(pq.elems[i], pq.elems[j]) }) +} +func (pq *priorityQueue[T, I]) IsEmpty() bool { + return len(pq.elems) == 0 +} +func (pq *priorityQueue[T, I]) Dequeue() (T, bool) { + var elem T + if pq.IsEmpty() { + return elem, false + } + + elem, pq.elems = pq.elems[0], pq.elems[1:] + return elem, true +} + +func heuristic(m Map, p point) int { + rows, cols := m.Size() + return rows - p[0] + cols - p[1] +} + +func search(m Map, minSize, maxSize int) int { + rows, cols := m.Size() + END := point{rows - 1, cols - 1} + + visited := make(map[vector]int) + pq := PriorityQueue(memo{}.sort) + pq.Enqueue(memo{h: heuristic(m, point{0, 0}), p: point{0, 0}, d: DN}) + + for !pq.IsEmpty() { + mem, _ := pq.Dequeue() + fmt.Println(mem) + if mem.h > dmap(visited, vector{mem.p[0], mem.p[1], mem.d[0], mem.d[1]}, INF) { + continue + } + + if mem.p == END { + return mem.s + } + + for _, nd := range m.NeighborDirections(mem.p) { + if nd[0] == 0 && mem.d == RT || nd[1] == 0 && mem.d == DN { + continue + } + + dscore := 0 + + for _, size := range irange(1, maxSize+1) { + np := mem.p.add(nd.scale(size)) + _, s, ok := m.Get(np) + + if !ok { + break + } + + dscore += s + pscore := mem.s + dscore + + nh := heuristic(m, np) + pscore + vec := vector{np[0], np[1], nd[0], nd[1]} + + if size >= minSize && nh < dmap(visited, vec, INF) { + pq.Enqueue(memo{nh, pscore, np, nd}) + visited[vec] = nh + } + } + } + } + + return 0 +} + +func dmap[K comparable, V any](m map[K]V, k K, d V) V { + if v, ok := m[k]; ok { + return v + } + return d +} +func irange(a, b int) []int { + lis := make([]int, b-a) + for i := range lis { + lis[i] = i + a + } + return lis +} + +type point [2]int + +func (p point) add(a point) point { return point{p[0] + a[0], p[1] + a[1]} } +func (p point) scale(m int) point { return point{p[0] * m, p[1] * m} } +func (p point) less(a point) bool { return p[0] < a[0] || p[1] < a[1] } + +type vector [4]int diff --git a/day17/main_test.go b/day17/main_test.go new file mode 100644 index 0000000..d7fd635 --- /dev/null +++ b/day17/main_test.go @@ -0,0 +1,41 @@ +package main + +import ( + "bufio" + "bytes" + "testing" + + _ "embed" + + "github.com/matryer/is" +) + +//go:embed example.txt +var example []byte + +//go:embed input.txt +var input []byte + +func TestExample(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 102) + is.Equal(result.valuePT2, 94) +} + +func TestSolution(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 843) + is.Equal(result.valuePT2, 1017) +} diff --git a/day20/example1.txt b/day20/example1.txt new file mode 100644 index 0000000..9ed10dd --- /dev/null +++ b/day20/example1.txt @@ -0,0 +1,5 @@ +broadcaster -> a, b, c +%a -> b +%b -> c +%c -> inv +&inv -> a \ No newline at end of file diff --git a/day20/example2.txt b/day20/example2.txt new file mode 100644 index 0000000..4da4379 --- /dev/null +++ b/day20/example2.txt @@ -0,0 +1,5 @@ +broadcaster -> a +%a -> inv, con +&inv -> b +%b -> con +&con -> output \ No newline at end of file diff --git a/day20/input.txt b/day20/input.txt new file mode 100644 index 0000000..c2efe97 --- /dev/null +++ b/day20/input.txt @@ -0,0 +1,58 @@ +%vg -> lf, vd +%dr -> kg +%cn -> mv, pt +%rq -> bk, gr +%vp -> lp, bk +%kg -> lv +%lv -> jc, tp +%sj -> rm, vd +%jc -> tp, qr +%km -> tp, dr +%jx -> cn +&vd -> tf, lf, nb, cx, hx, lr +%lp -> jt, bk +%vj -> ps +broadcaster -> km, lr, xh, rf +%dj -> pt, gc +%cg -> vd, hx +&ln -> tg +%fl -> pt, sk +%lm -> tr, bk +%lr -> vd, vg +&pt -> vq, rf, cm, jx, rg +%cx -> gp +%gp -> vd, sj +&db -> tg +%st -> vd +%jt -> bk +%jh -> lm, bk +%xf -> bd, tp +%gc -> cm, pt +&tp -> dr, km, kg, db, vj, qr +%ps -> xf, tp +%rf -> pt, dj +%lf -> nb +%bd -> tp, gg +%dk -> tp, vj +%mn -> jh, bk +&tg -> rx +%ql -> bk, zx +%tr -> bk, vp +%sk -> pt +%nb -> cg +%sb -> vd, cx +%qr -> dk +%xh -> bk, ql +%rg -> sd +%hx -> sb +%sd -> pt, jx +%gr -> bk, mn +%gg -> tp +%zx -> rq +&bk -> xh, ln, zx +%rm -> st, vd +%hq -> fl, pt +&vq -> tg +%cm -> rg +&tf -> tg +%mv -> pt, hq diff --git a/day20/main.go b/day20/main.go new file mode 100644 index 0000000..fdcd47c --- /dev/null +++ b/day20/main.go @@ -0,0 +1,282 @@ +package main + +import ( + "bufio" + _ "embed" + "fmt" + "strings" + + aoc "go.sour.is/advent-of-code" + "golang.org/x/exp/maps" +) + +// var log = aoc.Log + +func main() { aoc.MustResult(aoc.Runner(run)) } + +type result struct { + valuePT1 int + valuePT2 int +} + +func (r result) String() string { return fmt.Sprintf("%#v", r) } + +func run(scan *bufio.Scanner) (*result, error) { + m := &machine{} + receivers := make(map[string][]string) + + for scan.Scan() { + text := scan.Text() + + name, text, _ := strings.Cut(text, " -> ") + dest := strings.Split(text, ", ") + + switch { + case name == "broadcaster": + m.Add(name, &broadcaster{dest: dest}) + case strings.HasPrefix(name, "%"): + name = strings.TrimPrefix(name, "%") + m.Add(name, &flipflop{name: name, dest: dest}) + + case strings.HasPrefix(name, "&"): + name = strings.TrimPrefix(name, "&") + m.Add(name, &conjunction{name: name, dest: dest}) + } + + for _, d := range dest { + // rx is present so enable pt 2 + if d == "rx" { + m.Add("rx", &rx{}) + } + receivers[d] = append(receivers[d], name) + } + } + + m.setup(receivers) + + result := &result{} + + for i := 0; i < 10_000; i++ { // need enough presses to find the best LCM values for each conjunction + if i == 1000 { + result.valuePT1 = m.highPulses * m.lowPulses + } + m.Push(i) + } + + // rx is present.. perform part 2. + if rx, ok := receivers["rx"]; ok { + tip := m.m[rx[0]].(*conjunction) // panic if missing! + + var lvalues []int + for k, v := range tip.pushes { + for i, h := range makeHistory(v) { + if i == 1 && len(h) > 0 && h[0] > 0 { + fmt.Println(tip.name, k, "frequency", h[0]) + lvalues = append(lvalues, h[0]) + } + } + } + + result.valuePT2 = aoc.LCM(lvalues...) + fmt.Println(tip.name, "LCM", result.valuePT2, lvalues) + } + return result, nil +} + +type signal bool + +const ( + LOW signal = false + HIGH signal = true +) + +type message struct { + signal + from, to string +} + +type machine struct { + m map[string]pulser + + queue []message + + press int + highPulses int + lowPulses int +} + +func (m *machine) Add(name string, p pulser) { + if m.m == nil { + m.m = make(map[string]pulser) + } + p.SetMachine(m) + m.m[name] = p +} +func (m *machine) Send(msgs ...message) { + m.queue = append(m.queue, msgs...) + for _, msg := range msgs { + // fmt.Println(msg) + if msg.signal { + m.highPulses++ + } else { + m.lowPulses++ + } + } +} +func (m *machine) Push(i int) { + m.press = i + m.Send(generate(LOW, "button", "broadcaster")...) + m.processQueue(i) +} +func (m *machine) processQueue(i int) { + // look for work and process up to the queue length. repeat. + hwm := 0 + for hwm < len(m.queue) { + end := len(m.queue) + + for ; hwm < end; hwm++ { + msg := m.queue[hwm] + + if p, ok := m.m[msg.to]; ok { + // fmt.Println(i, "S:", m.m[msg.from], msg.signal, "R:", p) + p.Pulse(msg) + } + } + + hwm = 0 + copy(m.queue, m.queue[end:]) + m.queue = m.queue[:len(m.queue)-end] + // fmt.Println("") + } +} +func (m *machine) setup(receivers map[string][]string) { + for name, recv := range receivers { + if p, ok := m.m[name]; ok { + if p, ok := p.(interface{ Receive(...string) }); ok { + p.Receive(recv...) + } + } + } +} + +type pulser interface { + Pulse(message) + SetMachine(*machine) +} + +// IsModule implements the machine registration for each module. +type IsModule struct { + *machine +} + +func (p *IsModule) SetMachine(m *machine) { p.machine = m } + +type broadcaster struct { + dest []string + IsModule +} + +func (b *broadcaster) Pulse(msg message) { + b.Send(generate(msg.signal, "broadcaster", b.dest...)...) +} + +type flipflop struct { + name string + state signal + dest []string + + IsModule +} + +func (b *flipflop) Pulse(msg message) { + if !msg.signal { + b.state = !b.state + b.Send(generate(b.state, b.name, b.dest...)...) + } +} + +type conjunction struct { + name string + state map[string]signal + dest []string + + pushes map[string][]int + + IsModule +} + +func (b *conjunction) Receive(names ...string) { + if b.state == nil { + b.state = make(map[string]signal) + b.pushes = make(map[string][]int) + } + for _, name := range names { + b.state[name] = false + b.pushes[name] = []int{} + } +} +func (b *conjunction) Pulse(msg message) { + b.state[msg.from] = msg.signal + + if msg.signal { + // collect frequency of pushes to esti,ate rate + b.pushes[msg.from] = append(b.pushes[msg.from], b.press) + } + + if all(HIGH, maps.Values(b.state)...) { + b.Send(generate(LOW, b.name, b.dest...)...) + return + } + b.Send(generate(HIGH, b.name, b.dest...)...) +} + +type rx struct { + IsModule +} + +func (rx *rx) Pulse(msg message) { + if !msg.signal { + panic("pulse received") // will never happen... + } +} + +// helper funcs +func all[T comparable](match T, lis ...T) bool { + for _, b := range lis { + if b != match { + return false + } + } + + return true +} +func generate(t signal, from string, destinations ...string) []message { + msgs := make([]message, len(destinations)) + for i, to := range destinations { + msgs[i] = message{signal: t, from: from, to: to} + } + return msgs +} + +// makeHistory from day 9 +func makeHistory(in []int) [][]int { + var history [][]int + history = append(history, in) + + // for { + var diffs []int + + current := history[len(history)-1] + + for i := range current[1:] { + diffs = append(diffs, current[i+1]-current[i]) + } + + history = append(history, diffs) + + // if len(diffs) == 0 || aoc.Max(diffs[0], diffs[1:]...) == 0 && aoc.Min(diffs[0], diffs[1:]...) == 0 { + // break + // } + // } + return history +} diff --git a/day20/main_test.go b/day20/main_test.go new file mode 100644 index 0000000..1ab6824 --- /dev/null +++ b/day20/main_test.go @@ -0,0 +1,56 @@ +package main + +import ( + "bufio" + "bytes" + "testing" + + _ "embed" + + "github.com/matryer/is" +) + +//go:embed example1.txt +var example1 []byte + +//go:embed example2.txt +var example2 []byte + +//go:embed input.txt +var input []byte + +func TestExample1(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example1)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 32000000) + is.Equal(result.valuePT2, 0) +} + +func TestExample2(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example2)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 11687500) + is.Equal(result.valuePT2, 0) +} + +func TestSolution(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 819397964) + is.Equal(result.valuePT2, 252667369442479) +} diff --git a/math.go b/math.go index 468b1d6..b6d1124 100644 --- a/math.go +++ b/math.go @@ -2,8 +2,14 @@ package aoc import "cmp" +type uinteger interface { + uint | uint8 | uint16 | uint32 | uint64 +} +type sinteger interface { + int | int8 | int16 | int32 | int64 +} type integer interface { - int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 + sinteger | uinteger } // type float interface { diff --git a/search.go b/search.go new file mode 100644 index 0000000..8a8eea8 --- /dev/null +++ b/search.go @@ -0,0 +1,103 @@ +package aoc + +import ( + "sort" +) + +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 +}