This commit is contained in:
Oystein Kristoffer Tveit 2022-12-07 11:00:25 +01:00
parent e07d1cefec
commit 80fc798274
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
3 changed files with 1102 additions and 0 deletions

71
day07/default.nix Normal file
View File

@ -0,0 +1,71 @@
{ pkgs, lib }:
with lib;
let
updateState = s@{ path ? [], filetree ? { } }: line: let
splitCommand = splitString " " line;
in if line == "$ cd /" then { path = [ "/" ]; inherit filetree; }
else if line == "$ cd .." then { path = init path; inherit filetree; }
else if elemAt splitCommand 1 == "cd" then { path = path ++ [(elemAt splitCommand 2)]; inherit filetree; }
else if line == "$ ls" then s
else if head splitCommand == "dir" then s
else let
newPath = path ++ [(elemAt splitCommand 1)];
filesize = toInt (elemAt splitCommand 0);
in {
inherit path;
filetree = recursiveUpdate filetree (setAttrByPath newPath filesize);
};
mapAttrsRecursiveToList = f: g: attrs: let
inner = path: attrs': flatten (mapAttrsToList (attrsToListF path) attrs');
attrsToListF = path: n: v: let newPath = path ++ [n];
in if isAttrs v
then g newPath (inner newPath v)
else f newPath v;
in inner [] attrs;
backfillDirSizes = { path, size }: dirs:
if path == []
then dirs
else let
key = concatStringsSep "/" path;
newDirs = recursiveUpdate dirs { ${key} = (dirs.${key} or 0) + size; };
in backfillDirSizes { path = init path; inherit size; } newDirs;
mapFiletreeToDirSizes = attrs: pipe attrs [
(mapAttrsRecursiveToList (path: size: { inherit path size; }) (_: v: v))
(map (x: x // { path = init x.path; }))
(fold backfillDirSizes {})
];
dirSizes = pipe ./input.txt [
fileContents
(splitString "\n")
(foldl updateState { })
(x: x.filetree)
mapFiletreeToDirSizes
];
answer1 = pipe dirSizes [
(filterAttrs (_: v: v <= 100000))
attrValues
(foldr add 0)
toString
];
answer2 = pipe dirSizes [
(filterAttrs (_: v: v >= dirSizes."/" - 40000000))
attrValues
(foldr min (30000000))
toString
];
in pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''

1030
day07/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,7 @@
day04 = pkgs.callPackage ./day04 { }; day04 = pkgs.callPackage ./day04 { };
day05 = pkgs.callPackage ./day05 { }; day05 = pkgs.callPackage ./day05 { };
day06 = pkgs.callPackage ./day06 { }; day06 = pkgs.callPackage ./day06 { };
day07 = pkgs.callPackage ./day07 { };
}; };
}; };
} }