From 394cf9fccb4f29021b0bf81c8e3686ee8d18c4e2 Mon Sep 17 00:00:00 2001 From: tristan Date: Sun, 15 Dec 2024 16:56:29 +0000 Subject: [PATCH] 2024 day 15 part 2 in nix --- 2024/15/example3.txt | 9 ++++++ 2024/15/solution.nix | 73 ++++++++++++++++++++++++++++++++++++++++++-- 2024/flake.nix | 4 +-- 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 2024/15/example3.txt diff --git a/2024/15/example3.txt b/2024/15/example3.txt new file mode 100644 index 0000000..6ee6098 --- /dev/null +++ b/2024/15/example3.txt @@ -0,0 +1,9 @@ +####### +#...#.# +#.....# +#..OO@# +#..O..# +#.....# +####### + + index y |> index x; @@ -11,7 +11,6 @@ init = let parts = splitString "\n\n" input; chart = elemAt parts 0 |> splitString "\n" |> map stringToCharacters; - # ins = elemAt parts 1 |> stringToCharacters |> filter (char: elem char (stringToCharacters "^> splitString "\n" |> map stringToCharacters; width = (elemAt chart 0 |> length) + 1; startIndex = elemAt parts 0 |> stringToCharacters |> findFirstIndex (char: char == "@") null; @@ -24,11 +23,13 @@ } ; - addVec = a: b: {x = a.x + b.x; y = a.y + b.y;}; + addVec = a: b: {x = a.x or 0 + b.x or 0; y = a.y or 0 + b.y or 0;}; objects = { wall = "#"; moveable = "O"; + leftedge = "["; + rightedge = "]"; empty = "."; }; @@ -94,4 +95,70 @@ ; part1result = getScore result.chart; + + init2 = { + chart = init.chart + |> map (map (c: if c == objects.moveable then ["[" "]"] + else if c == "@" then ["@" "."] else [c c])) + |> map concatLists + ; + pos = { + inherit (init.pos) y; + x = init.pos.x * 2; + }; + }; + + move2 = dir: {chart, pos, ...}: + let + nextPos = addVec pos dir; + char = chart |> index2d pos; + switch = state: state // {chart = state.chart |> replace2d pos objects.empty |> replace2d nextPos char;}; + moved = move2 dir {inherit chart; pos = nextPos;} |> switch; + moved2 = if dir.y == 0 || !moved.moved then moved else if char == objects.leftedge then + move2 dir {inherit (moved) chart; pos = addVec pos {x = 1;};} + else if char == objects.rightedge then + move2 dir {inherit (moved) chart; pos = addVec pos {x = -1;};} + else + moved; + in if char == objects.wall + then { + inherit chart pos; + moved = false; + } else if char == objects.empty + then { + inherit chart pos; + moved = true; + } else if !moved2.moved + then {inherit chart pos; moved = false;} + else { + moved = true; + pos = nextPos; + inherit (moved2) chart; + } + ; + + applyIns2 = foldl' (c: ins: let + dir = strToDir ins; + in move2 dir c); + + applyInsList2 = foldl' (chart: ins: + # evaluate each line of instructions one at a time, to avoid stack overflow. + let res = applyIns2 chart ins; in builtins.deepSeq res + res + ); + + result2 = applyInsList2 init2 init.ins; + + getScore2 = chart: chart + |> imap0 (y: row: row + |> imap0 (x: obj: + if obj == objects.leftedge then x + y * 100 else 0 + ) + ) + |> concatLists + |> foldl' builtins.add 0 + ; + + part2result = getScore2 result2.chart; + } diff --git a/2024/flake.nix b/2024/flake.nix index 7362cbd..ac43fc8 100644 --- a/2024/flake.nix +++ b/2024/flake.nix @@ -31,11 +31,11 @@ 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 [ ./${id}/solution.test.nix