From d9b2d7990b6c0fbee1dc7d6b9ce82a39d689aeb7 Mon Sep 17 00:00:00 2001 From: tristan Date: Sat, 7 Dec 2024 17:56:20 +0000 Subject: [PATCH] 2024 day 7, plus an optimised version --- 2024/06/solution.nix | 104 +++++ 2024/07/example.txt | 9 + 2024/07/input.txt | 850 ++++++++++++++++++++++++++++++++++++++ 2024/07/solution.nix | 193 +++++++++ 2024/07/solution.test.nix | 91 ++++ 2024/flake.nix | 37 +- 6 files changed, 1267 insertions(+), 17 deletions(-) create mode 100644 2024/07/example.txt create mode 100644 2024/07/input.txt create mode 100644 2024/07/solution.nix create mode 100644 2024/07/solution.test.nix diff --git a/2024/06/solution.nix b/2024/06/solution.nix index 80c210a..ea351f0 100644 --- a/2024/06/solution.nix +++ b/2024/06/solution.nix @@ -128,4 +128,108 @@ annotated = chart |> annotatePath |> annotateObs |> chartToStr; + # FASTER! + + startingObsMap = getObsMap chart; + + getObsMap = let + findObs = lib.lists.findFirstIndex (b: b == "#") null; + in chart: chart + |> lib.imap0 (y: line: line + |> lib.imap0 (x: char: if char != "#" then null else { + right = if y + 1 >= height || x + 1 >= width then null else (chart + |> index (y + 1) + |> lib.sublist (x + 1) width + |> findObs + |> maybeAdd (x + 1) + ); + left = if y < 1 || x <= 0 then null else (chart + |> index (y - 1) + |> lib.sublist 0 (x) + |> lib.reverseList + |> findObs + |> maybeSub (x - 1) + ); + down = if x < 1 then null else (chart + |> lib.sublist (y + 1) (height) + |> map (index (x - 1)) + |> findObs + |> maybeAdd (y + 1) + ); + up = if x + 1 >= width then null else (searchUp (b: b == "#") {x = x + 1; y = y - 1;} chart); + }) + ) + ; + + maybe = f: b: if isNull b then null else f b; + maybeAdd = v: builtins.add v |> maybe; + maybeSub = v: builtins.sub v |> maybe; + + searchUp = matcher: {x, y}: chart: chart + |> lib.sublist 0 (y + 1) + |> map (index (x)) + |> lib.reverseList + |> lib.lists.findFirstIndex matcher null + |> maybeSub (y) + ; + + firstOb = { + y = searchUp (i: !isNull i) startingPos startingObsMap; + x = startingPos.x; + }; + + index2d = {x, y}: m: m |> index y |> index x; + + lookRight = obsMap: prev: { + x = (index2d prev obsMap).right; + y = prev.y + 1; + }; + + lookDown = obsMap: prev: { + x = prev.x - 1; + y = (index2d prev obsMap).down; + }; + + lookLeft = obsMap: prev: { + x = (index2d prev obsMap).left; + y = prev.y - 1; + }; + + lookUp = obsMap: prev: { + x = prev.x + 1; + y = (index2d prev obsMap).up; + }; + + getNextDir = i: [lookRight lookDown lookLeft lookUp] + |> index (lib.mod i 4) + ; + + getFastpath = {i ? 0, pos ? firstOb, obsMap ? startingObsMap, hist ? []}: let + nextPos = (getNextDir i) obsMap pos; + newHist = hist ++ [point]; + point = pos // {dir = lib.mod i 4;}; + looped = lib.elem point hist; + in if isNull pos.x || isNull pos.y || looped + then {path = hist ++ [pos]; inherit looped;} + else getFastpath {i = i + 1; pos = nextPos; hist = newHist; inherit obsMap;} + ; + + fastpath = getFastpath {}; + + allObs = builtins.genList (i: { + x = lib.mod i width; + y = i / height; + }) (height * width) + |> builtins.filter (pos: index2d pos chart == ".") + |> map ({x, y}: replace2d {inherit x y;} "#" chart) + ; + + # this is a really slow way to do this, instead just find the adjacent columns and update accordingly + allMaps = allObs |> map getObsMap; + + loopyMaps = allMaps + |> builtins.filter (m: (getFastpath {obsMap = m;}).looped); + + part2faster = builtins.length loopyMaps; + } diff --git a/2024/07/example.txt b/2024/07/example.txt new file mode 100644 index 0000000..fc6e099 --- /dev/null +++ b/2024/07/example.txt @@ -0,0 +1,9 @@ +190: 10 19 +3267: 81 40 27 +83: 17 5 +156: 15 6 +7290: 6 8 6 15 +161011: 16 10 13 +192: 17 8 14 +21037: 9 7 18 13 +292: 11 6 16 20 diff --git a/2024/07/input.txt b/2024/07/input.txt new file mode 100644 index 0000000..4647750 --- /dev/null +++ b/2024/07/input.txt @@ -0,0 +1,850 @@ +1233: 1 18 5 3 +109101993: 3 8 2 471 2 5 5 2 1 1 517 +344748: 66 4 518 3 790 +5354640: 3 1 490 890 9 37 +663936: 8 920 25 272 855 78 +14099635: 268 6 4 2 6 5 3 214 2 31 +640057: 183 8 4 436 9 +2854: 9 3 13 59 82 +1773145: 805 8 78 73 6 9 1 4 +30614712991: 964 201 790 3 2 93 3 +368648: 4 6 192 1 8 +207308636: 6 30 7 7 9 3 5 94 5 90 4 1 +9329401237: 7 2 855 628 453 8 238 +111833872456: 42 97 792 33 9 7 8 2 5 8 +2942544984: 947 1 31 68 449 84 +19831717202: 346 87 646 918 885 5 +3798404: 3 6 9 38 4 50 945 38 82 +141719146: 46 333 428 57 90 2 6 8 +1628213600: 4 93 3 9 8 22 4 6 3 7 3 5 +1300751: 4 82 7 811 751 +136155101: 24 8 795 892 155 66 +104057566: 80 2 9 6 8 9 4 6 +35614965: 6 886 3 5 1 1 9 9 743 1 2 +971077: 495 37 53 330 51 1 +14453: 3 8 4 897 72 232 3 +9565163893557: 21 935 51 63 89 355 7 +2561: 8 17 62 +7286: 6 90 3 764 5 +282273493355: 3 5 3 4 8 8 548 7 8 57 5 +80890325640: 25 86 34 26 7 920 +416850: 14 3 7 397 7 +1825919359292: 55 89 64 323 359 292 +289725437473152: 772 7 63 9 23 851 152 +85457839140: 984 37 6 954 3 2 7 820 +93106926: 3 344 6 46 8 9 4 3 974 9 +99605551: 4 249 10 555 4 +56952: 569 49 57 3 7 4 3 +17135089: 85 652 2 46 24 65 +484740: 3 8 2 4 3 115 52 7 1 2 1 9 +834609: 83 4 431 176 2 +17658858: 79 33 6 7 53 +48992111574: 4 8 992 111 57 4 +21035: 7 63 9 1 16 26 52 +3892138: 9 6 1 429 4 4 7 7 4 3 8 2 +1288934: 4 350 31 9 29 8 5 992 +167846003069: 808 31 230 20 30 72 +5574: 5 952 5 7 799 +199273356: 701 2 7 9 4 16 6 47 5 5 3 +721984233: 7 2 19 842 30 +77061440: 290 1 272 857 160 +595697: 7 77 7 9 7 240 7 5 505 7 +27740629913: 8 68 365 629 910 +5809: 3 5 623 6 819 +32358137220: 64 9 7 7 557 1 3 63 9 23 +22372: 913 4 34 43 6 +11178860: 23 48 5 28 17 +64682084678: 66 70 2 2 978 10 7 8 +37332412653: 9 154 18 3 369 614 +57132: 3 8 9 602 2 9 727 5 9 +1130753406060: 8 99 689 5 9 6 35 605 9 +1816563600: 90 6 94 4 5 202 3 5 5 24 +569882610: 4 1 2 236 761 1 6 33 93 +830228: 401 9 5 23 43 +210262: 1 1 44 42 8 7 206 744 4 +165128576: 16 4 43 6 856 5 9 +11738564439: 233 234 67 189 251 +659845643: 6 26 8 1 9 6 1 4 1 4 1 +711578010: 4 954 474 187 887 +1592: 8 2 6 1 525 3 1 7 143 87 +7567457: 631 56 950 11 7 +89724: 64 6 203 105 185 394 +10565637356: 5 56 377 96 3 1 73 5 4 2 +4792246: 1 4 77 3 8 9 8 9 4 5 9 6 +4951: 9 6 7 2 9 5 3 +220884971223514: 474 466 9 712 23 514 +1910495: 210 9 9 6 1 43 9 6 3 7 4 5 +1711476: 4 8 9 6 2 8 1 18 9 383 6 9 +56819123: 5 520 240 956 2 5 +22701435: 1 12 72 407 664 603 +943269: 6 1 2 75 8 131 62 1 4 3 1 +684976150728: 90 174 1 76 7 5 81 54 +36040: 67 388 382 43 49 +18116058240: 1 43 63 3 1 864 215 3 4 +9574204: 7 75 647 9 789 6 81 1 +32200: 1 9 30 35 23 +9831: 3 90 5 2 5 6 +4368: 44 9 34 6 8 +51632093208: 7 1 287 655 4 89 9 7 +378088896: 62 4 2 914 834 +4819853: 25 82 55 819 38 +14655314: 6 9 52 828 4 84 93 2 +3593362626: 6 24 50 225 91 1 117 9 +266235417: 7 7 9 74 67 3 50 3 3 7 9 +4615721289: 71 797 427 9 4 4 7 639 +5108492031133: 90 7 7 968 646 81 9 2 +327638: 3 52 6 1 35 +2534: 426 4 94 602 56 78 +75442: 2 12 4 4 50 516 115 4 6 +314880394762: 320 164 6 394 764 +21219287189: 70 535 1 869 652 1 +9168: 84 6 7 516 8 +279388285046: 8 465 3 1 3 7 6 7 550 43 +13450525: 65 8 781 175 3 9 +179797698432: 2 968 946 303 54 6 +479001668: 308 8 1 27 300 3 8 65 3 +366921: 58 2 50 419 59 +308689423: 1 29 868 9 424 2 +48444576404: 48 44 4 57 6 404 +568888103559: 6 95 69 8 3 4 99 5 56 73 +343877037304: 985 886 436 63 8 +4386277350: 13 987 53 86 75 +2378904521: 5 19 581 431 24 +7758: 843 3 8 984 6 +413120526: 67 26 491 69 7 +425332: 4 421 26 7 66 +516566: 3 4 23 53 1 5 653 7 9 6 +1023358353: 7 538 5 426 82 637 17 +2470025504: 7 9 7 7 8 2 77 148 504 +308762622: 8 910 738 83 4 8 6 1 +154407150848: 5 9 7 8 8 2 9 7 562 60 8 +17634: 8 9 634 +450592: 2 3 445 59 2 +387052428: 6 6 6 5 933 4 7 88 2 426 +34926021250: 91 4 24 9 602 1 1 8 7 66 +1616641: 4 36 79 14 37 +58333671: 79 5 7 570 6 664 703 2 +122527: 720 217 39 249 30 +4765512: 2 5 4 7 98 6 4 6 4 378 2 3 +1432: 4 343 9 1 4 8 +23112862: 83 36 2 914 2 592 +1090765: 39 79 79 9 7 13 +72509: 72 50 9 +201027: 40 9 3 7 6 473 +39123912115: 3 8 899 411 8 112 +6136325: 939 81 8 94 8 5 +45621014: 83 8 241 73 30 5 +4724738307476: 7 7 216 8 3 576 8 4 2 9 4 +26290: 3 2 1 6 5 90 727 1 363 +3031608: 6 148 561 8 53 8 +1377: 95 3 7 550 29 7 499 +19834: 7 4 268 +783511509: 95 43 38 821 14 +3288842180462: 82 2 210 54 4 4 4 45 7 4 +484: 4 460 9 5 6 +2519409274: 3 85 2 6 13 1 185 5 1 2 4 +760388283731: 3 90 79 2 7 6 5 8 9 26 1 1 +198119: 6 5 9 99 121 +452284: 5 632 710 13 1 +37436854690509: 671 988 367 6 70 253 +227160009081: 327 9 507 80 75 908 1 +87988735: 877 1 6 272 735 +14121187: 1 419 3 79 334 +6877248522: 19 3 2 6 9 5 1 8 8 7 513 7 +1026117: 9 95 6 57 1 2 +5827398923: 298 1 65 54 663 3 23 +495795976: 3 682 3 3 2 9 5 4 496 8 6 +1829: 3 55 1 8 497 4 +94865: 5 935 260 32 77 1 +537: 17 9 3 +214232862: 471 96 9 3 74 53 5 20 9 +161032340: 3 7 3 8 7 69 8 41 88 12 7 +1320891: 5 8 11 9 891 +16202584: 8 48 9 333 248 +52556323: 3 2 7 4 52 1 2 6 314 7 2 +3458493705: 9 929 76 221 341 1 8 6 +1429440: 300 76 59 965 60 +51719895: 6 8 3 80 1 7 2 3 437 894 +3637900448: 327 561 61 7 7 8 731 +4803971810: 632 101 5 760 410 +8523057: 4 8 7 9 9 864 1 4 5 1 669 +1506471912: 10 22 9 17 83 8 99 +187923899: 6 9 6 6 994 5 7 820 7 3 6 +204934: 16 9 8 601 34 +10378874447: 3 3 59 3 3 642 34 814 8 +34485753: 43 1 8 1 4 642 111 +288644076000: 48 25 635 622 609 +630886: 6 8 7 5 9 1 638 9 5 40 3 3 +110160: 37 7 4 90 958 90 +24186155: 1 3 4 85 8 6 774 29 7 +402954366: 6 3 9 56 2 3 2 35 1 9 3 54 +6755973358: 66 1 5 25 34 1 7 3 358 +67363154: 126 685 8 45 780 3 6 5 +285964221: 952 598 1 616 1 3 23 +264232: 7 4 12 1 2 33 +71286: 1 99 4 7 1 648 6 1 +734329140: 688 46 32 9 1 40 +51943: 3 787 22 1 +476596: 6 5 880 64 8 6 5 3 85 +1380: 46 6 5 +28011827935397: 9 5 1 2 925 1 7 955 3 9 7 +294720: 163 2 3 8 558 3 1 96 +67: 8 8 3 +9984210999: 1 38 870 3 302 3 7 2 9 +3998862: 447 892 2 7 2 213 +4142481: 23 8 9 5 3 1 8 5 7 9 +11784606857757: 8 2 36 7 5 1 8 761 973 9 +1329: 2 9 19 4 9 +271929206545: 33 103 8 920 64 60 85 +9949987386: 479 261 239 301 333 +588095365: 188 465 38 87 69 85 +1693813434: 470 5 204 6 69 2 5 4 34 +592803534: 988 75 441 8 8 +22449547836: 747 1 2 9 8 3 3 2 47 836 +1186939560: 471 6 2 7 65 3 1 2 1 3 5 2 +64276258: 172 8 39 7 974 292 50 +22212455826: 43 84 7 7 883 993 99 +1215695: 9 707 13 856 767 +38097: 824 23 9 53 2 6 63 +24728256: 6 1 848 5 9 4 19 2 3 9 1 8 +70704: 1 740 7 269 4 +963: 3 8 3 86 5 4 6 1 817 3 7 2 +3588456480: 42 71 972 140 6 +298882200: 877 8 71 98 2 6 +839197668: 8 3 4 6 3 447 8 3 9 5 668 +613568: 1 96 1 3 2 48 2 6 75 8 8 +2008: 28 5 82 9 8 +6077386: 57 2 26 1 6 957 78 11 +1408: 602 6 6 4 790 +2684664: 9 73 86 6 54 +2416491814: 9 33 54 38 7 568 +95367: 6 6 84 33 5 36 2 2 9 3 72 +2952934851480: 628 284 47 51 480 +496516537: 58 4 1 371 85 +20002192714: 36 3 996 8 5 98 94 716 +12557514: 67 99 7 73 1 4 45 7 4 3 6 +42204: 76 71 47 3 2 603 63 84 +5540549: 551 20 285 1 13 35 +14458: 65 27 8 182 219 17 +222257560552066: 39 77 4 80 54 8 740 66 +49621070: 9 6 2 75 2 7 5 8 5 914 7 3 +286985509: 49 748 5 87 2 9 3 342 4 +217459: 3 6 151 4 4 +10880465149926: 1 34 3 267 30 246 81 +115569756542: 9 7 7 3 56 97 565 34 8 +19540053: 47 664 6 626 89 +1100135: 13 65 1 563 17 14 15 +14116534: 59 4 7 40 9 72 7 +13978309529631: 774 25 885 4 54 4 1 51 +5120326: 1 510 9 1 326 +886738104: 78 29 27 84 58 63 +192490: 4 92 2 395 95 +12856134373: 9 50 5 2 7 7 60 2 2 4 713 +28761847: 6 98 1 3 91 43 6 3 4 5 7 +212784: 15 92 22 886 93 +1064532685: 4 1 9 211 6 6 1 805 6 85 +459693111: 3 9 57 39 548 29 4 8 4 +655199: 3 3 2 2 60 60 +67369104: 5 279 981 7 3 1 7 5 2 3 9 +38174772: 190 87 386 2 3 +1109: 1 99 622 19 369 +1200192: 6 8 23 135 5 25 4 48 +3870126: 2 43 36 6 319 9 1 962 +357278904016: 2 6 4 7 2 2 3 3 105 48 1 5 +8833713: 2 702 392 81 32 145 +4717442: 51 2 7 5 9 3 94 967 81 +7429: 73 3 9 7 3 +22441693: 85 738 7 923 51 6 4 +1460217600: 9 99 9 8 113 9 303 5 3 4 +80049209453: 85 8 4 66 615 8 9 4 52 +1096445328: 42 181 6 2 73 92 282 +20315988: 4 521 2 8 18 1 539 +4727090370: 1 5 999 6 5 91 913 56 +11976662: 1 1 8 5 8 2 6 70 1 98 6 64 +7787171057: 77 85 1 1 8 1 126 105 5 +320686096980: 7 7 3 8 3 8 78 8 4 4 980 +3211: 7 205 8 27 1 13 +20117: 6 2 42 8 3 8 469 5 4 277 +47766: 1 8 4 8 544 7 6 4 6 65 7 3 +20333: 216 31 82 2 77 +1408512: 896 4 393 +335067840: 6 1 4 40 9 2 7 6 5 864 90 +245970960: 6 85 75 501 646 +64525323: 595 2 9 9 6 183 9 5 3 4 3 +1515151: 16 2 5 1 1 9 91 1 +107014412422: 6 818 918 218 21 +6050: 458 697 1 53 5 5 +10888863804: 493 6 1 2 91 27 886 +14863629: 89 8 258 508 8 1 2 6 31 +4956: 4 603 7 340 6 +272910: 5 1 28 8 174 88 648 +48097: 7 1 2 1 43 6 86 1 9 98 4 +152711714183: 90 4 62 4 54 181 20 9 7 +3935475: 80 7 2 211 1 7 +27681695038: 2 1 9 6 67 329 64 351 8 +124189056: 643 6 67 96 3 +34955586: 695 4 11 17 1 5 +576720450: 2 8 1 190 5 1 36 5 9 4 5 3 +106531141: 93 44 928 311 39 +45391948: 3 601 8 9 54 8 3 7 8 2 8 +1902304964: 3 97 35 5 8 37 37 57 5 +1245: 816 339 8 6 2 4 9 2 5 54 +91641578: 29 8 395 837 741 +86158666362: 2 6 70 8 220 848 76 2 +75426432: 3 121 2 9 3 3 6 2 89 8 8 +45203862847: 8 753 54 39 3 11 8 4 1 9 +1503: 7 742 688 63 6 +1910635650: 1 9 5 5 5 64 6 5 6 3 737 5 +545187456: 5 6 4 471 87 8 2 8 384 9 +857376: 5 99 916 9 1 +44770397: 63 95 7 7 4 94 +28047: 7 46 4 86 9 2 +18585855: 6 9 5 343 5 5 8 96 +38352707369: 77 1 2 9 4 113 5 867 2 1 +6310322137: 62 184 918 75 47 1 35 +109342: 467 3 78 63 1 +19118473: 8 47 2 5 4 94 12 4 +82914917: 3 853 324 5 8 1 4 16 16 +57748841: 577 4 586 298 2 +3251746104: 5 50 60 17 72 82 9 13 +178494: 1 62 67 9 728 64 +50056: 641 9 2 3 483 8 +47350: 26 3 759 159 50 +67593: 4 2 1 84 21 372 +83376911: 36 386 6 91 2 +335413587: 9 2 77 55 36 44 +70364: 92 6 718 +132846367118: 632 6 7 3 367 119 +547675: 1 9 537 586 89 +31931928: 1 73 682 862 49 +35304: 27 94 5 6 1 27 755 8 +531051: 1 434 8 4 9 19 2 2 8 78 5 +461616611: 6 833 5 9 549 5 670 8 6 +343363: 6 6 8 37 16 29 4 +25480: 3 1 11 26 70 +1653303: 14 9 574 17 586 13 +678732: 2 9 5 3 6 7 1 89 8 4 155 5 +6796293: 190 48 8 4 54 43 69 +12842869456864: 6 4 214 3 47 27 5 7 1 6 4 +14397007437: 568 267 12 879 5 9 1 +880304454: 2 4 104 7 6 16 3 76 8 6 8 +18072257552: 8 61 5 668 41 930 59 +31216833: 6 24 33 27 26 5 70 +33643649656: 8 4 7 8 945 491 49 653 +365450360: 804 99 22 1 54 9 35 85 +5946173: 76 46 869 6 173 +213978: 2 17 79 167 75 +5037020668: 2 25 37 50 6 5 8 4 54 6 5 +653934708: 5 3 5 33 3 1 703 8 162 +3896904: 7 793 43 5 104 82 +3226: 437 222 341 613 2 +81942324826: 7 6 657 6 8 6 8 7 53 2 1 4 +2160750: 837 5 43 2 3 +198732: 31 659 96 4 3 +170898663: 18 9 88 6 50 57 9 300 +28120999: 4 1 1 59 40 5 15 5 997 +11379563100: 81 6 4 228 113 9 2 5 2 6 +342732047: 52 589 1 51 6 31 3 1 1 7 +9161: 868 44 99 7 9 +44467433: 3 947 499 7 430 +3435401: 356 965 2 +258803: 2 7 5 3 308 4 3 8 50 7 8 7 +31256107765586: 18 5 818 488 65 87 86 +209945274: 60 30 99 6 2 7 561 +9014976: 37 36 47 6 24 +12718835413: 61 28 50 6 14 911 11 +8687463: 24 6 791 6 3 +22272: 2 1 2 232 24 1 +15225570: 16 9 20 2 9 9 4 456 44 5 +1193094: 8 731 4 2 51 +16046: 90 70 51 1 24 8 66 8 +4403024730: 4 7 76 7 7 729 90 18 30 +28336268: 3 68 77 2 67 +301438993: 557 2 256 77 7 9 +161487419: 702 1 169 23 532 +194081938: 4 6 6 6 50 2 16 6 643 2 +1746520: 3 9 5 3 7 5 651 3 6 7 6 41 +67383: 673 8 3 +50297: 6 1 10 6 303 +497381797: 69 6 4 5 9 6 9 518 79 7 +45: 8 8 2 7 6 +490: 7 6 1 5 7 +4363239498: 5 6 6 2 5 5 6 7 8 680 6 76 +530469: 3 6 858 41 7 45 85 3 27 +7214986: 9 88 1 8 73 410 54 809 +967324: 161 6 1 31 8 4 +3905608: 185 95 21 56 1 97 +3315232373: 7 1 8 8 74 323 71 2 +1952584313: 6 1 5 4 14 2 84 2 7 7 24 3 +697: 623 5 5 66 +213749: 31 86 8 46 9 +4587919: 12 41 288 3 919 +462318: 77 6 182 58 79 +13958: 55 4 752 89 5 +684975: 743 1 61 281 15 +276312: 50 44 5 9 69 29 +759155: 8 4 5 9 1 355 2 2 436 6 1 +2140786: 201 834 47 44 406 +82350: 79 30 1 7 6 675 +292818: 39 82 1 49 9 1 48 9 75 +20825303: 59 40 7 8 7 180 20 +60780774: 50 6 97 8 202 3 5 3 5 9 7 +20537398770: 600 224 6 9 2 3 2 764 +259627: 63 7 3 220 67 476 3 +436821: 1 50 5 5 780 7 4 5 4 3 6 3 +1664391: 35 432 2 2 891 3 +330191: 714 5 92 90 611 +2525156: 7 865 4 6 3 3 4 8 167 5 1 +1653324: 3 8 3 66 495 23 +97856: 26 46 195 217 32 +612: 62 481 63 3 +44581500402: 56 3 434 1 795 219 +32075707507: 606 6 7 7 1 7 6 3 8 5 50 6 +1136: 6 8 1 99 5 +9122831: 19 16 784 93 27 93 40 +1279500544: 97 2 8 35 471 4 1 3 98 1 +901382145: 90 13 819 2 47 +45227: 864 4 1 2 3 67 25 7 1 20 +10812221: 2 54 114 8 21 +93159765: 1 930 5 976 5 +1404522241: 4 3 3 7 994 90 1 5 8 9 4 +78765: 9 99 1 71 8 73 +10764570360287: 8 7 4 6 91 40 587 5 7 4 7 +285978: 27 45 110 4 75 +15955199920: 87 5 24 79 81 8 55 4 2 +112128: 365 8 42 3 27 +3009824160: 511 4 2 8 8 76 29 632 5 +16434357: 9 74 3 66 35 8 +39529: 1 6 6 29 41 4 3 9 +156026125: 9 9 57 72 2 8 1 32 33 4 9 +73428768: 213 4 57 21 1 9 8 +11712035: 42 818 91 340 55 +1272102: 9 5 96 3 7 50 6 94 +13573956: 31 4 9 391 789 +242324: 757 5 8 8 84 +3130: 330 3 2 3 693 757 675 +31358486736526: 3 919 8 8 867 365 1 1 4 +23163457: 10 3 5 3 2 9 4 8 4 1 59 57 +3367569: 1 7 29 6 409 8 7 532 8 1 +44432361044: 54 85 135 64 3 5 6 44 +29581531374898: 295 815 31 3 743 5 9 5 +2481264485: 2 479 34 192 4 415 67 +36332832695: 1 5 4 2 476 5 1 230 99 5 +10150657: 4 2 8 53 570 7 9 17 6 65 +83620425: 2 4 9 475 707 +38795: 920 7 6 143 12 +54222210: 9 92 89 5 81 146 +324091: 4 3 385 2 1 9 7 70 5 8 18 +10533762: 59 1 26 5 15 8 5 3 9 +4884809762: 2 3 3 3 8 860 91 2 4 6 3 +2642160: 2 3 4 3 8 63 8 286 5 8 3 3 +197589: 6 8 5 736 9 158 59 +12024: 110 6 8 2 9 +75189063: 888 2 672 3 8 4 3 2 8 7 5 +7551584667: 304 92 83 21 27 +5368844: 49 791 581 36 11 8 +7590272: 5 2 5 5 1 2 6 9 683 582 9 +25835640: 5 33 3 956 9 276 6 4 1 +18285992: 3 58 967 310 20 2 +9099208719996: 8 940 121 871 9 9 94 +1013436: 66 415 37 6 +8452: 3 9 7 5 4 +902510: 5 722 25 7 3 +69876: 7 55 32 14 647 +2744: 20 6 63 49 34 +7489418400: 2 551 1 217 3 78 8 49 2 +2151596379: 30 4 9 3 9 3 9 98 8 882 7 +234735729: 234 73 5 726 1 +78987524814: 76 167 9 691 9 5 8 14 +113571754565: 81 2 57 48 956 792 3 5 +3052805: 525 37 96 58 91 +304167: 87 93 124 161 3 +1051155: 558 5 308 89 7 3 53 47 +5066612: 6 261 811 2 470 1 5 6 6 +628014: 51 5 92 5 43 2 3 1 5 8 6 6 +1770831233: 76 233 3 1 231 +4235592666: 423 5 5 92 665 4 +6784: 4 77 21 4 8 8 +13258948651: 929 8 713 2 654 +14466762: 3 15 18 17 27 +958180: 7 4 28 8 7 188 180 +693430: 7 33 3 421 11 +35127451: 89 7 310 291 37 51 +14920080: 3 5 71 69 5 415 56 1 +6161459348: 3 7 8 584 41 628 +74584001436: 5 98 5 88 9 8 8 137 6 6 +680498063439: 921 51 7 980 634 33 6 +1010100: 6 3 7 9 424 54 2 78 4 4 6 +27480817504: 916 30 8 17 45 7 44 3 +33052: 7 988 6 33 19 +3297: 289 99 8 146 43 4 +13673373568: 5 2 6 4 8 81 5 2 5 3 568 3 +91986147503: 415 8 4 3 1 4 299 7 4 79 +887695534: 7 7 634 8 80 37 38 36 +108702: 5 49 303 2 96 +10115040: 71 4 3 6 6 5 2 624 +29945841: 148 2 9 56 573 2 20 +1294927056: 4 2 5 119 9 9 4 6 93 74 7 +2970: 87 797 54 26 26 3 +2818449110: 7 8 368 982 2 5 +504694019: 38 8 2 891 15 3 3 40 19 +3314292: 33 133 9 92 +521850: 71 96 1 8 71 7 6 +1337171983: 329 9 4 8 4 7 2 84 7 6 6 1 +64964: 228 1 420 6 3 +8151404809: 97 3 35 32 748 9 +2309014488: 11 9 2 6 329 7 78 1 9 3 5 +1001843: 997 3 8 4 5 93 50 +10989423299: 131 4 2 882 9 78 32 99 +540384177: 6 928 78 13 4 7 +293878: 79 7 3 83 8 308 48 +251376048: 45 226 822 363 63 +16601364: 4 40 5 960 53 64 +2350952: 4 27 43 5 946 5 +847053286: 4 98 434 90 9 918 +2709: 2 9 8 1 17 4 7 495 4 6 4 3 +2263207: 36 984 736 6 9 +10796424750: 8 6 395 23 3 75 1 +248133271: 7 9 38 3 7 1 654 7 4 9 8 +28882553: 38 95 4 5 2 8 1 30 623 +33817833704: 403 17 57 878 91 954 +1531428: 6 96 664 393 4 +3373286: 290 2 6 7 83 +72660260: 4 8 1 59 9 3 6 583 19 6 1 +5143084626: 8 984 164 64 4 4 623 +42799: 4 18 92 49 860 +280854: 655 77 11 21 18 +64059: 54 73 4 489 +1976: 95 72 34 8 9 95 +43345725708: 91 62 3 9 247 192 2 6 +1697645278: 305 9 475 186 7 6 9 9 4 +27892: 77 5 7 2 46 892 +33528: 2 8 76 2 6 +26498810: 441 59 5 61 7 6 +458616828: 979 950 52 25 9 +6009386: 48 92 9 5 853 +666688: 5 8 71 6 61 8 42 27 +644785225: 81 487 8 495 68 6 77 5 +657279163352: 9 927 7 5 58 276 703 8 +1066: 198 5 2 72 2 +81578: 79 93 9 11 662 +281: 8 85 3 1 2 +7288: 7 4 44 77 9 +3873096: 3 641 579 2 774 +227429: 2 217 8 8 5 8 1 3 8 8 1 36 +11260555295: 918 538 6 38 93 2 +56797885950887: 873 81 363 650 887 +1700538: 5 9 583 451 106 6 534 +2143: 2 986 547 545 63 +117304: 2 9 62 2 86 +19233: 21 9 3 2 1 3 +858728: 8 5 83 3 88 2 39 +174642220249: 6 3 7 33 60 8 8 860 7 4 9 +33150846: 65 250 30 68 846 +1043570077: 9 1 325 493 227 +1003068: 618 2 5 9 7 6 86 7 2 9 +1118262560: 2 994 747 7 12 5 +24451135158: 73 587 745 858 446 +1690468099494: 8 555 52 38 9 9 492 +122849868: 6 16 4 354 4 8 67 9 417 +65292: 2 63 29 4 +364874424: 265 3 73 7 5 1 1 4 6 1 7 7 +92920996540: 891 421 4 983 9 6 2 7 8 +983596468: 6 2 773 98 77 8 +15348995: 377 6 7 4 994 +11322109: 555 204 4 9 62 +21008913: 2 8 2 4 777 23 3 5 26 +5511608387: 646 878 7 6 8 5 5 5 9 5 3 +38885805634: 617 3 6 5 5 41 9 2 1 3 7 4 +774117: 8 462 27 61 27 +1154452731: 2 74 78 5 2 731 +15866020576: 3 32 276 629 4 952 +2828343: 880 6 76 4 42 63 +2191427: 14 3 513 368 30 1 +13067: 9 71 2 1 9 8 2 932 183 +4962424884: 6 2 271 2 4 21 3 591 6 6 +358305: 39 81 3 5 3 +65118348247: 911 460 674 106 7 +5801335: 3 5 2 7 19 80 21 5 335 +1738801: 68 1 36 70 2 +21307: 4 760 2 7 4 9 +92236167: 96 155 523 6 3 998 9 +8770607861: 8 7 70 607 8 61 +16293412126: 7 9 898 8 3 8 27 7 2 6 4 6 +325741871: 3 7 9 4 340 70 1 7 643 +27855783: 5 97 72 693 646 60 +101072: 146 690 48 5 279 +419051520997: 89 35 536 24 875 +305418037: 16 5 65 7 6 770 3 132 +755099: 744 44 8 65 877 +3188444: 5 5 2 16 5 24 383 6 9 5 +2054: 232 6 8 59 91 +481995: 94 2 39 9 5 +64736: 7 3 1 9 9 1 43 3 44 6 1 56 +1121759: 70 381 9 7 8 6 8 6 1 7 7 9 +163260849: 90 907 2 670 86 7 9 78 +1266721: 8 1 8 3 65 4 414 7 3 5 4 4 +48397230: 7 6 8 6 8 5 6 57 6 7 3 126 +26216: 12 64 23 11 7 3 92 +453475: 1 5 17 52 31 85 +27363: 79 1 341 19 64 +191920: 9 2 4 420 47 402 464 8 +3973459430: 1 17 201 7 33 21 7 430 +33778428: 190 584 4 613 76 +163842074: 6 7 9 3 9 9 893 3 785 5 9 +182: 2 165 8 7 +39786132: 26 51 5 6 6 130 +470868123239: 8 82 4 541 3 2 54 482 2 +5820: 332 632 6 30 6 +8729763849: 5 91 995 51 448 4 9 +25984365: 884 1 59 33 835 +56955254: 5 564 54 916 337 +1710: 4 46 545 2 317 782 7 7 +97677863003: 50 179 817 786 300 2 +306965793: 71 288 36 1 417 +563200: 7 3 628 120 88 +519115509: 4 44 8 7 5 98 71 3 +2013797035813: 67 506 307 9 660 1 1 +103324: 8 1 27 478 73 +198216: 83 28 85 669 7 +94090: 54 53 9 7 97 +440928: 9 76 57 91 33 +586: 1 74 8 504 +1239389: 233 2 2 3 879 +3524032016: 3 8 2 11 307 8 7 8 8 5 2 8 +12327037: 35 22 5 7 37 +171779832: 4 89 17 5 652 697 +580729169176: 7 10 2 591 146 147 8 +7738083: 229 6 84 392 5 +6428270: 686 764 95 1 231 18 2 +213249: 809 1 883 126 44 1 13 +317722752: 3 4 5 3 646 6 778 4 2 2 6 +20592132: 68 83 4 299 2 +1883551824: 4 5 9 1 8 9 3 5 890 6 8 24 +6464916: 9 922 8 868 6 46 +58421376: 663 2 46 3 38 3 332 78 +9543117: 24 7 3 1 44 70 11 64 7 9 +12870: 9 8 7 9 390 +485867969623: 8 603 994 9 6 195 8 8 +105315: 36 8 99 31 72 9 413 +8284: 4 5 17 5 91 19 +883213593: 7 8 2 7 7 1 2 167 1 7 9 8 +41864454: 54 88 60 87 54 +39433286146: 1 3 144 3 5 2 76 7 3 46 +78733223: 9 5 828 3 8 1 621 1 9 4 +25970: 41 1 5 52 265 +4308565248: 6 871 4 57 113 8 4 +763796508: 60 2 5 318 61 9 35 7 9 4 +21358: 2 6 3 99 59 +3507309037: 16 951 93 1 390 2 7 5 6 +88911062622: 56 70 85 3 74 62 620 +13623775: 5 3 3 641 6 4 885 84 1 +414677448: 414 6 770 1 4 50 +108162: 1 4 21 88 17 1 1 6 486 9 +18345600678: 65 98 960 3 677 +211524321121: 9 3 6 2 41 62 8 96 70 73 +20589489: 935 2 59 627 22 +198069200: 2 58 12 5 889 16 +23229919: 5 790 7 4 2 4 7 3 386 3 +39436947: 3 925 9 7 7 3 20 4 7 1 2 2 +143712905: 920 292 155 765 5 +930461: 860 68 5 2 9 9 3 8 2 1 2 +63976109863: 887 9 2 7 131 137 33 8 +17117103548: 2 85 66 91 354 2 4 +81246444492: 3 809 4 641 7 26 1 492 +307812: 8 5 84 8 732 7 1 2 3 4 3 +6787: 4 3 8 76 6 47 442 89 6 +98648197: 72 9 54 46 152 2 198 +168209: 90 55 580 2 9 +1793: 9 91 61 4 899 8 2 +362178006: 36 38 75 353 8 +100825683: 80 30 95 396 414 41 3 +27088: 61 33 35 96 8 +347904: 4 53 768 +91426378: 52 51 13 67 345 97 +24541687675: 2 45 527 2 6 29 19 82 7 +993266: 4 2 103 16 44 66 +2555574: 56 455 5 574 1 +3810459059: 4 5 4 2 96 2 5 82 6 4 5 9 +36468: 91 4 68 +57715681: 31 4 365 816 2 +993036: 357 276 769 2 3 +23381016912: 953 70 757 3 629 16 +9369: 84 9 6 9 +8440459776: 2 1 4 80 2 32 8 92 72 62 +4881608697731: 4 9 1 9 46 3 4 6 7 1 7 728 +117610: 64 47 25 6 56 38 +241726884: 9 37 8 72 426 6 769 81 +14946245: 3 5 5 8 73 803 1 3 6 96 2 +3026319: 4 9 689 9 479 +104261: 467 81 223 15 19 9 5 +24114775173: 6 693 357 8 755 267 2 +55835511406: 83 738 68 7 511 406 +202914: 86 1 582 94 4 2 +220344: 7 5 751 564 4 23 3 1 +975114559: 3 7 88 7 4 3 981 5 4 1 8 8 +5190749142: 52 1 41 9 9 1 3 4 68 3 4 3 +3121654437: 9 35 991 44 36 +259710723718: 5 347 11 15 162 3 840 +267304573: 8 3 2 41 12 148 9 5 73 +46092: 35 47 775 140 9 6 2 1 +459838722: 1 5 133 84 6 6 3 4 92 1 +422154208: 4 1 491 2 47 209 +28957825142: 7 3 448 6 945 1 228 5 2 +69550: 58 59 8 2 773 488 3 50 +423881584: 86 8 2 7 9 71 8 954 7 6 4 +435262699: 8 5 2 8 187 8 9 32 3 3 3 2 +536481853: 958 56 18 25 29 +163018: 815 2 18 +151801017: 8 9 68 807 31 +317445283: 8 53 6 7 7 6 52 9 8 1 801 +6693248: 660 9 32 4 8 +3037037133: 72 187 7 37 703 713 2 +406681618: 38 21 245 81 619 +1182198: 841 1 234 5 6 +1639: 15 1 59 75 3 1 +21496: 141 68 2 99 607 +4014: 59 387 9 +30336922: 94 5 3 8 4 8 854 7 9 33 2 +7833789: 87 9 374 4 3 5 +39981905: 25 832 6 3 466 376 1 1 +523774: 9 3 1 45 3 7 3 2 8 240 5 +278262534: 9 2 45 9 1 7 3 1 6 83 9 6 +594309628: 594 30 953 5 93 +1401025277578: 39 8 44 8 15 3 6 697 4 2 +11871: 11 9 9 10 58 9 6 +11059783: 763 42 475 3 2 72 4 6 +15116568192: 234 52 29 864 528 +4434: 71 1 1 27 6 +4188969400770: 50 7 51 835 8 9 4 2 5 18 +170: 1 18 55 +3279: 3 2 7 9 +50575: 7 3 6 7 2 4 844 431 9 5 5 +6049688: 9 80 2 84 8 +656467638703: 6 564 67 638 705 +24565893: 7 87 8 1 8 8 7 5 4 6 287 3 +23820995: 50 8 2 638 63 872 73 +80039400005: 26 5 44 750 8 646 1 5 +611696: 275 278 8 86 10 +179: 3 8 9 26 54 +187939: 18 1 6 9 7 7 8 5 58 332 9 +12840981: 1 8 7 8 535 1 1 2 4 853 2 +54341: 52 6 6 87 51 9 2 8 7 301 +967: 112 4 512 7 +38049: 3 804 8 +795: 6 52 30 9 3 +11859: 3 1 32 86 852 +4435056: 5 635 67 8 171 +37365640: 3 292 38 555 397 8 +78655270: 9 1 2 7 27 9 917 4 6 5 5 +941684: 2 35 42 4 4 +302874592: 6 5 287 459 2 +9264: 86 2 7 3 2 447 170 2 8 +95: 6 52 37 +268834786: 4 88 753 352 603 902 +6909118: 2 2 1 2 8 389 4 9 57 372 +7509504: 8 7 2 8 757 4 5 1 6 8 4 8 +361004: 8 64 87 570 8 95 +8574202529: 2 15 4 997 25 28 +787: 2 741 2 6 36 +37666754917: 1 98 74 734 3 53 97 +3808280279: 7 15 7 8 4 5 67 7 2 77 +6055948524: 4 4 6 9 7 4 75 4 8 519 1 7 +338755517830: 5 2 837 5 568 4 5 404 7 +109122588638: 3 400 46 909 774 638 +71930: 7 428 8 8 3 +788647: 2 5 886 4 9 +35762875199: 1 61 2 2 75 1 5 2 2 730 2 +18124: 374 1 402 58 330 4 +3456278: 72 48 199 77 +2685: 20 9 4 591 +99036: 9 8 335 697 4 +15675050267: 114 275 5 50 267 +108600995: 229 6 988 4 8 +122274: 9 392 5 20 624 9 46 6 +730044702: 814 782 8 7 4 3 4 3 1 3 9 +13241870: 6 757 65 267 2 3 +471077320199: 4 1 7 3 8 701 8 8 670 6 2 +31725865643: 64 6 5 45 8 52 1 5 7 643 +9428926: 49 41 4 289 26 +99689: 987 71 916 +167296160741: 5 4 7 96 3 462 9 5 22 3 9 +1340053: 3 2 3 85 9 8 3 70 95 1 4 8 +4903668: 272 6 195 4 671 +1180351: 8 58 239 387 1 +4955137: 830 1 153 4 3 2 7 9 7 +11990: 8 5 9 7 4 18 3 9 69 7 6 5 +13446985: 91 72 18 716 74 +724110: 94 2 2 1 5 59 4 3 31 942 +56109: 148 961 5 66 2 +8028913: 200 7 152 63 4 53 +2641584: 73 906 3 7 4 8 1 8 8 8 2 3 +143505: 1 96 7 58 7 20 5 +1127015: 53 3 350 7 2 +401603: 2 131 318 889 96 568 +234: 2 154 34 43 1 +9590766: 48 5 1 3 846 6 6 +3452: 306 2 11 66 +1430529: 2 110 44 917 9 +94873923: 7 64 47 6 51 92 8 15 +460484427: 3 9 6 6 8 59 5 9 6 2 1 383 +625: 9 4 3 420 164 +128758913: 75 958 684 24 5 70 6 +1088990308917: 7 7 5 398 432 7 8 2 1 3 9 +34976: 7 38 8 400 77 95 +8983256: 58 27 717 11 1 72 2 8 +40900: 4 1 508 3 3 6 39 42 4 5 5 +6686266236: 2 329 786 257 33 +58787: 4 1 2 5 863 8 4 4 8 76 8 3 +128914226745: 1 2 9 78 836 3 6 8 395 3 +419: 8 280 4 42 85 +129098342441: 8 587 28 245 98 2 4 43 +997478: 8 659 8 3 5 83 59 8 142 +201341: 4 82 21 35 4 +1347: 1 281 90 918 57 +30404749: 6 62 6 1 8 8 92 7 41 3 6 +4983: 4 78 6 14 3 1 +833843: 5 6 758 39 4 +16286077: 811 1 7 2 626 77 +622577340: 2 3 651 42 5 759 +702: 2 14 35 7 2 +257108549: 119 935 3 84 968 5 +10656373: 562 23 9 769 91 1 +13293389391: 850 9 2 2 862 785 947 +198606000: 246 1 2 7 5 8 6 4 6 948 2 +7772652: 4 9 3 911 79 1 +21590408051: 2 921 614 51 747 7 7 3 +37153326656: 95 311 8 8 16 6 614 16 +229700: 45 638 8 6 7 7 +50009844: 5 7 1 1 1 301 1 395 852 +152: 5 1 3 +3597311785879: 35 973 11 785 87 9 +122227110: 154 95 52 37 246 5 51 +18124713: 66 4 5 48 658 994 9 4 9 +90902: 11 92 860 94 86 +2652432899: 92 6 59 48 891 2 3 1 +1676: 9 8 61 3 2 4 418 71 98 1 +39951147: 44 385 5 9 61 89 +209614080: 1 6 8 9 6 8 4 5 7 95 2 544 +606458715: 8 5 666 9 1 2 467 52 7 7 +9558776: 34 878 16 3 1 5 8 8 56 +3424: 4 25 4 78 48 +149: 8 4 60 1 57 +12700428: 3 9 3 2 51 6 7 3 36 77 6 2 +7665133: 991 31 75 47 86 +21986: 1 223 96 7 570 1 +46931769062: 7 17 713 7 3 9 8 7 3 7 7 +40929106510: 6 5 50 1 827 61 9 9 3 7 3 +31086928: 6 6 471 688 8 48 183 +7116070: 3 4 947 57 76 10 13 +54096: 33 6 7 23 +1430826: 85 362 64 7 1 5 8 5 6 5 2 +442152: 4 44 2 276 9 +29440969: 89 875 9 8 78 6 7 607 +75669678: 7 5 7 41 1 53 6 239 414 +939849096: 6 736 9 8 7 49 6 9 79 8 +27390016949: 1 6 92 6 62 2 443 4 4 7 +3110383666: 1 2 13 954 73 28 313 +6636272641: 53 2 2 6 1 6 2 1 6 56 639 +453812261177: 31 509 2 7 823 175 +28605822: 8 57 99 41 324 69 9 9 diff --git a/2024/07/solution.nix b/2024/07/solution.nix new file mode 100644 index 0000000..cc3b14a --- /dev/null +++ b/2024/07/solution.nix @@ -0,0 +1,193 @@ +{lib, ...}: input: rec { + + index = i: arr: lib.elemAt arr i; + + data = input + |> lib.trim + |> lib.splitString "\n" + |> map (line: line + |> lib.splitString ": " + |> (parts: { + target = index 0 parts |> lib.strings.toInt; + vals = parts + |> index 1 + |> lib.splitString " " + |> map lib.strings.toInt; + }) + ) + ; + +/* + + +obvious answer: +try every combination of + and * between each number. +that is O(2^n) +I expect part 2 will introduce more operators, making this very slow. + +slight optimisation idea: + +all numbers in the input are positive integers. + +if you multiply all numbers, and the result is less than the target, +it's not possible. + +if the sum of all numbers is greater than the target, it's not possible. + +but won't work where there are 1's in the list! + +we could try going right to left and dividing and subtracting, +the complexity is the same though + +*/ + + total = builtins.foldl' builtins.add 0; + multiply = builtins.foldl' builtins.mul 1; + + isPossible = {target, vals, running ? null}: + let + nextVals = (lib.sublist 1 (builtins.length vals) vals); + in + if isNull running then isPossible { + inherit target; + running = (index 0 vals); + vals = nextVals; + } else let + val = index 0 vals; + sum = running + val; + multiple = running * val; + isEnd = builtins.length vals == 0; + in if isEnd then false else + if sum == target || multiple == target then true else + isPossible { + inherit target; + vals = nextVals; + running = sum; + } || isPossible { + inherit target; + vals = nextVals; + running = multiple; + } + ; + + part1result = data + # |> builtins.filter isInRange + |> builtins.filter isPossible + |> map (l: l.target) + |> total + ; + + /* Part 2 + + i had a feeling part 2 would introduce new operations. + this increases the complexity to O(3^n) + + Really wish I went right to left now! + going right to left, we could check if the target ends in the running value + + concat will always increase the value of a number. + concat will always increase the value more than multiplying. + + */ + + concat = a: b: lib.strings.toInt (toString a + toString b); + + isPossible2 = {target, vals, running ? null}: + let + try = v: isPossible2 { + inherit target; + vals = (lib.sublist 1 (builtins.length vals) vals); + running = v; + }; + val = index 0 vals; + in + if isNull running then try val else + if builtins.length vals == 0 then target == running else + if running > target then false else + let + sum = running + val; + multiple = running * val; + concatination = concat running val; + in + try sum || try multiple || try concatination; + + part2result = data + |> builtins.filter isPossible2 + |> map (l: l.target) + |> total + ; + + /* + lets try right to left + */ + + isPossibleFast = ops: {target, vals, running ? target}: + (let + try = v: isPossibleFast ops { + inherit target; + running = v; + vals = (lib.sublist 0 (builtins.length vals - 1) vals); + }; + val = index (builtins.length vals - 1) vals; + isFinal = builtins.length vals == 1; + in + builtins.any ({op, fin, check}: ( + let vars = {inherit target running val isFinal; result = op vars;}; in ( + if isFinal then fin vars else check vars && try vars.result + ))) ops + ); + + isWhole = a: a == builtins.floor a; + toFloat = a: a * 1.0; + floatDiv = a: b: (toFloat a) / (toFloat b); + + part1ops = [ + { + op = {running, val, ...}: running - val; + check = {result, ...}: result > 0; + fin = {result, ...}: result == 0; + } + { + op = {running, val, ...}: floatDiv running val; + check = {result, ...}: isWhole result; + fin = {result, ...}: result == 1; + } + ]; + + part1resultFast = data + |> builtins.filter (isPossibleFast part1ops) + |> map (l: l.target) + |> total + ; + + endsWith = a: b: let + full = intToString b; + end = intToString a; + endLen = builtins.stringLength end; + fullLen = builtins.stringLength full; + in endLen < fullLen && builtins.substring (fullLen - endLen) endLen full == end; + + intToString = a: a |> builtins.floor |> toString; + intLength = a: a |> intToString |> builtins.stringLength; + + truncate = a: amt: let + full = intToString a; + remLen = builtins.stringLength full - amt; + in if remLen <= 0 then 0 else builtins.substring 0 remLen full + |> lib.strings.toInt; + + part2ops = part1ops ++ [ + { + op = {running, val, ...}: truncate running (intLength val); + check = {running, val, ...}: endsWith val running; + fin = {target, val, result, ...}: (toString result) + (toString val) == toString target; + } + ]; + + part2resultFast = data + |> builtins.filter (isPossibleFast part2ops) + |> map (l: l.target) + |> total + ; + +} diff --git a/2024/07/solution.test.nix b/2024/07/solution.test.nix new file mode 100644 index 0000000..b634672 --- /dev/null +++ b/2024/07/solution.test.nix @@ -0,0 +1,91 @@ +{describe, it, ...}: +let + pkgs = import {}; + solution = import ./solution.nix pkgs; + exampleInput = pkgs.lib.readFile ./example.txt; + example = solution exampleInput; + parsedExample = [ + {target = 190; vals = [10 19];} + {target = 3267; vals = [ 81 40 27 ];} + {target = 83; vals = [ 17 5 ];} + {target = 156; vals = [ 15 6 ];} + {target = 7290; vals = [ 6 8 6 15 ];} + {target = 161011; vals = [ 16 10 13 ];} + {target = 192; vals = [ 17 8 14 ];} + {target = 21037; vals = [ 9 7 18 13 ];} + {target = 292; vals = [ 11 6 16 20 ];} + ]; +in [ + (describe "part 1" [ + (it "get's lines" { + actual = example.data; + expected = parsedExample; + }) + (it "calculates sum of a list" { + actual = map example.total [ + [1 1 1 1] + [1 2 3 4] + ]; + expected = [ + 4 + 10 + ]; + }) + (it "calculates mult of a list" { + actual = map example.multiply [ + [1 1 1 1] + [1 2 3 4] + ]; + expected = [ + 1 + 24 + ]; + }) + (it "checks if a list is possible" { + actual = map example.isPossible parsedExample; + expected = [ + true true false false false false false false true + ]; + }) + (it "gets the correct result" { + actual = example.part1result; + expected = 3749; + }) + ]) + (describe "part 2" [ + (it "concats numbers" { + actual = example.concat 12 34; + expected = 1234; + }) + (it "checks if a list is possible" { + actual = map example.isPossible2 parsedExample; + expected = [ + true true false true true false true false true + ]; + }) + ]) + + (describe "faster" [ + (it "checks number ends with" { + actual = example.endsWith 6 486 ; + expected = true; + }) + (it "cuts end off number" { + actual = example.truncate 7515 2; + expected = 75; + }) + (it "checks if a list is possible with add and mult" { + actual = map (example.isPossibleFast example.part1ops) parsedExample; + expected = [ + true true false false false false false false true + ]; + }) + (it "checks if a list is possible with concat" { + actual = map (example.isPossibleFast example.part2ops) parsedExample; + expected = [ + true true false true true false true false true + ]; + }) + ]) + +] diff --git a/2024/flake.nix b/2024/flake.nix index e41b41e..a9eea02 100644 --- a/2024/flake.nix +++ b/2024/flake.nix @@ -17,28 +17,31 @@ in { example = (solution example); sample1 = (solution '' -.#.. -.^#. -.#..''); +.#. +..# +#^. +.#.''); sample2 = (solution '' -.#...... -........ -.....#.. -#....... -..#.#... -...^....''); +.#.... +...... +.....# +#..... +..#.#. +...^..''); real = (solution input); }; - packages.x86_64-linux.watch = tix.watch { - cmd = '' - nix run .\#test --impure --extra-experimental-features pipe-operators - ''; + day07 = let + solution = import ./07/solution.nix pkgs; + example = (pkgs.lib.readFile ./07/example.txt); + input = (pkgs.lib.readFile ./07/input.txt); + in { + example = (solution example); + real = (solution input); + test = tix.run [ + ./07/solution.test.nix + ]; }; - packages.x86_64-linux.test = tix.run [ - ./solution.test.nix - ]; - }; }