2024 day 18 part 1

This commit is contained in:
Tristan 2024-12-18 19:38:22 +00:00
parent dbe990e782
commit 5467ea9393
6 changed files with 299 additions and 3 deletions

25
2024/18/example.txt Normal file
View file

@ -0,0 +1,25 @@
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0

52
2024/18/solution.nix Normal file
View file

@ -0,0 +1,52 @@
{lib, input ? "", my-lib, ...}@pkgs: rec {
inherit pkgs;
inherit lib;
inherit (builtins) elemAt genList elem concatStringsSep;
inherit (lib) splitString;
inherit (lib.strings) toIntBase10;
init = input: input |> lib.trim
|> splitString "\n"
|> map (line: let c = splitString "," line; in {
x = elemAt c 0 |> toIntBase10;
y = elemAt c 1 |> toIntBase10;
})
;
genChart = size: coords: genList (y: genList (x:
if x == 0 && y == 0 then "S" else
if x == size - 1 && y == size - 1 then "E" else
if elem {inherit x y;} coords then "#" else "."
) size) size;
chartToStr = chart: chart |> (map (concatStringsSep "")) |> concatStringsSep "\n";
mkSolution = {size, bytes, input}: rec {
inherit size bytes input;
walls = lib.sublist 0 bytes (init input);
chart = genChart size walls;
part1result = dijkstra.toGoal.steps;
dijkstra = my-lib.dijkstra {
pos = {x = 0; y = 0;};
goal = {x = size - 1; y = size - 1;};
chart = chart;
width = size;
height = size;
};
};
example = mkSolution {
size = 7;
bytes = 12;
input = lib.readFile ./example.txt;
};
real = mkSolution {
size = 71;
bytes = 1024;
input = input;
};
# not 272, too high
}