From 506b1112262794297b7811d161a5e61d6ea29c7a Mon Sep 17 00:00:00 2001 From: tristan Date: Wed, 11 Dec 2024 12:18:32 +0000 Subject: [PATCH] 2024 day 11 part1 in nix --- 2024/11/example.txt | 1 + 2024/11/solution.nix | 48 +++++++++++++++++++++++++++++ 2024/11/solution.test.nix | 64 +++++++++++++++++++++++++++++++++++++++ 2024/flake.lock | 4 +-- 2024/flake.nix | 4 +-- 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 2024/11/example.txt create mode 100644 2024/11/solution.nix create mode 100644 2024/11/solution.test.nix diff --git a/2024/11/example.txt b/2024/11/example.txt new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/2024/11/example.txt @@ -0,0 +1 @@ +125 17 diff --git a/2024/11/solution.nix b/2024/11/solution.nix new file mode 100644 index 0000000..ee8e969 --- /dev/null +++ b/2024/11/solution.nix @@ -0,0 +1,48 @@ +{lib, ...}: input: let + inherit (builtins) stringLength concatLists substring foldl'; + + initStones = input + |> lib.trim + |> lib.splitString " " + |> map lib.strings.toIntBase10 + ; + + blinkStone = stone: + if stone == 0 then 1 + else let + strStone = toString stone; + len = stringLength strStone; + in + if lib.mod len 2 == 0 then [ + (substring 0 (len / 2) strStone |> lib.strings.toIntBase10) + (substring (len / 2) (len / 2) strStone |> lib.strings.toIntBase10) + ] + else stone * 2024 + ; + + blinkStones = stones: + if !builtins.isList stones then blinkStone stones + else map blinkStones stones + ; + + blinkTimes = count: stones: lib.range 1 count |> + foldl' (stones: i: blinkStones stones) stones + ; + + flattenStones = {count ? 0, stones}: if builtins.isList stones then + map (stone: flattenStones {stones = stone;}) stones |> foldl' (a: b: a + b) 0 + else 1; + +in { + + inherit initStones blinkStone blinkTimes blinkStones flattenStones; + + part1result = blinkTimes 25 initStones + |> (stones: flattenStones {inherit stones;}) + ; + + part2result = blinkTimes 75 initStones + |> (stones: flattenStones {inherit stones;}) + ; + +} diff --git a/2024/11/solution.test.nix b/2024/11/solution.test.nix new file mode 100644 index 0000000..56329f4 --- /dev/null +++ b/2024/11/solution.test.nix @@ -0,0 +1,64 @@ +{it, describe, ...}: let + pkgs = import {}; + lib = pkgs.lib; + solution = import ./solution.nix pkgs; +in [ + (describe "part 1" [ + (it "gets the stones" { + actual = (solution "125 17").initStones; + expected = [125 17]; + }) + + (it "blinks a 0 to a 1" { + actual = (solution "").blinkStone 0; + expected = 1; + }) + + (it "blinks a even len num to two nums" { + actual = (solution "").blinkStone 1000; + expected = [10 0]; + }) + + (it "blinks a other nums to 2024" { + actual = (solution "").blinkStone 125; + expected = 253000; + }) + + (it "blinks stones once" { + actual = (solution "").blinkStones [125 17]; + # |> builtins.concatLists; + expected = [253000 [1 7]]; + }) + + (it "blinks stones list of lists" { + actual = (solution "").blinkStones [253000 [1 7]]; + expected = [[253 0] [2024 14168]]; + }) + + (it "blinks stones 2 times" { + actual = (solution "").blinkTimes 2 [125 17]; + expected = [[253 0] [2024 14168]]; + }) + + (it "counts 1 stone" { + actual = (solution "").flattenStones {stones = 17;}; + expected = 1; + }) + + (it "counts 2 stones" { + actual = (solution "").flattenStones {stones = [1 2];}; + expected = 2; + }) + + (it "flattens stones" { + actual = (solution "").flattenStones {stones = [[125 1 [1 2 3]] 17];}; + expected = builtins.length [125 1 1 2 3 17]; + }) + + (it "gets the result" { + actual = (solution "125 17").part1result; + expected = 55312; + }) + + ]) +] diff --git a/2024/flake.lock b/2024/flake.lock index 04962c8..fb55822 100644 --- a/2024/flake.lock +++ b/2024/flake.lock @@ -3,8 +3,8 @@ "aoc-inputs": { "flake": false, "locked": { - "lastModified": 1733814937, - "narHash": "sha256-hF+/daBl8ud8FYSvkhOn1QnpmXEYVfoXDpPM7RKW8Zg=", + "lastModified": 1733919427, + "narHash": "sha256-9QvGJVeFT1BfuoGcFUWSr6iq4VHiaEd9SKUrl6k2tzw=", "path": "/tmp/aoc-inputs", "type": "path" }, diff --git a/2024/flake.nix b/2024/flake.nix index 9f99456..815c2cb 100644 --- a/2024/flake.nix +++ b/2024/flake.nix @@ -25,7 +25,7 @@ inherit (pkgs) lib; - in (lib.range 1 8 + in (lib.range 1 11 |> map (i: let id = lib.fixedWidthNumber 2 i; in { name = "day-${id}"; value = let @@ -36,7 +36,7 @@ example = solution example; real = solution input; test = tix.run [ - "./${id}/solution.test.nix" + ./${id}/solution.test.nix ]; }; })