advent2023

Advent of Code 2023 solutions
git clone git://bsandro.tech/advent2023
Log | Files | Refs | LICENSE

commit 30a28fb0acb99550ad4370b820ab6a78cbff727b
parent 064374a6e534a628338bad38adea33b7aa8996c9
Author: bsandro <email@bsandro.tech>
Date:   Sat, 13 Jan 2024 20:08:59 +0200

day 07 p1

Diffstat:
Aday07/Makefile | 26++++++++++++++++++++++++++
Aday07/input.txt | 1000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday07/main.c | 30++++++++++++++++++++++++++++++
Aday07/puzzle.c | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday07/test.txt | 5+++++
5 files changed, 1199 insertions(+), 0 deletions(-)

diff --git a/day07/Makefile b/day07/Makefile @@ -0,0 +1,26 @@ + +NAME=$(shell basename ${PWD}) +SRC=$(wildcard *.c) +DEPS:=$(wildcard *.h) +OBJ:=$(SRC:.c=.o) +CFLAGS=-O2 -std=c11 -Werror -Wall -Wextra -I. -I../common +LDFLAGS=-lc + +all: $(NAME) + +.PHONY: clean run + +clean: + rm -f $(OBJ) $(NAME) + +%.o : %.c $(DEPS) + @$(CC) $(CFLAGS) -c $< -o $@ + +$(NAME): $(OBJ) + @$(CC) $(OBJ) -o $@ $(LDFLAGS) + +run: $(NAME) + @./$(NAME) input.txt + +test: $(NAME) + @./$(NAME) test.txt diff --git a/day07/input.txt b/day07/input.txt @@ -0,0 +1,1000 @@ +AT777 727 +7QT8A 998 +95925 783 +JK7T3 899 +JQ99A 970 +Q8AJQ 630 +44493 399 +TT47T 671 +88846 921 +7K7K7 279 +2K2KA 477 +2282J 277 +5QT64 886 +92J9J 80 +99987 929 +72333 958 +QA884 555 +8999J 850 +6KK6A 940 +TT22T 814 +TQTTT 681 +2A5KT 746 +K5K9K 239 +77828 964 +76A24 915 +9KKKK 379 +9KJK4 720 +2T87K 298 +TT7T3 655 +3Q7QK 96 +33739 265 +TT8TQ 270 +6QJQ9 44 +48QA6 610 +93399 328 +J38A6 362 +59Q57 79 +AQ456 949 +82AQA 857 +3333J 759 +5Q298 177 +9Q9Q9 721 +QQ55J 490 +24263 593 +Q4555 608 +K96K4 151 +644JK 354 +8J9KJ 62 +55A22 340 +TK38J 293 +K9774 64 +95229 518 +A8435 460 +9TT3T 629 +725AA 706 +4A4A4 950 +4QAK6 991 +A222K 393 +Q447J 112 +2Q223 108 +2KQKQ 865 +9JKKJ 197 +K8KKK 131 +Q6T96 471 +JT6T2 549 +4AA4J 572 +22Q24 740 +54367 333 +4KK4K 152 +94422 363 +77JQQ 413 +8A6J6 359 +76Q66 601 +KK77K 81 +88J99 419 +K62JA 897 +22J2J 370 +222A2 12 +34983 695 +6TKJT 384 +9763T 938 +5QK37 21 +3JK3Q 909 +37A33 113 +A6AQA 182 +2T222 213 +Q84KK 1 +AQT4T 660 +96696 777 +9JA54 162 +KQA2T 139 +333J5 903 +79654 786 +QAQ3A 734 +T888T 576 +28825 838 +JAAJA 312 +9955K 148 +K4JQT 132 +7J784 314 +ATAAA 784 +255Q4 930 +6699J 956 +45447 858 +88828 757 +J9594 267 +KKJK9 205 +527JA 848 +65A56 751 +QQJ88 533 +86Q97 331 +JT4Q8 846 +84258 707 +AAAA2 718 +AA769 808 +64KTJ 421 +QK637 574 +84KJ4 300 +88848 153 +T3TTQ 563 +443Q4 943 +T5T6T 632 +JA8A9 866 +424KK 932 +53555 58 +7Q737 794 +QJ3Q2 155 +JKA37 795 +AQQA4 609 +829J3 296 +7T46A 502 +855A5 802 +JQQ9Q 845 +55877 455 +224Q4 102 +35355 647 +QA6QA 564 +J4222 90 +85835 137 +5737K 36 +QQQ6Q 869 +77479 93 +85K55 622 +A3T77 754 +A5AAA 859 +JKKJK 824 +9K85T 100 +44785 444 +T262T 377 +QQJQ7 348 +22323 46 +6AKAA 585 +J5K66 39 +22244 454 +T8252 436 +75Q83 114 +K5Q26 4 +A9KAK 926 +6QAJK 369 +7778J 160 +37J55 860 +ATJ63 452 +97QQQ 542 +KTTTA 358 +9444T 731 +86J3Q 763 +73A75 725 +3AA3A 772 +68379 828 +A896T 284 +Q55QK 883 +JAAAA 8 +A67Q2 426 +33393 360 +JQT39 407 +9QJA5 667 +2A78A 631 +7J777 318 +9T949 592 +7AK88 361 +A997A 904 +99TJ4 37 +37377 513 +33K3K 928 +QAJQQ 640 +TK6KT 15 +4953T 780 +9TT9T 558 +3KJK4 174 +3J975 800 +353Q3 636 +88998 826 +65663 10 +5Q355 790 +545QQ 398 +J99A9 166 +7A7TT 690 +92T98 486 +28J88 191 +5A557 353 +99T99 605 +TTAA4 679 +4JT75 143 +36TJ7 591 +77T7J 881 +7JTQQ 990 +352T8 289 +9999J 642 +J555Q 561 +3A4J9 801 +4J5A5 22 +8J8KT 82 +7J667 953 +JJ553 526 +KT2K2 821 +88J88 343 +22474 285 +6A67A 94 +54354 56 +K3Q2Q 199 +4444K 169 +QKQKQ 432 +4KQ78 401 +T6A6J 465 +4999J 906 +A6A93 997 +55377 775 +J5553 901 +A3A53 19 +K6AA6 600 +AJ99A 257 +552T5 32 +55Q56 306 +TTKK2 520 +QQQQ5 185 +3877Q 150 +KA3T7 498 +6466J 449 +Q7759 594 +TTATT 77 +TTKTT 149 +52555 861 +99QQJ 299 +84468 625 +45433 159 +TT666 339 +Q589K 962 +2363K 587 +8J793 543 +K33AK 337 +22K2K 193 +99669 536 +AA666 68 +K5K5K 787 +444J4 588 +9A899 971 +QTTKQ 439 +2KQ5T 430 +T9T49 411 +344K5 822 +5T58T 724 +QQQ2Q 666 +3348K 375 +QAJQA 837 +A475A 415 +K324A 390 +5555J 933 +75525 31 +J6TTT 483 +829QA 807 +J7KQJ 584 +Q2Q34 127 +TTT5T 283 +3J633 17 +3A737 659 +5255Q 761 +3QKJ9 319 +7K7QK 689 +43844 559 +Q77QQ 804 +65454 202 +9QQ8K 286 +2888Q 580 +KA848 135 +6TT55 75 +38K38 656 +AA72A 429 +4J445 51 +49447 809 +576J6 234 +K965K 566 +555A5 357 +22282 321 +T663T 491 +6AAAT 59 +8J8A8 726 +99TKT 730 +73322 386 +446J9 222 +99979 652 +4958T 248 +AATKT 404 +58J55 245 +3477A 388 +JA33A 696 +33Q33 45 +24Q65 138 +Q66J2 554 +7779K 844 +T223T 538 +85K96 627 +53J99 194 +T76JJ 247 +43TK8 988 +KKJ28 769 +KK4K6 639 +AAATQ 701 +5682Q 553 +QQAKQ 994 +QA2QQ 20 +33332 128 +KJKK2 907 +TAJ48 612 +AATQ6 72 +55852 842 +QTTQQ 877 +J52A8 66 +QAA4A 494 +6J9A6 979 +68686 747 +K344Q 672 +K85A9 134 +897A7 852 +49559 569 +38J8T 487 +J3424 715 +3887J 570 +94939 799 +58588 13 +776T7 597 +7Q855 261 +9762T 53 +6774Q 527 +QTJQ3 372 +5JJ22 141 +KKKAA 571 +K7555 119 +K6K6K 525 +Q555Q 121 +83QT2 972 +QQQK2 482 +A4542 673 +23K64 974 +99J9J 16 +3K265 251 +QT93T 254 +A587K 442 +4J334 224 +AJA6A 324 +Q789A 957 +35QT7 552 +2T33T 76 +293JQ 999 +37535 297 +64774 249 +A38T4 524 +3Q6T7 253 +494Q9 665 +4Q9Q3 544 +7Q662 756 +28868 457 +A738J 745 +3652A 186 +KKK7A 303 +TTKTK 329 +5A6K4 104 +A56T9 403 +872T8 519 +99T8T 336 +AAKJT 628 +4T44T 836 +AKAKA 42 +3Q67J 397 +36734 230 +AT9TA 968 +29253 317 +3K76A 27 +K4JA6 551 +K92K9 78 +52TJJ 614 +AJA88 692 +T8J8T 955 +T7TT9 654 +9255J 776 +28855 500 +776J7 813 +79A77 616 +3939J 305 +T8857 505 +9T233 517 +KK2KK 986 +985J3 48 +A777A 165 +9QQ82 770 +3A997 700 +66776 262 +A56T2 238 +5555K 653 +42647 325 +K86TT 33 +9393K 764 +98A8Q 540 +A2KJT 509 +9T9JT 85 +J7995 338 +3Q2QQ 650 +2QQQ7 896 +59K5A 641 +8J979 719 +88J3J 57 +4QKAT 98 +J935A 758 +77577 722 +9AAA9 615 +QQJJQ 892 +55T55 451 +ATAJ5 456 +TT6K6 290 +T6TJA 810 +9T99T 352 +Q5T82 3 +57K36 607 +7736Q 440 +8K983 557 +JA4QA 793 +Q66QQ 743 +A9AQQ 895 +88A88 461 +J834T 475 +T7797 749 +39998 638 +4AQ3T 963 +7A592 973 +A699J 935 +KKTK8 993 +4444A 762 +T6QJQ 427 +28J3T 985 +38K3Q 503 +77755 595 +923QQ 414 +7Q6Q7 863 +AAQAA 678 +K7KKQ 263 +99929 229 +66TQ6 890 +8TATA 507 +987TT 5 +KQKKK 107 +75A66 63 +3KKKK 1000 +3J26Q 684 +6776T 402 +6A922 366 +3T334 129 +4TKKK 237 +KKK24 603 +6AT2Q 511 +QQA8Q 748 +33K33 392 +9J927 86 +6J3KJ 586 +65J98 714 +26669 712 +4AJ89 646 +Q3QT3 545 +8J5Q5 206 +92995 606 +35QK3 462 +36TQJ 323 +TTT2T 733 +4A847 876 +66AAJ 211 +4KK44 870 +AA77A 782 +384K9 445 +76382 7 +63655 99 +A2758 313 +4K228 698 +K5355 207 +A6388 894 +63268 853 +JAAQA 136 +3Q223 670 +7JJA2 164 +QJQK5 728 +5JAA5 753 +8JK83 983 +QQQQJ 223 +22922 713 +68888 766 +AAA7A 531 +A4352 996 +88787 816 +77637 568 +K82Q9 739 +QAK48 781 +42J2A 124 +J5J72 773 +TJT79 110 +86868 443 +TJJTJ 49 +89J86 244 +7Q6AT 741 +AAK4T 711 +A2A5A 351 +A94JA 334 +33Q4J 266 +KJKKK 750 +88AJQ 515 +K5Q4T 130 +666T6 30 +453JJ 18 +35J53 434 +A252A 378 +A86QA 156 +AJ4KK 431 +5QA5A 944 +34J92 888 +J8866 510 +6Q9TT 179 +K2722 661 +73QJT 855 +3QQJK 982 +7Q868 144 +73877 218 +JQ2K7 493 +352QA 948 +J33J6 812 +77767 644 +9J8T5 35 +48484 276 +3T3TT 11 +AAA9J 710 +82T28 819 +87J87 833 +997JQ 458 +448KK 939 +33A33 785 +988A8 683 +62555 342 +272Q2 529 +Q9QQ9 101 +TT7T8 70 +32Q79 433 +66622 735 +99494 133 +T6K84 699 +778TT 346 +3JJAA 158 +3339J 368 +J7747 774 +924A3 934 +KK3K3 326 +33553 294 +QQ444 405 +3759Q 106 +7A8J9 468 +85388 450 +J8A34 674 +7JQ64 634 +K56AJ 648 +5K972 446 +T76KQ 28 +9Q532 523 +A469A 203 +QQ4Q4 190 +25552 188 +8K3K3 167 +45QK8 344 +588J8 613 +22444 226 +A8855 620 +JK56T 535 +AAK99 806 +KKTKA 619 +35TTT 496 +QQQ4Q 474 +J77J7 84 +4Q54Q 473 +93K33 316 +TKAJQ 210 +3KKKJ 479 +TAJ55 556 +995Q9 208 +8K23A 52 +J7755 669 +9699J 947 +JT335 736 +77QTT 103 +TT3TT 69 +86838 792 +A7777 596 +46556 662 +44744 275 +22T82 74 +A5AA5 959 +6J66A 367 +ATTK2 796 +29384 599 +KJ9T4 495 +KKTKT 992 +Q4TQK 867 +38888 521 +TTJ7T 771 +77772 117 +A35J8 854 +KKTJA 910 +KK22K 236 +79484 146 +395A8 301 +7K775 635 +7397K 965 +TT973 126 +J4449 55 +AAAAK 195 +T8365 116 +44564 891 +AAJ4A 927 +J9737 382 +4T26K 534 +79979 862 +A35AA 273 +TT5QA 347 +22777 851 +93Q9J 142 +A545A 38 +44555 878 +T727J 428 +366JA 788 +TTT44 95 +66696 425 +T333J 803 +6Q66J 441 +394J6 976 +TJQQQ 29 +93752 23 +2798J 980 +252J2 43 +33QK6 278 +5J75J 917 +TT66Q 967 +QJ4TQ 744 +JQQ5Q 435 +8T999 420 +Q77A9 246 +K8K86 856 +55585 88 +JTJTT 34 +29Q89 913 +K5A96 508 +AAQQA 470 +QQAA8 274 +4Q2A7 697 +AQAQQ 742 +55574 232 +A748K 868 +K87T9 831 +JK5KT 196 +JJJJ8 466 +JQ3JA 448 +53JA3 120 +5JK5J 484 +Q8QQ8 60 +48488 729 +4ATTT 541 +4274K 387 +2233J 355 +447K4 488 +9Q7A2 506 +26237 737 +54J43 154 +445JA 264 +J8JJ8 464 +TAT7T 252 +6Q95A 581 +87JAK 335 +27654 817 +JJ444 530 +T8JTT 288 +43AKQ 623 +975J4 307 +44484 547 +44944 240 +8J88Q 767 +24J6J 24 +32646 499 +T3724 832 +666TJ 705 +45T24 841 +7J3J4 984 +Q2KJQ 463 +5J5J5 898 +3Q3QQ 176 +46474 760 +KQKQ9 67 +942T9 818 +TAJT7 381 +43QQQ 716 +8KJKK 738 +836J6 823 +J4888 105 +9J46T 418 +KK828 175 +J6556 250 +A354J 573 +68AK9 282 +35755 937 +K343K 514 +22552 924 +AQ5K3 645 +47AAJ 966 +8Q649 830 +QJAQ5 693 +6AAAA 349 +AJAAT 140 +AA557 657 +88K8K 911 +44545 2 +5JK52 125 +98T87 161 +T55TT 702 +7Q767 73 +6KQ64 611 +AQJQK 380 +39947 171 +JAQK4 241 +T2T22 231 +23222 271 +44743 201 +AAAA8 424 +74835 952 +KKKTK 908 +TT377 214 +TTK44 111 +J78J4 579 +49QJ3 923 +6K2Q3 269 +K55J5 717 +27J7A 682 +J2KQ8 893 +84Q26 578 +22325 512 +T67K3 322 +39368 840 +6T2K8 765 +9943J 178 +KQ3JK 308 +88QQ8 522 +TTJTT 687 +4TA4Q 118 +TTKK7 834 +KKJTT 889 +75755 677 +558J2 453 +QQ5T5 220 +7J2Q3 87 +4QQTQ 459 +4466T 243 +TTA8T 686 +5255J 811 +K2555 385 +54795 575 +66646 624 +4JKK6 548 +49A4J 902 +A2TT7 330 +273TA 885 +8A928 847 +95555 216 +3J733 768 +J99T9 900 +48J48 310 +TT2T5 791 +T8TT6 691 +KT4TK 637 +AAK7A 916 +8A2AA 987 +92T22 280 +ATATA 258 +KQ9QQ 981 +7378T 472 +T7TJ7 590 +69TQ9 172 +6J6J6 704 +A4494 147 +93A66 65 +67TTT 887 +J58Q9 374 +77773 537 +AA5A4 198 +KT33T 946 +J9J39 145 +QA888 383 +TJ229 942 +44784 26 +39QQQ 501 +58A88 778 +JATTT 89 +A7222 356 +88J86 373 +KA69J 447 +724TJ 233 +26T22 839 +72884 633 +8K3QT 170 +66667 260 +8TJ88 187 +4A7KJ 184 +A2947 685 +744A7 219 +JQKQQ 272 +T62J2 256 +4T2J3 589 +635K3 995 +2A6Q4 481 +99998 688 +TTTT4 122 +33388 304 +66Q86 582 +5K55K 189 +26479 371 +TTTJ3 295 +66K66 829 +47949 873 +645Q8 395 +6666A 504 +262A8 676 +35333 825 +7KJ73 827 +638JT 752 +86889 50 +85J6T 919 +J4TT4 602 +TT68J 123 +J2229 604 +237QA 396 +22J37 365 +6JJT8 880 +99969 560 +632T4 225 +K3K39 723 +TKT22 872 +TTQ9J 281 +6KAKK 83 +88QJT 389 +T8Q3T 423 +TTJ5T 905 +62T58 309 +7J77A 227 +34JT3 485 +6284J 492 +7KKJK 874 +42A6J 882 +28872 217 +QQ9QQ 408 +78796 550 +77799 157 +6J656 480 +5K83A 422 +8TTTT 864 +3J638 192 +3QQ5Q 621 +8TK83 115 +K99JK 598 +5J3T6 884 +3Q8KQ 61 +T55K5 989 +AA8A8 675 +33J43 320 +3674J 797 +77977 936 +22466 703 +9J488 945 +7TTKT 680 +82828 875 +AAKKJ 663 +JJJJJ 302 +JA222 25 +5TA98 364 +6J667 47 +Q398T 668 +82386 180 +34JK4 173 +3AT24 476 +2K6KT 849 +87355 97 +44424 779 +92Q29 798 +AKAK2 709 +A63Q3 92 +33366 350 +58Q5T 871 +J8855 71 +95A3A 341 +4446A 412 +49K94 417 +74J55 376 +J5K27 843 +J5TTJ 617 +6666J 694 +68KQ6 516 +35K3K 565 +T7T7T 528 +49934 539 +3J436 183 +J4477 406 +649JJ 204 +J5A2Q 583 +88827 416 +AK3Q9 562 +KT56A 532 +866J2 969 +6366Q 577 +TT788 975 +KAKK3 345 +J6922 664 +2222K 497 +23K93 438 +6J6T7 755 +68KK6 914 +85855 268 +5JJT5 287 +6J622 212 +22828 922 +9QQ96 815 +44788 960 +5T243 643 +QQQ76 410 +J3999 292 +Q566J 918 +QJ4JK 41 +99895 391 +5K4TA 835 +Q5Q8Q 651 +33686 732 +A3Q6K 879 +36663 626 +9TT9Q 649 +J365A 941 +K2KK6 163 +77237 977 +J6QT9 242 +AJ4T7 332 +KK6KK 315 +4733J 221 +K4J96 912 +33KTQ 469 +KQ78T 200 +2A32A 235 +2A33A 54 +6K63T 489 +62444 978 +J2TTQ 478 +TA6JQ 931 +46644 546 +76KKJ 168 +88JJ8 255 +23233 708 +88Q4K 228 +6A4AQ 805 +55JAJ 181 +4869T 327 +TT777 259 +7Q777 209 +4J22J 109 +22J22 789 +T88TT 91 +388QK 40 +36326 618 +44J3Q 925 +45283 215 +A4K9K 14 +T3666 961 +Q452T 920 +2JK22 437 +Q9JA8 567 +KK8K8 954 +663J6 6 +Q4J4J 409 +KQ7T2 394 +K7KKK 9 +JJ333 400 +K4999 658 +98899 467 +76778 311 +K2A99 951 +834Q2 291 +K6KJ9 820 diff --git a/day07/main.c b/day07/main.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <time.h> +#include <string.h> + +void puzzle(const char *filename, long long *res1, long long *res2); +//void puzzle_test(const char *filename, long long *res1, long long *res2); + +int main(int argc, char *argv[]) { + printf("Advent of Code: day 07\n"); + double time_start = clock(); + + if (argc <= 1) { + printf("Usage: %s inputfile.txt\n", argv[0]); + return -1; + } + + const char *filename = argv[1]; + long long counter1 = -1; + long long counter2 = -1; + + puzzle(filename, &counter1, &counter2); + + printf("Puzzle #1: %lld\n", counter1); + printf("Puzzle #2: %lld\n", counter2); + + double elapsed = clock() - time_start; + printf("Elapsed: %f\n", elapsed / CLOCKS_PER_SEC); + + return 0; +} diff --git a/day07/puzzle.c b/day07/puzzle.c @@ -0,0 +1,138 @@ +#define _DEFAULT_SOURCE + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <strings.h> +#include <assert.h> +#include <stdbool.h> +#include <inttypes.h> + +#define STR_LEN 2048 +#define CARDS_LEN 5 +#define COUNTS_LEN 15 + +typedef enum HandType { + Hand_Unknown = 0, + Hand_HighCard = 50000, + Hand_OnePair = 41000, + Hand_TwoPair = 32000, + Hand_ThreeOfAKind = 31100, + Hand_FullHouse = 22100, + Hand_FourOfAKind = 21110, + Hand_FiveOfAKind = 11111 +} HandType; + +typedef struct Hand { + HandType type; + int bid; + char cards_orig[CARDS_LEN+1]; + int cards[CARDS_LEN]; + int counts[COUNTS_LEN]; + int32_t csum; +} Hand; + +int get_card(char c) { + if (c>='2'&&c<='9') return c-'0'; + if (c=='T') return 10; + if (c=='J') return 11; + if (c=='Q') return 12; + if (c=='K') return 13; + if (c=='A') return 14; + return -1; +} + +void make_type(Hand *h) { + int sum = 10000*h->counts[0] + + 1000*h->counts[1] + + 100*h->counts[2] + + 10*h->counts[3] + + h->counts[4]; + h->type = (HandType)sum; +} + +void make_counts(Hand *h) { + for (int i=0; i<CARDS_LEN; ++i) { + h->counts[h->cards[i]]++; + } +} + +void make_csum(Hand *h) { + h->csum = 100000000*get_card(h->cards_orig[0]) + + 1000000*get_card(h->cards_orig[1]) + + 10000*get_card(h->cards_orig[2]) + + 100*get_card(h->cards_orig[3]) + + get_card(h->cards_orig[4]); +} + +static int cmp_cards(const void *c1, const void *c2) { + return *(int *)c1 - *(int *)c2; +} + +static int cmp_counts(const void *c1, const void *c2) { + return *(int *)c2 - *(int *)c1; +} + +static int cmp_hands(const void *v1, const void *v2) { + const Hand *h1 = (Hand *)v1; + const Hand *h2 = (Hand *)v2; + if (h1->type!=h2->type) { + return h1->type - h2->type; + } + return h1->csum - h2->csum; +} + +void puzzle(const char *filename, long long *result1, long long *result2) { + FILE *infile = fopen(filename, "r"); + if (infile == NULL) { + fprintf(stderr, "fopen() error: %s\n", strerror(errno)); + return; + } + + char buf[STR_LEN] = {0}; + unsigned int line_num = 0; + *result1 = 0; + *result2 = 0; + + Hand *hands = NULL; + int hands_len = 0; + while (fgets(buf, STR_LEN, infile) != NULL) { + hands_len++; + hands = realloc(hands, sizeof(Hand)*hands_len); + Hand *h = &hands[hands_len-1]; + memset(h, 0, sizeof(Hand)); + sscanf(buf, "%s %d", h->cards_orig, &h->bid); + for (int i=0; i<CARDS_LEN; ++i) { + h->cards[i] = get_card(h->cards_orig[i]); + } + qsort(h->cards, CARDS_LEN, sizeof(int), cmp_cards); + make_counts(h); + make_csum(h); + qsort(h->counts, COUNTS_LEN, sizeof(int), cmp_counts); + make_type(h); + ++line_num; + bzero(buf, STR_LEN); + } + + qsort(hands, hands_len, sizeof(Hand), cmp_hands); + for (int i=0; i<hands_len; ++i) { + printf("hand %2d %s, type %d, bid %3d; ", i, hands[i].cards_orig, hands[i].type, hands[i].bid); + for (int j=0; j<CARDS_LEN; ++j) { + printf("%2d ", hands[i].cards[j]); + } + printf(" csum: %10d | ", hands[i].csum); + for (int j=0; j<COUNTS_LEN; ++j) { + if (hands[i].counts[j]>0) { + printf("%d ", hands[i].counts[j]); + } + } + printf("\n"); + + *result1 += (i+1)*hands[i].bid; + } + + // mutiny! ignoring feof/ferror. + fclose(infile); +} diff --git a/day07/test.txt b/day07/test.txt @@ -0,0 +1,5 @@ +32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483