Impl d1p2 in nix
This commit is contained in:
parent
3400e9db04
commit
3ad0212da4
2 changed files with 48 additions and 0 deletions
46
day1/part2.nix
Normal file
46
day1/part2.nix
Normal file
|
@ -0,0 +1,46 @@
|
|||
let
|
||||
# Import nixpkgs lib and input file
|
||||
inherit ((builtins.getFlake (builtins.toString ./..)).inputs.nixpkgs) lib;
|
||||
input = builtins.readFile (builtins.toString ./input.txt);
|
||||
|
||||
# Do parsing
|
||||
parsed = ( # Define function to sort lines into two lists
|
||||
lib.lists.foldr
|
||||
(cur: acc: {
|
||||
first = acc.first ++ [(lib.strings.toIntBase10 (builtins.elemAt cur 0))];
|
||||
second = acc.second ++ [(lib.strings.toIntBase10 (builtins.elemAt cur 1))];
|
||||
})
|
||||
{ first = []; second = []; }
|
||||
) ( # Call with input split on newlines and 3 spaces; formatted like [ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]
|
||||
lib.lists.forEach
|
||||
(lib.strings.splitString "\n" input)
|
||||
(e: lib.strings.splitString " " e)
|
||||
);
|
||||
collapsed = (
|
||||
lib.lists.foldr
|
||||
(cur: acc:
|
||||
acc // (
|
||||
if (builtins.hasAttr "${builtins.toString cur}" acc)
|
||||
then {
|
||||
"${builtins.toString cur}" = acc."${builtins.toString cur}" + 1;
|
||||
}
|
||||
else {
|
||||
"${builtins.toString cur}" = 1;
|
||||
}
|
||||
)
|
||||
)
|
||||
{}
|
||||
) parsed.second;
|
||||
|
||||
output = (
|
||||
lib.lists.foldr
|
||||
(cur: acc:
|
||||
acc + (
|
||||
if (builtins.hasAttr "${builtins.toString cur}" collapsed)
|
||||
then (cur * collapsed."${builtins.toString cur}")
|
||||
else 0
|
||||
)
|
||||
)
|
||||
0
|
||||
) parsed.first;
|
||||
in output
|
|
@ -196,6 +196,8 @@
|
|||
}
|
||||
},
|
||||
"remote": {
|
||||
"https://deno.land/std@0.119.0/encoding/hex.ts": "5bc7df19af498c315cdaba69e2fce1b2aef5fc57344e8c21c08991aa8505a260",
|
||||
"https://deno.land/std@0.119.0/hash/md5.ts": "957791b9052b40cfa211eeb755a76ad6ad12177387ba84b47c4bc8be4edd60c8",
|
||||
"https://deno.land/x/sqlite@v3.9.1/build/sqlite.js": "2afc7875c7b9c85d89730c4a311ab3a304e5d1bf761fbadd8c07bbdf130f5f9b",
|
||||
"https://deno.land/x/sqlite@v3.9.1/build/vfs.js": "7f7778a9fe499cd10738d6e43867340b50b67d3e39142b0065acd51a84cd2e03",
|
||||
"https://deno.land/x/sqlite@v3.9.1/mod.ts": "e09fc79d8065fe222578114b109b1fd60077bff1bb75448532077f784f4d6a83",
|
||||
|
|
Loading…
Reference in a new issue