2024 day 15 part 1 in nix

This commit is contained in:
tristan 2024-12-15 15:11:41 +00:00
parent ef621d0fdb
commit c5c6d4becc
5 changed files with 133 additions and 3 deletions

21
2024/15/example.txt Normal file
View file

@ -0,0 +1,21 @@
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#..O@..O.#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^

10
2024/15/example2.txt Normal file
View file

@ -0,0 +1,10 @@
########
#..O.O.#
##@.O..#
#...O..#
#.#.O..#
#...O..#
#......#
########
<^^>>>vv<v>>v<<

97
2024/15/solution.nix Normal file
View file

@ -0,0 +1,97 @@
{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;
index = i: list: elemAt list i;
index2d = {x, y}: m: m |> index y |> index x;
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 "^><v"));
ins = elemAt parts 1 |> splitString "\n" |> map stringToCharacters;
width = (elemAt chart 0 |> length) + 1;
startIndex = elemAt parts 0 |> stringToCharacters |> findFirstIndex (char: char == "@") null;
in {
inherit chart ins;
pos = {
x = mod startIndex width;
y = startIndex / width;
};
}
;
addVec = a: b: {x = a.x + b.x; y = a.y + b.y;};
objects = {
wall = "#";
moveable = "O";
empty = ".";
};
replace2d = {x, y}: updated: chart: let
newRow = chart |> index y |> replace x updated;
in
replace y newRow chart;
replace = i: updated: list:
let before = lib.sublist 0 i list;
after = lib.sublist (i + 1) (builtins.length list - i) list;
in
if i < 0 || i > builtins.length list then list else
before ++ [updated] ++ after;
strToDir = strDir: {
x = if strDir == "<" then -1 else if strDir == ">" then 1 else 0;
y = if strDir == "^" then -1 else if strDir == "v" then 1 else 0;
};
move = {chart, pos, ...}: dir: let
nextPos = addVec pos dir;
atPos = chart |> index2d pos;
moved = move {inherit chart; pos = nextPos;} dir;
in if atPos == objects.wall
then {
inherit chart pos;
moved = false;
} else if atPos == objects.empty
then {
inherit chart pos;
moved = true;
} else if !moved.moved
then {inherit chart pos; moved = false;}
else {
moved = true;
pos = nextPos;
chart = moved.chart |> replace2d pos objects.empty |> replace2d nextPos atPos;
};
chartToStr = chart: chart |> (map (concatStringsSep "")) |> concatStringsSep "\n";
applyIns = foldl' (c: ins: let
dir = strToDir ins;
in move c dir);
applyInsList = foldl' (chart: ins:
# evaluate each line of instructions one at a time, to avoid stack overflow.
let res = applyIns chart ins; in builtins.deepSeq res
(res)
);
result = applyInsList init init.ins;
getScore = chart: chart
|> imap0 (y: row: row
|> imap0 (x: obj:
if obj == objects.moveable then x + y * 100 else 0
)
)
|> concatLists
|> foldl' builtins.add 0
;
part1result = getScore result.chart;
}

View file

@ -3,8 +3,8 @@
"aoc-inputs": { "aoc-inputs": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1734173652, "lastModified": 1734266579,
"narHash": "sha256-jXnmQLoXNcaqYY/uupC8f8JQ27zdwSVBmxAnIxlennk=", "narHash": "sha256-fhPoaCWITp2KZtdxm+D9e6GiWf+c3/RGJeiq03/ynfY=",
"path": "/tmp/aoc-inputs", "path": "/tmp/aoc-inputs",
"type": "path" "type": "path"
}, },

View file

@ -25,15 +25,17 @@
inherit (pkgs) lib; inherit (pkgs) lib;
in (lib.range 1 14 in (lib.range 1 15
|> 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);
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;
real = solution input; real = solution input;
test = tix.run [ test = tix.run [
./${id}/solution.test.nix ./${id}/solution.test.nix