diff --git a/day05/default.nix b/day05/default.nix new file mode 100644 index 0000000..1b4fb47 --- /dev/null +++ b/day05/default.nix @@ -0,0 +1,76 @@ +{ pkgs, lib }: + +with lib; + +let + input = splitString "\n\n" (fileContents ./input.txt); + + getCharOffset = index: 1 + 4 * (index - 1); + + getLineAttrs = line: pipe (range 1 9) [ + (map (n: nameValuePair (toString n) (getCharOffset n))) + listToAttrs + (mapAttrs (_: elemAt (stringToCharacters line))) + (filterAttrs (_: char: char != " ")) + ]; + + stack = pipe input [ + (flip elemAt 0) + (splitString "\n") + + # remove the line with stack indices + reverseList + (drop 1) + reverseList + + (map getLineAttrs) + zipAttrs + ]; + + moves = pipe input [ + (flip elemAt 1) + (splitString "\n") + (map (s: + let words = splitString " " s; + in { + amount = elemAt words 1; + from = elemAt words 3; + to = elemAt words 5; + } + )) + (map (mapAttrs (_: toInt))) + ]; + + executeStackMove = move: stack: + stack // { + ${toString move.from} = drop move.amount stack.${toString move.from}; + ${toString move.to} = (reverseList (take move.amount stack.${toString move.from})) ++ stack.${toString move.to}; + }; + + answer1 = pipe stack + ((map executeStackMove moves) + ++ [ + (mapAttrsToList (_: head)) + concatStrings + ]); + + executeStackMove2 = move: stack: + stack // { + ${toString move.from} = drop move.amount stack.${toString move.from}; + ${toString move.to} = (take move.amount stack.${toString move.from}) ++ stack.${toString move.to}; + }; + + answer2 = pipe stack + ((map executeStackMove2 moves) + ++ [ + (mapAttrsToList (_: head)) + concatStrings + ]); + +in pkgs.writeText "answers" '' + Task1: + ${answer1} + + Task2: + ${answer2} +'' diff --git a/day05/input.txt b/day05/input.txt new file mode 100644 index 0000000..5c2c7e9 --- /dev/null +++ b/day05/input.txt @@ -0,0 +1,512 @@ + [M] [W] [M] + [L] [Q] [S] [C] [R] + [Q] [F] [F] [T] [N] [S] + [N] [V] [V] [H] [L] [J] [D] + [D] [D] [W] [P] [G] [R] [D] [F] +[T] [T] [M] [G] [G] [Q] [N] [W] [L] +[Z] [H] [F] [J] [D] [Z] [S] [H] [Q] +[B] [V] [B] [T] [W] [V] [Z] [Z] [M] + 1 2 3 4 5 6 7 8 9 + +move 1 from 7 to 4 +move 1 from 6 to 2 +move 5 from 9 to 4 +move 2 from 2 to 8 +move 2 from 2 to 6 +move 3 from 3 to 7 +move 3 from 7 to 1 +move 1 from 9 to 4 +move 4 from 7 to 3 +move 5 from 1 to 8 +move 1 from 1 to 2 +move 1 from 4 to 9 +move 4 from 5 to 6 +move 1 from 5 to 8 +move 3 from 2 to 4 +move 3 from 6 to 4 +move 3 from 5 to 9 +move 1 from 9 to 7 +move 1 from 9 to 8 +move 2 from 7 to 9 +move 1 from 7 to 9 +move 1 from 7 to 8 +move 2 from 9 to 8 +move 13 from 4 to 2 +move 2 from 6 to 1 +move 3 from 3 to 2 +move 9 from 2 to 7 +move 1 from 9 to 7 +move 5 from 7 to 8 +move 9 from 8 to 4 +move 2 from 7 to 1 +move 1 from 1 to 7 +move 2 from 3 to 2 +move 14 from 4 to 5 +move 3 from 8 to 4 +move 13 from 8 to 3 +move 2 from 7 to 1 +move 6 from 6 to 5 +move 1 from 6 to 9 +move 3 from 2 to 8 +move 1 from 7 to 8 +move 5 from 2 to 8 +move 4 from 4 to 8 +move 1 from 9 to 8 +move 1 from 7 to 1 +move 1 from 1 to 2 +move 7 from 3 to 2 +move 4 from 3 to 2 +move 2 from 5 to 3 +move 2 from 1 to 5 +move 5 from 8 to 7 +move 6 from 8 to 3 +move 11 from 5 to 8 +move 8 from 8 to 9 +move 5 from 7 to 8 +move 3 from 1 to 8 +move 5 from 3 to 8 +move 11 from 2 to 9 +move 1 from 3 to 5 +move 1 from 2 to 1 +move 1 from 2 to 7 +move 6 from 5 to 7 +move 19 from 9 to 7 +move 3 from 5 to 3 +move 1 from 5 to 4 +move 1 from 1 to 4 +move 1 from 9 to 8 +move 25 from 7 to 9 +move 2 from 4 to 1 +move 2 from 1 to 4 +move 2 from 4 to 7 +move 2 from 7 to 9 +move 5 from 3 to 1 +move 1 from 7 to 1 +move 9 from 9 to 5 +move 3 from 5 to 6 +move 9 from 9 to 1 +move 7 from 1 to 3 +move 6 from 8 to 9 +move 1 from 5 to 2 +move 10 from 9 to 2 +move 1 from 2 to 7 +move 5 from 9 to 8 +move 1 from 5 to 8 +move 5 from 3 to 8 +move 1 from 9 to 4 +move 4 from 3 to 6 +move 4 from 6 to 3 +move 3 from 1 to 4 +move 3 from 2 to 4 +move 3 from 5 to 8 +move 3 from 4 to 9 +move 1 from 7 to 3 +move 2 from 9 to 8 +move 4 from 2 to 5 +move 1 from 3 to 4 +move 1 from 9 to 3 +move 5 from 5 to 6 +move 7 from 8 to 5 +move 3 from 1 to 7 +move 6 from 5 to 8 +move 5 from 4 to 5 +move 3 from 3 to 2 +move 1 from 1 to 4 +move 19 from 8 to 1 +move 3 from 7 to 3 +move 4 from 2 to 9 +move 1 from 2 to 6 +move 7 from 6 to 4 +move 1 from 6 to 2 +move 2 from 1 to 3 +move 5 from 4 to 1 +move 1 from 6 to 2 +move 3 from 3 to 6 +move 12 from 1 to 2 +move 2 from 8 to 1 +move 14 from 2 to 4 +move 7 from 1 to 5 +move 10 from 4 to 6 +move 3 from 6 to 4 +move 1 from 8 to 4 +move 4 from 3 to 5 +move 1 from 2 to 3 +move 2 from 1 to 4 +move 17 from 5 to 3 +move 7 from 4 to 1 +move 1 from 9 to 4 +move 4 from 6 to 3 +move 5 from 4 to 8 +move 12 from 3 to 1 +move 6 from 3 to 5 +move 17 from 1 to 5 +move 2 from 1 to 9 +move 3 from 1 to 4 +move 7 from 8 to 2 +move 4 from 3 to 7 +move 1 from 1 to 8 +move 17 from 5 to 2 +move 11 from 2 to 8 +move 11 from 8 to 4 +move 11 from 2 to 4 +move 4 from 6 to 1 +move 4 from 1 to 3 +move 2 from 6 to 9 +move 3 from 7 to 8 +move 3 from 5 to 3 +move 23 from 4 to 3 +move 4 from 4 to 8 +move 1 from 7 to 4 +move 2 from 2 to 3 +move 6 from 3 to 2 +move 16 from 3 to 9 +move 2 from 5 to 8 +move 1 from 4 to 5 +move 2 from 5 to 9 +move 1 from 2 to 3 +move 1 from 3 to 8 +move 9 from 9 to 1 +move 6 from 3 to 8 +move 3 from 3 to 1 +move 18 from 8 to 9 +move 1 from 3 to 5 +move 5 from 1 to 4 +move 5 from 1 to 8 +move 3 from 4 to 1 +move 1 from 5 to 2 +move 2 from 4 to 8 +move 1 from 1 to 2 +move 5 from 2 to 7 +move 2 from 8 to 1 +move 2 from 2 to 6 +move 3 from 1 to 6 +move 3 from 9 to 6 +move 31 from 9 to 7 +move 26 from 7 to 8 +move 3 from 1 to 6 +move 22 from 8 to 4 +move 2 from 4 to 5 +move 4 from 6 to 5 +move 11 from 4 to 3 +move 9 from 4 to 6 +move 2 from 5 to 9 +move 4 from 7 to 1 +move 2 from 6 to 1 +move 1 from 5 to 3 +move 6 from 8 to 6 +move 8 from 6 to 2 +move 1 from 1 to 6 +move 3 from 1 to 3 +move 1 from 5 to 3 +move 1 from 5 to 9 +move 5 from 7 to 2 +move 2 from 9 to 6 +move 4 from 8 to 6 +move 1 from 7 to 2 +move 1 from 5 to 4 +move 12 from 3 to 4 +move 3 from 3 to 1 +move 3 from 6 to 8 +move 1 from 9 to 3 +move 6 from 2 to 6 +move 2 from 3 to 2 +move 10 from 2 to 7 +move 2 from 1 to 9 +move 2 from 1 to 6 +move 1 from 1 to 4 +move 9 from 7 to 9 +move 3 from 8 to 7 +move 7 from 4 to 8 +move 2 from 7 to 4 +move 4 from 8 to 1 +move 5 from 8 to 2 +move 3 from 1 to 3 +move 1 from 8 to 7 +move 3 from 3 to 7 +move 4 from 2 to 6 +move 8 from 4 to 2 +move 5 from 2 to 5 +move 11 from 9 to 7 +move 2 from 5 to 7 +move 16 from 7 to 8 +move 5 from 8 to 7 +move 1 from 4 to 3 +move 3 from 5 to 1 +move 11 from 6 to 5 +move 7 from 5 to 4 +move 5 from 7 to 4 +move 1 from 3 to 7 +move 2 from 5 to 4 +move 10 from 4 to 8 +move 14 from 6 to 3 +move 1 from 5 to 9 +move 1 from 6 to 5 +move 2 from 2 to 9 +move 2 from 1 to 3 +move 2 from 5 to 3 +move 2 from 7 to 6 +move 2 from 1 to 4 +move 1 from 2 to 3 +move 19 from 3 to 6 +move 3 from 9 to 2 +move 4 from 2 to 6 +move 6 from 6 to 7 +move 13 from 6 to 2 +move 14 from 8 to 1 +move 6 from 4 to 3 +move 5 from 7 to 8 +move 3 from 6 to 3 +move 2 from 8 to 2 +move 2 from 6 to 8 +move 4 from 1 to 8 +move 13 from 8 to 4 +move 10 from 4 to 7 +move 1 from 4 to 5 +move 1 from 5 to 1 +move 3 from 6 to 5 +move 3 from 8 to 9 +move 9 from 3 to 1 +move 3 from 5 to 8 +move 3 from 9 to 6 +move 3 from 8 to 7 +move 1 from 6 to 9 +move 1 from 9 to 4 +move 9 from 2 to 8 +move 2 from 2 to 6 +move 14 from 7 to 1 +move 31 from 1 to 5 +move 3 from 1 to 7 +move 4 from 6 to 8 +move 24 from 5 to 3 +move 2 from 8 to 1 +move 1 from 8 to 5 +move 2 from 1 to 7 +move 3 from 7 to 6 +move 6 from 8 to 6 +move 2 from 4 to 2 +move 1 from 4 to 3 +move 2 from 2 to 7 +move 6 from 6 to 7 +move 4 from 8 to 6 +move 7 from 6 to 2 +move 12 from 7 to 5 +move 4 from 2 to 8 +move 1 from 2 to 4 +move 1 from 4 to 6 +move 1 from 6 to 7 +move 1 from 7 to 3 +move 3 from 3 to 8 +move 17 from 3 to 5 +move 4 from 3 to 6 +move 35 from 5 to 3 +move 2 from 2 to 6 +move 1 from 5 to 9 +move 9 from 3 to 7 +move 6 from 8 to 1 +move 4 from 2 to 6 +move 4 from 6 to 9 +move 20 from 3 to 9 +move 22 from 9 to 7 +move 1 from 8 to 6 +move 29 from 7 to 5 +move 4 from 6 to 8 +move 6 from 1 to 8 +move 2 from 7 to 3 +move 1 from 6 to 5 +move 2 from 3 to 9 +move 1 from 9 to 3 +move 4 from 5 to 6 +move 18 from 5 to 1 +move 7 from 3 to 1 +move 1 from 3 to 6 +move 3 from 5 to 1 +move 1 from 3 to 9 +move 4 from 5 to 2 +move 10 from 8 to 7 +move 2 from 9 to 3 +move 1 from 3 to 5 +move 21 from 1 to 9 +move 1 from 3 to 2 +move 1 from 2 to 9 +move 15 from 9 to 3 +move 4 from 7 to 1 +move 2 from 6 to 1 +move 7 from 9 to 1 +move 1 from 5 to 4 +move 1 from 4 to 6 +move 6 from 3 to 9 +move 3 from 6 to 5 +move 19 from 1 to 6 +move 8 from 3 to 6 +move 1 from 3 to 7 +move 20 from 6 to 7 +move 1 from 2 to 6 +move 6 from 9 to 8 +move 2 from 9 to 4 +move 1 from 1 to 3 +move 1 from 2 to 9 +move 3 from 5 to 6 +move 2 from 7 to 3 +move 2 from 9 to 7 +move 1 from 4 to 8 +move 4 from 8 to 9 +move 4 from 7 to 1 +move 2 from 1 to 7 +move 1 from 3 to 2 +move 2 from 8 to 9 +move 6 from 6 to 2 +move 1 from 8 to 1 +move 1 from 5 to 7 +move 4 from 2 to 9 +move 1 from 3 to 5 +move 5 from 6 to 1 +move 1 from 4 to 2 +move 1 from 9 to 6 +move 1 from 9 to 6 +move 4 from 6 to 3 +move 7 from 9 to 7 +move 8 from 7 to 2 +move 1 from 5 to 8 +move 5 from 2 to 9 +move 3 from 2 to 5 +move 6 from 1 to 3 +move 17 from 7 to 6 +move 1 from 8 to 2 +move 2 from 1 to 7 +move 5 from 9 to 4 +move 4 from 3 to 8 +move 3 from 4 to 1 +move 1 from 9 to 2 +move 4 from 2 to 1 +move 1 from 8 to 4 +move 1 from 5 to 9 +move 1 from 2 to 3 +move 3 from 3 to 2 +move 10 from 7 to 6 +move 3 from 4 to 1 +move 5 from 3 to 2 +move 4 from 1 to 5 +move 3 from 8 to 6 +move 12 from 6 to 4 +move 1 from 9 to 3 +move 1 from 5 to 2 +move 3 from 1 to 6 +move 12 from 6 to 8 +move 3 from 1 to 5 +move 2 from 4 to 3 +move 5 from 8 to 7 +move 7 from 5 to 3 +move 3 from 7 to 9 +move 1 from 5 to 8 +move 5 from 3 to 7 +move 10 from 6 to 5 +move 2 from 7 to 5 +move 8 from 2 to 9 +move 5 from 3 to 9 +move 9 from 5 to 1 +move 5 from 7 to 4 +move 15 from 9 to 5 +move 1 from 2 to 5 +move 1 from 8 to 5 +move 6 from 4 to 1 +move 2 from 2 to 9 +move 18 from 5 to 8 +move 18 from 8 to 3 +move 16 from 3 to 4 +move 3 from 5 to 8 +move 1 from 9 to 2 +move 3 from 1 to 7 +move 3 from 8 to 2 +move 3 from 7 to 9 +move 2 from 3 to 4 +move 3 from 9 to 8 +move 11 from 1 to 6 +move 2 from 9 to 4 +move 3 from 6 to 9 +move 8 from 6 to 4 +move 26 from 4 to 7 +move 1 from 2 to 5 +move 1 from 5 to 2 +move 3 from 9 to 3 +move 21 from 7 to 6 +move 4 from 2 to 4 +move 1 from 2 to 3 +move 5 from 7 to 6 +move 8 from 8 to 1 +move 1 from 3 to 7 +move 9 from 1 to 4 +move 1 from 7 to 4 +move 20 from 4 to 7 +move 1 from 8 to 5 +move 2 from 4 to 8 +move 1 from 4 to 9 +move 3 from 8 to 9 +move 1 from 5 to 8 +move 2 from 3 to 1 +move 4 from 7 to 8 +move 3 from 7 to 5 +move 1 from 1 to 7 +move 4 from 8 to 3 +move 3 from 5 to 6 +move 1 from 8 to 4 +move 1 from 1 to 8 +move 28 from 6 to 4 +move 1 from 6 to 1 +move 2 from 7 to 8 +move 1 from 8 to 7 +move 1 from 8 to 1 +move 2 from 1 to 9 +move 3 from 9 to 3 +move 12 from 7 to 5 +move 7 from 3 to 1 +move 1 from 3 to 6 +move 26 from 4 to 9 +move 1 from 6 to 3 +move 1 from 3 to 6 +move 1 from 8 to 6 +move 1 from 7 to 8 +move 1 from 6 to 3 +move 3 from 1 to 3 +move 5 from 4 to 3 +move 28 from 9 to 4 +move 2 from 1 to 5 +move 22 from 4 to 1 +move 3 from 5 to 3 +move 5 from 5 to 7 +move 10 from 1 to 6 +move 1 from 5 to 2 +move 3 from 5 to 3 +move 2 from 5 to 9 +move 3 from 9 to 7 +move 2 from 4 to 5 +move 1 from 5 to 4 +move 4 from 3 to 8 +move 1 from 5 to 7 +move 9 from 6 to 5 +move 1 from 7 to 6 +move 1 from 6 to 5 +move 2 from 6 to 9 +move 3 from 5 to 1 +move 13 from 1 to 3 +move 7 from 7 to 5 +move 1 from 2 to 9 +move 3 from 8 to 2 +move 1 from 7 to 2 +move 3 from 4 to 3 +move 19 from 3 to 8 +move 5 from 3 to 7 +move 1 from 7 to 1 +move 19 from 8 to 6 +move 5 from 1 to 4 +move 5 from 5 to 2 +move 2 from 2 to 7 +move 3 from 4 to 1 +move 6 from 5 to 7 +move 2 from 8 to 7 +move 2 from 2 to 7 +move 3 from 3 to 5 +move 5 from 7 to 6 +move 6 from 6 to 1 +move 2 from 5 to 1 +move 2 from 4 to 3 +move 1 from 5 to 8 \ No newline at end of file diff --git a/flake.nix b/flake.nix index dd6e333..c07deb8 100644 --- a/flake.nix +++ b/flake.nix @@ -10,6 +10,7 @@ day02 = pkgs.callPackage ./day02 { }; day03 = pkgs.callPackage ./day03 { }; day04 = pkgs.callPackage ./day04 { }; + day05 = pkgs.callPackage ./day05 { }; }; }; }