From 0c39e2f0c67989c046c510da84c4e8caf76d8ffb Mon Sep 17 00:00:00 2001 From: Tristan Date: Mon, 16 Dec 2024 10:00:40 +0000 Subject: [PATCH] 2024 day 16 partially in nix --- 2024/16/example.txt | 15 ++++++++++++ 2024/16/example2.txt | 17 ++++++++++++++ 2024/16/solution.nix | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2024/flake.lock | 4 ++-- 2024/flake.nix | 4 +++- 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 2024/16/example.txt create mode 100644 2024/16/example2.txt create mode 100644 2024/16/solution.nix diff --git a/2024/16/example.txt b/2024/16/example.txt new file mode 100644 index 0000000..2c21676 --- /dev/null +++ b/2024/16/example.txt @@ -0,0 +1,15 @@ +############### +#.......#....E# +#.#.###.#.###.# +#.....#.#...#.# +#.###.#####.#.# +#.#.#.......#.# +#.#.#####.###.# +#...........#.# +###.#.#####.#.# +#...#.....#.#.# +#.#.#.###.#.#.# +#.....#...#.#.# +#.###.#.#.#.#.# +#S..#.....#...# +############### diff --git a/2024/16/example2.txt b/2024/16/example2.txt new file mode 100644 index 0000000..bc61c57 --- /dev/null +++ b/2024/16/example2.txt @@ -0,0 +1,17 @@ +################# +#...#...#...#..E# +#.#.#.#.#.#.#.#.# +#.#.#.#...#...#.# +#.#.#.#.###.#.#.# +#...#.#.#.....#.# +#.#.#.#.#.#####.# +#.#...#.#.#.....# +#.#.#####.#.###.# +#.#.#.......#...# +#.#.###.#####.### +#.#.#...#.....#.# +#.#.#.#####.###.# +#.#.#.........#.# +#.#.#.#########.# +#S#.............# +################# diff --git a/2024/16/solution.nix b/2024/16/solution.nix new file mode 100644 index 0000000..6629304 --- /dev/null +++ b/2024/16/solution.nix @@ -0,0 +1,56 @@ +{lib, ...}: input: rec { + inherit lib; + inherit (lib) splitString mod; + inherit (lib.lists) findFirstIndex imap0; + inherit (lib.strings) stringToCharacters; + inherit (builtins) elemAt concatStringsSep length elem filter foldl' concatLists floor deepSeq; + + index = i: list: elemAt list i; + index2d = {x, y}: m: m |> index y |> index x; + + init = let + chart = input |> splitString "\n" |> map stringToCharacters; + width = (elemAt chart 0 |> length) + 1; + height = length chart; + startIndex = input |> stringToCharacters |> findFirstIndex (char: char == "S") null; + endIndex = input |> stringToCharacters |> findFirstIndex (char: char == "E") null; + in { + inherit chart width height; + pos = { + x = mod startIndex width; + y = startIndex / width; + }; + goal = { + x = mod endIndex width; + y = endIndex / width; + }; + } + ; + + chartToStr = chart: chart |> (map (concatStringsSep "")) |> concatStringsSep "\n"; + + rotate' = {x, y}: {y = -x; x = y;}; + rotate = {x, y}: {y = x; x = -y;}; + + addVec = a: b: {x = a.x or 0 + b.x or 0; y = a.y or 0 + b.y or 0;}; + multVec = a: m: {x = a.x or 0 * m; y = a.y or 0 * m;}; + + minl = foldl' (a: e: if isNull a then e else if isNull e then a else if a < e then a else e) null; + + search = {pos ? init.pos, dir ? {x = 1; y = 0;}, hist ? [], max ? null}: + let + fwd = search {pos = addVec pos dir; inherit dir; hist = hist ++ [{inherit pos dir;}];} |> add 1; + rot = search {inherit pos; dir = rotate dir; hist = hist ++ [{inherit pos dir;}]; max = fwd;} |> add 1000; + rot' = search {inherit pos; dir = rotate' dir; hist = hist ++ [{inherit pos dir;}]; max = fwd;} |> add 1000; + add = n: a: if isNull a then null else n + a; + in + if pos == init.goal then 0 else + if + elem {inherit pos dir;} hist || + elem {inherit pos; dir = multVec dir (-1);} hist || + index2d pos init.chart == "#" then null else + minl [fwd rot rot'] + ; + + +} diff --git a/2024/flake.lock b/2024/flake.lock index dd3f27c..fa45999 100644 --- a/2024/flake.lock +++ b/2024/flake.lock @@ -3,8 +3,8 @@ "aoc-inputs": { "flake": false, "locked": { - "lastModified": 1734266579, - "narHash": "sha256-fhPoaCWITp2KZtdxm+D9e6GiWf+c3/RGJeiq03/ynfY=", + "lastModified": 1734334657, + "narHash": "sha256-NjsEC/6Mu+i94YPgK4yN0Y5TxhlWwXK5Ev2UzIkfGZo=", "path": "/tmp/aoc-inputs", "type": "path" }, diff --git a/2024/flake.nix b/2024/flake.nix index ac43fc8..67dfb0b 100644 --- a/2024/flake.nix +++ b/2024/flake.nix @@ -25,16 +25,18 @@ inherit (pkgs) lib; - in (lib.range 1 15 + in (lib.range 1 25 |> map (i: let id = lib.fixedWidthNumber 2 i; in { name = "day-${id}"; value = let solution = import ./${id}/solution.nix pkgs; example = (pkgs.lib.readFile ./${id}/example.txt); + example2 = (pkgs.lib.readFile ./${id}/example2.txt); example3 = (pkgs.lib.readFile ./${id}/example3.txt); input = (pkgs.lib.readFile "${aoc-inputs}/${id}"); in { example = solution example; + example2 = solution example2; example3 = solution example3; real = solution input; test = tix.run [