From 3ad0212da4aa686e31f123d2205bc623762d7a58 Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Wed, 11 Dec 2024 19:02:45 -0700 Subject: [PATCH] Impl d1p2 in nix --- day1/part2.nix | 46 ++++++++++++++++++++++++++++++++++++++++++++++ deno.lock | 2 ++ 2 files changed, 48 insertions(+) create mode 100644 day1/part2.nix diff --git a/day1/part2.nix b/day1/part2.nix new file mode 100644 index 0000000..8a8a9ae --- /dev/null +++ b/day1/part2.nix @@ -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 \ No newline at end of file diff --git a/deno.lock b/deno.lock index aa26051..a827cc1 100644 --- a/deno.lock +++ b/deno.lock @@ -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",