advent2023

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

commit 8d6c90c171aa8ab3713367cbe9c3b1436f590a70
parent d00df266d39a97e3e9c4042f7b5016ad5000de8c
Author: bsandro <email@bsandro.tech>
Date:   Wed, 17 Jan 2024 17:41:32 +0200

day 09 p1

Diffstat:
Aday09/Makefile | 26++++++++++++++++++++++++++
Aday09/input.txt | 200+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday09/main.c | 30++++++++++++++++++++++++++++++
Aday09/puzzle.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday09/test.txt | 3+++
5 files changed, 347 insertions(+), 0 deletions(-)

diff --git a/day09/Makefile b/day09/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/day09/input.txt b/day09/input.txt @@ -0,0 +1,200 @@ +23 45 81 131 195 273 365 471 591 725 873 1035 1211 1401 1605 1823 2055 2301 2561 2835 3123 +10 9 7 18 72 228 601 1407 3030 6115 11691 21328 37332 62982 102813 162949 251490 378957 558799 807966 1147552 +15 24 45 84 160 333 746 1690 3708 7756 15432 29276 53164 92931 157702 263310 441377 762715 1394801 2735901 5712640 +9 23 37 51 65 79 93 107 121 135 149 163 177 191 205 219 233 247 261 275 289 +-4 -3 1 6 15 44 130 350 877 2122 5057 11920 27747 63713 144400 323457 716912 1573135 3418095 7353718 15663901 +11 11 6 -2 -6 19 151 584 1719 4277 9421 18878 35121 61944 106521 185777 343407 690347 1492628 3348666 7526272 +24 34 55 114 262 593 1272 2573 4927 8979 15652 26215 42351 66220 100511 148476 213938 301264 415293 561208 744340 +16 29 42 55 68 81 94 107 120 133 146 159 172 185 198 211 224 237 250 263 276 +12 18 27 48 109 267 618 1307 2538 4584 7797 12618 19587 29353 42684 60477 83768 113742 151743 199284 258057 +12 21 46 108 251 565 1227 2564 5142 9885 18228 32308 55197 91181 146089 227676 346064 514245 748650 1069788 1502959 +15 26 49 98 200 413 863 1829 3917 8369 17559 35761 70381 134094 248804 453148 816495 1464140 2620729 4681900 8327650 +23 43 78 139 250 470 936 1950 4142 8745 18017 35839 68507 125720 221745 376714 617977 981399 1512448 2266875 3310736 +20 31 42 53 64 75 86 97 108 119 130 141 152 163 174 185 196 207 218 229 240 +0 15 57 148 321 621 1108 1873 3092 5169 9076 17126 34681 73798 160679 350170 752624 1581391 3233211 6419036 12369433 +2 1 -3 -20 -55 -84 -7 426 1722 4808 11320 24198 49013 96975 191609 382999 776786 1590403 3257099 6609988 13194513 +-2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 +9 35 76 135 213 309 430 612 952 1657 3134 6192 12547 26120 56334 126239 291751 685211 1609544 3732749 8472629 +24 35 52 91 186 400 840 1691 3309 6464 12912 26611 56091 118753 248218 506285 1001598 1917777 3554548 6386323 11143744 +9 28 65 130 238 408 661 1017 1491 2088 2797 3584 4384 5092 5553 5551 4797 2916 -567 -6242 -14830 +-6 -1 17 53 106 163 196 180 164 441 1877 6473 18248 44545 97876 198436 377430 681371 1177521 1960661 3161390 +19 40 76 146 276 503 886 1524 2581 4318 7132 11602 18542 29061 44630 67156 99063 143380 203836 284962 392200 +-2 -6 -7 3 46 177 525 1376 3329 7574 16384 34009 68342 134023 258070 489679 916556 1691226 3072795 5494019 9673122 +7 7 22 68 176 410 894 1865 3795 7664 15531 31666 64708 131653 265044 525720 1025263 1965581 3709152 6904436 12710152 +16 26 36 46 56 66 76 86 96 106 116 126 136 146 156 166 176 186 196 206 216 +-4 6 36 104 241 501 976 1815 3245 5592 9301 14956 23304 35291 52123 75371 107146 150378 209242 289784 400811 +8 25 59 108 175 283 511 1076 2498 5894 13453 29147 59742 116241 216188 388198 682488 1196587 2136347 3952882 7632173 +15 11 5 -2 -5 7 47 131 305 726 1840 4747 11976 29235 69521 162826 378622 876280 2013882 4573923 10213632 +24 42 72 125 233 477 1043 2317 5031 10487 20924 40172 74896 137056 248878 453039 833754 1560773 2981569 5814499 11551681 +21 38 61 81 86 63 -2 -134 -398 -955 -2157 -4712 -10000 -20721 -42208 -84879 -168217 -325852 -608783 -1077773 -1763618 +7 8 23 69 176 395 813 1592 3060 5897 11484 22530 44179 85950 165108 312439 581949 1066774 1924629 3417499 5972050 +12 9 -1 -24 -72 -171 -378 -801 -1601 -2921 -4614 -5497 -1577 17858 78499 238063 620902 1487121 3367913 7324398 15429890 +3 14 36 72 136 274 610 1441 3418 7865 17304 36271 72536 138914 256055 457083 795977 1363556 2318440 3946246 6769676 +2 16 55 127 234 377 568 851 1344 2344 4600 9969 22842 52977 120721 266061 563534 1145768 2239341 4217755 7677650 +11 11 29 95 256 577 1156 2180 4057 7663 14743 28501 54406 101229 182310 317034 532471 865107 1362561 2085147 3107100 +21 31 42 51 55 51 36 7 -39 -105 -194 -309 -453 -629 -840 -1089 -1379 -1713 -2094 -2525 -3009 +12 18 24 30 49 121 347 969 2543 6287 14739 32935 70418 144520 285524 544516 1004982 1799496 3133186 5316060 8806727 +10 13 18 28 40 55 106 316 1015 2972 7836 18944 42785 91686 188838 377798 740352 1429455 2728326 5153228 9626686 +8 19 45 104 233 490 968 1844 3492 6690 12947 24982 47454 88291 161643 295047 546643 1044556 2071996 4241511 8835038 +12 29 70 161 343 668 1201 2047 3429 5859 10494 19903 39787 82874 177571 386510 844670 1834465 3927726 8244169 16905811 +8 30 60 110 206 395 759 1433 2633 4721 8367 14913 27101 50396 95216 180474 338942 625064 1125974 1976616 3380016 +23 42 64 95 152 274 554 1209 2721 6110 13438 28685 59174 117738 225800 417455 744475 1281874 2133236 3434387 5353140 +9 36 81 148 247 410 728 1419 2944 6206 12913 26303 52711 105084 210820 428669 883487 1834088 3799009 7778210 15625579 +17 22 37 74 144 256 411 599 820 1170 2065 4718 12021 29977 69708 149770 298107 553014 964659 1607208 2635322 +0 -4 -2 18 85 267 697 1610 3424 6942 13828 27638 55897 114066 232885 471860 944392 1865993 3649882 7101307 13817005 +8 34 87 193 395 772 1473 2764 5088 9142 15982 27181 45093 73328 117632 187508 299128 480394 779433 1278385 2115095 +11 20 37 67 123 223 397 713 1327 2557 4975 9504 17499 30782 51591 82392 125491 182370 252657 332625 413099 +9 31 57 84 125 226 493 1132 2501 5169 9973 18060 30897 50228 77953 115900 165457 227027 299265 378052 455157 +8 15 38 104 264 604 1254 2409 4393 7819 13935 25313 47153 89656 172187 330316 627302 1172175 2147268 3848838 6745262 +6 3 -3 -12 -24 -39 -57 -78 -102 -129 -159 -192 -228 -267 -309 -354 -402 -453 -507 -564 -624 +24 45 73 119 200 350 657 1349 2961 6621 14496 30439 60875 115958 211022 368337 619166 1006101 1585635 2430903 3634498 +19 38 71 119 183 264 363 481 619 778 959 1163 1391 1644 1923 2229 2563 2926 3319 3743 4199 +17 26 45 87 165 292 481 745 1097 1550 2117 2811 3645 4632 5785 7117 8641 10370 12317 14495 16917 +3 9 24 56 117 234 465 920 1787 3363 6090 10596 17741 28668 44859 68196 101027 146237 207324 288480 394677 +14 19 37 86 191 386 716 1239 2028 3173 4783 6988 9941 13820 18830 25205 33210 43143 55337 70162 88027 +5 11 17 23 26 13 -44 -174 -345 -317 622 4049 12929 32343 70816 143492 280245 545331 1081347 2200395 4561208 +7 -1 -3 21 109 332 815 1764 3499 6493 11417 19191 31041 48562 73787 109262 158127 224203 312085 427241 576117 +5 10 24 57 116 206 345 605 1199 2666 6286 15018 35548 82550 187161 413252 887922 1857866 3792947 7576141 14851341 +2 2 9 45 153 408 943 2008 4084 8086 15728 30215 57605 109488 208106 395742 751198 1417530 2646987 4871393 8809105 +22 36 66 123 219 371 617 1060 1975 4047 8855 19778 43574 92972 190720 375649 711444 1298958 2293064 3925213 6533053 +-4 3 29 92 226 488 967 1811 3311 6123 11783 23793 49749 105270 220899 453716 906166 1755603 3299329 6021514 10690372 +0 14 49 120 254 497 935 1758 3412 6898 14285 29502 59458 115505 215203 384264 658440 1084974 1723049 2642444 3919334 +9 9 17 53 157 407 949 2051 4204 8321 16141 31048 59716 115411 224661 440777 871119 1731357 3451621 6880631 13672371 +6 22 40 56 66 66 52 20 -34 -114 -224 -368 -550 -774 -1044 -1364 -1738 -2170 -2664 -3224 -3854 +4 1 -11 -35 -73 -126 -194 -276 -370 -473 -581 -689 -791 -880 -948 -986 -984 -931 -815 -623 -341 +13 38 77 136 229 398 760 1609 3625 8273 18507 39916 82444 162760 307215 555060 961161 1596772 2545941 3893740 5700627 +21 43 77 123 193 320 564 1022 1851 3311 5837 10178 17743 31544 58626 115748 241494 520271 1127583 2413496 5056684 +15 24 34 50 88 182 385 764 1392 2355 3833 6399 11828 24933 57251 133785 304473 662658 1373760 2719048 5162780 +8 20 45 90 178 377 841 1870 4011 8242 16306 31292 58595 107427 193096 340320 587898 995120 1650363 2682390 4274944 +10 16 28 55 104 183 318 590 1203 2624 5916 13565 31480 73605 172064 398589 908292 2026648 4418316 9407879 19576932 +7 4 18 74 207 477 994 1943 3603 6363 10751 17518 27887 44252 72022 124196 232113 469632 1005758 2216376 4916729 +16 32 64 129 267 565 1206 2558 5332 10872 21712 42665 82914 159862 305861 579384 1082849 1990675 3594851 6385422 11209158 +9 36 90 193 372 661 1121 1887 3248 5763 10413 18786 33289 57378 95794 154790 242331 368246 544308 784215 1103442 +9 30 77 168 332 616 1092 1864 3075 4914 7623 11504 16926 24332 34246 47280 64141 85638 112689 146328 187712 +-1 7 24 61 140 301 622 1265 2567 5203 10455 20621 39594 73671 132842 233475 405140 708644 1281603 2446154 4947265 +27 40 67 134 295 658 1433 3013 6099 11880 22279 40276 70319 118834 194845 310715 483019 733560 1090539 1589890 2276791 +-1 8 28 58 93 136 229 518 1384 3705 9385 22443 51282 113394 244938 519769 1086333 2237658 4541626 9079320 17874011 +0 9 29 72 164 348 686 1261 2199 3779 6790 13439 29329 67362 155025 347723 753290 1577685 3211003 6389054 12492794 +0 7 14 14 -1 -42 -122 -233 -275 101 1813 7020 20607 53528 130207 304367 691620 1534318 3327201 7056486 14650527 +6 16 26 36 46 56 66 76 86 96 106 116 126 136 146 156 166 176 186 196 206 +4 17 50 110 217 432 894 1860 3749 7202 13183 23175 39614 66941 114183 201017 371130 719767 1447176 2956860 6027919 +-2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 +17 23 29 35 41 47 53 59 65 71 77 83 89 95 101 107 113 119 125 131 137 +30 49 84 152 290 571 1121 2137 3906 6825 11422 18378 28550 42995 62995 90083 126070 173073 233544 310300 406554 +0 3 7 26 82 207 465 1009 2200 4834 10552 22570 47025 95634 191303 380374 759432 1533031 3135243 6480330 13471592 +22 43 89 170 297 493 812 1366 2360 4135 7219 12386 20723 33705 53278 81950 122890 180035 258205 363226 502061 +8 15 14 1 -31 -84 -136 -110 170 1037 3054 7103 14491 27074 47400 78872 125932 194267 291038 425133 607445 +7 13 29 67 139 257 433 679 1007 1429 1957 2603 3379 4297 5369 6607 8023 9629 11437 13459 15707 +16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168 176 +11 15 37 105 267 596 1195 2202 3795 6197 9681 14575 21267 30210 41927 57016 76155 100107 129725 165957 209851 +13 23 41 74 127 196 253 216 -112 -1159 -3779 -9521 -20848 -40824 -70865 -103013 -98630 63678 705132 2593323 7433868 +13 39 91 179 322 559 971 1737 3267 6477 13301 27586 56604 113555 221637 420524 776409 1397107 2454023 4212997 7076038 +8 30 59 101 175 312 562 1021 1898 3659 7312 14935 30599 61901 122399 235333 439124 795268 1399385 2396345 4000575 +8 35 82 167 327 621 1133 1982 3354 5589 9392 16306 29720 56955 113506 231541 476624 979871 1998130 4023353 7975513 +11 31 63 107 163 231 311 403 507 623 751 891 1043 1207 1383 1571 1771 1983 2207 2443 2691 +13 25 47 94 196 413 862 1769 3564 7042 13618 25709 47281 84604 147263 249478 411791 663183 1043689 1607584 2427218 +8 11 20 45 113 279 640 1351 2638 4798 8173 13087 19745 28114 37841 48315 59052 70677 86898 118017 186705 +4 21 56 122 249 498 977 1861 3436 6225 11317 21113 40831 81280 163626 327136 641204 1223341 2265254 4069652 7101005 +2 1 -4 -12 -13 27 204 756 2214 5663 13158 28351 57442 110778 206035 375461 683251 1267046 2434265 4877891 10142562 +17 27 50 92 164 288 511 935 1765 3383 6485 12388 23761 46336 92785 191282 404083 867289 1874702 4051558 8706948 +-1 12 36 76 140 239 387 601 901 1310 1854 2562 3466 4601 6005 7719 9787 12256 15176 18600 22584 +6 21 41 71 132 272 582 1221 2454 4707 8643 15263 26036 43062 69272 108669 166614 250161 368445 533127 758900 +14 28 48 74 106 144 188 238 294 356 424 498 578 664 756 854 958 1068 1184 1306 1434 +-6 -8 -10 -12 -14 -16 -18 -20 -22 -24 -26 -28 -30 -32 -34 -36 -38 -40 -42 -44 -46 +5 7 4 0 3 39 177 561 1456 3351 7236 15295 32446 69425 148467 313096 644111 1284559 2477332 4620026 8342869 +14 34 58 87 132 220 400 749 1378 2438 4126 6691 10440 15744 23044 32857 45782 62506 83810 110575 143788 +17 35 66 123 226 397 654 1004 1435 1907 2342 2613 2532 1837 178 -2898 -7963 -15725 -27046 -42961 -64698 +8 18 26 28 18 0 11 155 648 1874 4452 9314 17794 31728 53565 86489 134552 202818 297518 426216 597986 +19 37 56 78 106 146 213 346 644 1344 2987 6797 15602 36071 83924 195466 452048 1030333 2303243 5038800 10784057 +17 28 51 102 211 433 867 1697 3290 6410 12626 24993 49050 94085 174436 310296 527031 851360 1301837 1869864 2485893 +17 24 39 70 126 227 439 945 2161 4900 10572 21379 40416 71517 118584 184002 265569 351152 410013 379428 144842 +15 33 62 114 211 403 811 1717 3729 8057 16964 34530 68018 130392 244940 453528 830775 1507405 2707198 4803310 8401225 +6 19 38 58 74 81 74 48 -2 -81 -194 -346 -542 -787 -1086 -1444 -1866 -2357 -2922 -3566 -4294 +8 19 44 88 162 297 574 1175 2460 5075 10096 19214 34966 61017 102498 166405 262064 401667 600884 879556 1262474 +15 39 82 163 314 589 1089 2027 3878 7692 15701 32445 66816 135735 270739 529781 1018569 1930098 3620678 6762210 12656794 +4 18 47 99 185 335 637 1311 2843 6242 13560 28945 60694 125051 252868 500729 968742 1827946 3361173 6022263 10519767 +23 41 62 86 113 143 176 212 251 293 338 386 437 491 548 608 671 737 806 878 953 +18 37 79 160 302 543 954 1663 2888 4994 8620 14987 26637 49147 94956 191581 398592 841529 1778108 3726431 7715728 +-1 6 21 40 72 161 416 1049 2421 5096 9903 18006 30982 50907 80450 122975 182651 264570 374873 520884 711252 +3 0 1 17 78 244 626 1445 3172 6807 14370 29692 59609 115677 216541 391106 682673 1154218 1895007 3028755 4723552 +-5 -14 -19 -8 48 220 661 1665 3749 7762 15025 27506 48034 80556 130441 204835 313071 467138 682213 977260 1375700 +15 36 71 127 211 330 491 701 967 1296 1695 2171 2731 3382 4131 4985 5951 7036 8247 9591 11075 +14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 +18 26 50 112 238 458 819 1415 2446 4337 7974 15144 29301 56828 109044 205346 378126 680528 1198798 2072044 3523804 +8 4 3 14 55 159 377 784 1511 2864 5674 12187 28100 66841 157954 362573 800554 1695996 3450749 6759220 12782501 +-8 0 18 57 139 295 563 986 1610 2482 3648 5151 7029 9313 12025 15176 18764 22772 27166 31893 36879 +9 20 41 76 135 248 486 990 2009 3948 7427 13352 22999 38112 61016 94746 143193 211268 305085 432164 601655 +12 25 42 71 120 192 295 497 1078 2860 7832 20230 48282 106885 221545 433982 809880 1449347 2500742 4178625 6786692 +19 33 58 105 201 404 823 1646 3179 5899 10524 18103 30129 48678 76577 117604 176723 260357 376702 536085 751369 +10 10 11 19 57 180 511 1324 3223 7506 16864 36657 77170 157594 313262 609417 1168408 2224154 4230137 8068075 15437719 +3 -2 -6 -1 20 63 149 371 1028 2879 7567 18292 40917 85979 172742 337805 653417 1265432 2469112 4854769 9577442 +19 39 68 106 155 231 393 804 1847 4327 9798 21062 42895 83063 153699 273120 468171 777191 1253704 1970946 3027347 +-3 -7 -2 39 171 498 1208 2632 5351 10412 19796 37447 71491 138884 274898 552102 1117775 2266721 4581352 9198353 18311737 +15 44 102 205 383 704 1306 2434 4479 8017 13850 23060 37103 57995 88678 133703 200431 301034 455678 697391 1079263 +9 10 30 98 266 628 1359 2796 5597 11036 21529 41545 79144 148508 273998 496487 882993 1540974 2639056 4436452 7323902 +18 28 56 117 226 396 630 915 1239 1672 2589 5180 12505 31530 76845 177142 386048 799596 1583510 3014615 5542100 +27 56 110 199 333 522 776 1105 1519 2028 2642 3371 4225 5214 6348 7637 9091 10720 12534 14543 16757 +13 23 43 87 187 400 815 1560 2809 4789 7787 12157 18327 26806 38191 53174 72549 97219 128203 166643 213811 +16 28 41 51 66 119 283 705 1703 4009 9292 21158 46899 100350 206312 407110 771978 1410098 2488267 4254325 7067648 +14 31 49 67 82 95 134 309 920 2646 6861 16175 35431 73693 148406 294238 581857 1157694 2328280 4740020 9763827 +-5 4 27 74 161 311 564 1010 1874 3706 7762 16704 35798 74849 151181 294048 550949 996416 1743949 2961886 4894119 +8 17 37 87 194 394 742 1343 2435 4583 9085 18763 39456 82858 172116 353395 720587 1468728 3008765 6210291 12905653 +12 25 38 51 64 77 90 103 116 129 142 155 168 181 194 207 220 233 246 259 272 +7 3 -3 -3 16 72 188 392 717 1201 1887 2823 4062 5662 7686 10202 13283 17007 21457 26721 32892 +9 26 47 69 89 104 111 107 89 54 -1 -79 -183 -316 -481 -681 -919 -1198 -1521 -1891 -2311 +-4 10 52 141 311 632 1256 2497 4944 9591 17948 32072 54427 87447 132636 188994 250508 302392 315700 239871 -7305 +3 6 21 69 182 414 877 1829 3856 8205 17340 35808 71517 137543 254598 454306 783449 1309360 2126655 3365511 5201712 +17 23 31 50 97 205 433 878 1689 3083 5363 8938 14345 22273 33589 49366 70913 99807 137927 187490 251089 +-4 -3 13 56 148 328 653 1202 2115 3736 6990 14233 31017 69585 155567 340442 724062 1494143 2993391 5830146 11055394 +18 16 22 62 174 418 903 1839 3627 7009 13316 24884 45771 83028 149010 265687 472904 844578 1520870 2772042 5123556 +6 1 9 58 190 461 941 1714 2878 4545 6841 9906 13894 18973 25325 33146 42646 54049 67593 83530 102126 +1 -8 -23 -47 -85 -144 -233 -363 -547 -800 -1139 -1583 -2153 -2872 -3765 -4859 -6183 -7768 -9647 -11855 -14429 +27 48 89 167 315 594 1114 2068 3781 6786 11976 20963 36931 66554 124059 239407 474086 950555 1905751 3787297 7428516 +0 15 56 149 337 697 1369 2593 4758 8482 14767 25322 43259 74632 131901 241774 462875 925072 1915541 4063226 8724872 +17 16 6 -13 -35 -45 -19 78 321 945 2696 7697 21367 56354 140146 329277 735532 1574894 3261667 6595221 13138060 +-7 -10 -13 -16 -19 -22 -25 -28 -31 -34 -37 -40 -43 -46 -49 -52 -55 -58 -61 -64 -67 +4 20 61 156 351 709 1311 2272 3800 6346 10937 19894 38402 78003 164393 354628 774325 1696223 3702358 8012171 17134942 +30 52 94 184 366 701 1268 2165 3510 5442 8122 11734 16486 22611 30368 40043 51950 66432 83862 104644 129214 +1 12 45 111 232 450 838 1530 2816 5390 10902 23066 49741 106666 223937 456917 904131 1735888 3238971 5884833 10431430 +15 24 42 78 148 289 586 1218 2536 5195 10389 20317 39196 75519 146950 290410 581737 1172046 2351872 4659708 9059072 +3 14 36 82 177 354 653 1131 1889 3130 5304 9505 18503 39172 87673 199638 448855 979662 2060516 4169116 8123141 +1 16 56 143 314 636 1232 2330 4353 8080 14933 27498 50513 92864 171862 322712 619571 1221784 2473492 5114193 10716358 +17 27 48 88 155 257 402 598 853 1175 1572 2052 2623 3293 4070 4962 5977 7123 8408 9840 11427 +6 6 24 78 201 466 1020 2125 4209 7934 14300 24856 42247 71705 124878 228873 446004 914130 1924576 4065778 8477825 +-1 -4 7 40 103 210 389 699 1271 2412 4866 10427 23261 52532 117256 254742 534535 1080468 2104273 3955210 7191363 +5 0 7 49 171 452 1024 2107 4065 7488 13314 23019 38908 64515 105044 167646 261145 394660 574549 799449 1054253 +20 22 30 57 128 303 710 1593 3385 6818 13081 24033 42471 72443 119583 191429 297666 450214 663056 951673 1331922 +-1 -6 -11 -16 -21 -26 -31 -36 -41 -46 -51 -56 -61 -66 -71 -76 -81 -86 -91 -96 -101 +21 43 75 121 198 350 671 1337 2647 5073 9319 16389 27664 44988 70763 108053 160697 233431 332019 463393 635802 +16 40 81 144 239 391 651 1117 1991 3731 7421 15598 33986 74989 164591 355903 755745 1576771 3239427 6570786 13191854 +6 13 19 31 75 211 555 1319 2896 6054 12376 25223 51776 107292 223927 469003 982649 2054422 4276176 8839186 18094644 +26 37 48 59 70 81 92 103 114 125 136 147 158 169 180 191 202 213 224 235 246 +2 5 6 -1 -15 -14 75 435 1462 3929 9230 19725 39209 73530 131383 225309 372930 598453 934478 1424147 2123673 +23 46 84 142 242 440 858 1747 3602 7355 14677 28425 53275 96587 169553 288684 477697 769868 1210922 1862536 2806536 +8 4 6 23 65 143 269 456 718 1070 1528 2109 2831 3713 4775 6038 7524 9256 11258 13555 16173 +11 38 89 170 291 476 774 1281 2195 3948 7508 15051 31428 67304 145713 315372 676964 1433677 2986360 6110411 12283918 +3 9 25 52 95 176 354 752 1603 3352 6906 14241 29801 63521 136951 294943 626795 1302747 2634431 5171442 9850787 +19 49 95 162 257 400 644 1100 1972 3632 6821 13173 26463 55350 119093 259174 562889 1211641 2580375 5441334 11374741 +3 15 34 71 151 329 719 1545 3234 6586 13070 25298 47706 87402 155001 265026 435077 682417 1015850 1419721 1825497 +9 13 19 37 87 198 418 846 1691 3369 6681 13190 26060 51887 104561 213247 438877 908693 1889631 3944903 8265165 +20 37 67 110 174 301 608 1340 2942 6180 12381 23926 45220 84467 156673 288398 525070 942862 1671099 2942227 5213580 +11 33 69 135 258 476 833 1372 2136 3192 4709 7181 12045 23291 51354 121922 294891 703706 1634871 3685194 8066502 +-5 -7 -14 -35 -88 -205 -437 -859 -1575 -2723 -4480 -7067 -10754 -15865 -22783 -31955 -43897 -59199 -78530 -102643 -132380 +12 20 25 27 26 22 15 5 -8 -24 -43 -65 -90 -118 -149 -183 -220 -260 -303 -349 -398 +8 15 35 73 138 242 409 716 1412 3201 7834 19236 45500 102214 217752 441359 855096 1590987 2855029 4960091 8370142 +-2 -2 7 29 65 112 159 187 194 292 967 3661 11946 33775 85782 201771 450282 972262 2062795 4346533 9144907 +12 25 38 51 64 77 90 103 116 129 142 155 168 181 194 207 220 233 246 259 272 +5 13 40 109 251 515 988 1825 3289 5801 10000 16813 27535 43919 68276 103585 153613 223045 317624 444301 611395 +6 10 36 102 235 483 934 1742 3160 5580 9580 15978 25893 40813 62670 93922 137642 197614 278436 385630 525759 +9 19 40 71 110 153 206 323 687 1763 4590 11387 26907 61512 137949 305540 668297 1437767 3027736 6216923 12418248 +2 10 23 53 130 305 653 1276 2306 3908 6283 9671 14354 20659 28961 39686 53314 70382 91487 117289 148514 +2 6 28 89 226 508 1077 2228 4536 9042 17552 33229 61933 115269 217128 416739 812962 1596778 3121648 6014509 11343424 +0 3 -1 -7 2 69 299 932 2485 6027 13701 29690 61986 125662 249045 485521 936066 1791511 3409659 6451431 12113054 +6 16 50 127 274 539 1020 1922 3659 7037 13600 26308 50867 98299 189865 366607 708485 1374511 2690083 5340298 10796253 +-4 3 18 40 83 189 441 976 1998 3791 6732 11304 18109 27881 41499 60000 84592 116667 157814 209832 274743 +10 22 39 62 98 164 296 582 1260 2959 7228 17630 41941 96500 214692 463232 972914 1995776 4009897 7909102 15342256 +5 22 58 120 214 349 538 791 1107 1507 2218 4229 10604 29164 77450 191262 438545 940972 1906266 3675118 6787506 +18 27 35 39 40 57 148 438 1154 2667 5541 10589 18936 32089 52014 81220 122850 180779 259719 365331 504344 +18 27 33 33 26 13 -3 -17 -22 -9 33 117 258 473 781 1203 1762 2483 3393 4521 5898 +2 6 12 20 36 92 290 886 2445 6133 14284 31511 66847 137737 277192 546103 1053644 1989920 3676596 6643238 11739574 +9 16 27 42 71 144 313 658 1323 2628 5336 11206 24040 51546 108530 222323 442273 857336 1630809 3071910 5785193 diff --git a/day09/main.c b/day09/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 09\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/day09/puzzle.c b/day09/puzzle.c @@ -0,0 +1,88 @@ +#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 + +typedef struct IntArray { + int *values; + int len; +} IntArray; +void IntArray_Add(IntArray *a, int v) { + a->len++; + a->values = reallocarray(a->values, a->len, sizeof(int)); + a->values[a->len-1] = v; +} +bool IntArray_Same(IntArray *a) { + if (a->len <= 0) return false; + int v = a->values[0]; + for (int i=0; i<a->len; ++i) { + if (a->values[i]!=v) { + return false; + } + } + return true; +} +void IntArray_Print(IntArray *a) { + for (int i=0; i<a->len; ++i) { + printf("v:%2d (%p)\n", a->values[i], a->values+i); + } +} + +int IntArray_Diff(IntArray *a) { + if (IntArray_Same(a)) return a->values[0]; + if (a->len==1) { printf("error: array of len 1\n"); exit(-1); } + IntArray n = {0}; + for (int i=1; i<a->len; ++i) { + IntArray_Add(&n, a->values[i] - a->values[i-1]); + } + //printf("diff array:\n"); + //IntArray_Print(&n); + int diff = IntArray_Diff(&n); + //printf("d=%d\n", diff); + return a->values[a->len-1]+diff; +} + +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; + + while (fgets(buf, STR_LEN, infile) != NULL) { + IntArray readings = {0}; + char *s = strndup(buf, STR_LEN); + for (char *t; (t=strsep(&s, " "));) { + IntArray_Add(&readings, atoi(t)); + } + free(s); + + /*printf("----- readings: -----\n"); + IntArray_Print(&readings); + printf("---------------------\n");*/ + + int diff = IntArray_Diff(&readings); + //printf("diffs:%d\n", diff); + *result1 += diff; + //@todo list_free ? + ++line_num; + bzero(buf, STR_LEN); + } + + // mutiny! ignoring feof/ferror. + fclose(infile); +} diff --git a/day09/test.txt b/day09/test.txt @@ -0,0 +1,3 @@ +0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45