2024 day 16 partially in nix
This commit is contained in:
parent
394cf9fccb
commit
0c39e2f0c6
15
2024/16/example.txt
Normal file
15
2024/16/example.txt
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
###############
|
||||||
|
#.......#....E#
|
||||||
|
#.#.###.#.###.#
|
||||||
|
#.....#.#...#.#
|
||||||
|
#.###.#####.#.#
|
||||||
|
#.#.#.......#.#
|
||||||
|
#.#.#####.###.#
|
||||||
|
#...........#.#
|
||||||
|
###.#.#####.#.#
|
||||||
|
#...#.....#.#.#
|
||||||
|
#.#.#.###.#.#.#
|
||||||
|
#.....#...#.#.#
|
||||||
|
#.###.#.#.#.#.#
|
||||||
|
#S..#.....#...#
|
||||||
|
###############
|
17
2024/16/example2.txt
Normal file
17
2024/16/example2.txt
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#################
|
||||||
|
#...#...#...#..E#
|
||||||
|
#.#.#.#.#.#.#.#.#
|
||||||
|
#.#.#.#...#...#.#
|
||||||
|
#.#.#.#.###.#.#.#
|
||||||
|
#...#.#.#.....#.#
|
||||||
|
#.#.#.#.#.#####.#
|
||||||
|
#.#...#.#.#.....#
|
||||||
|
#.#.#####.#.###.#
|
||||||
|
#.#.#.......#...#
|
||||||
|
#.#.###.#####.###
|
||||||
|
#.#.#...#.....#.#
|
||||||
|
#.#.#.#####.###.#
|
||||||
|
#.#.#.........#.#
|
||||||
|
#.#.#.#########.#
|
||||||
|
#S#.............#
|
||||||
|
#################
|
56
2024/16/solution.nix
Normal file
56
2024/16/solution.nix
Normal file
|
@ -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']
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -3,8 +3,8 @@
|
||||||
"aoc-inputs": {
|
"aoc-inputs": {
|
||||||
"flake": false,
|
"flake": false,
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1734266579,
|
"lastModified": 1734334657,
|
||||||
"narHash": "sha256-fhPoaCWITp2KZtdxm+D9e6GiWf+c3/RGJeiq03/ynfY=",
|
"narHash": "sha256-NjsEC/6Mu+i94YPgK4yN0Y5TxhlWwXK5Ev2UzIkfGZo=",
|
||||||
"path": "/tmp/aoc-inputs",
|
"path": "/tmp/aoc-inputs",
|
||||||
"type": "path"
|
"type": "path"
|
||||||
},
|
},
|
||||||
|
|
|
@ -25,16 +25,18 @@
|
||||||
inherit (pkgs) lib;
|
inherit (pkgs) lib;
|
||||||
|
|
||||||
|
|
||||||
in (lib.range 1 15
|
in (lib.range 1 25
|
||||||
|> map (i: let id = lib.fixedWidthNumber 2 i; in {
|
|> map (i: let id = lib.fixedWidthNumber 2 i; in {
|
||||||
name = "day-${id}";
|
name = "day-${id}";
|
||||||
value = let
|
value = let
|
||||||
solution = import ./${id}/solution.nix pkgs;
|
solution = import ./${id}/solution.nix pkgs;
|
||||||
example = (pkgs.lib.readFile ./${id}/example.txt);
|
example = (pkgs.lib.readFile ./${id}/example.txt);
|
||||||
|
example2 = (pkgs.lib.readFile ./${id}/example2.txt);
|
||||||
example3 = (pkgs.lib.readFile ./${id}/example3.txt);
|
example3 = (pkgs.lib.readFile ./${id}/example3.txt);
|
||||||
input = (pkgs.lib.readFile "${aoc-inputs}/${id}");
|
input = (pkgs.lib.readFile "${aoc-inputs}/${id}");
|
||||||
in {
|
in {
|
||||||
example = solution example;
|
example = solution example;
|
||||||
|
example2 = solution example2;
|
||||||
example3 = solution example3;
|
example3 = solution example3;
|
||||||
real = solution input;
|
real = solution input;
|
||||||
test = tix.run [
|
test = tix.run [
|
||||||
|
|
Loading…
Reference in a new issue