Skip to content

Instantly share code, notes, and snippets.

@ncfavier
Last active December 2, 2020 18:14
Show Gist options
  • Save ncfavier/3f84c5e0ecb05495aa2832fb4ade67bf to your computer and use it in GitHub Desktop.
Save ncfavier/3f84c5e0ecb05495aa2832fb4ade67bf to your computer and use it in GitHub Desktop.
Advent of Code 2020 in Nix
#!/usr/bin/env -S nix-instantiate --eval --strict
# real 0m4.143s
with (builtins.getFlake "nixos").lib;
let
tails = l: if l == [] then [] else [ l ] ++ tails (tail l);
sum = foldl' (x: y: x + y) 0;
product = foldl' (x: y: x * y) 1;
lines = s: splitString "\n" (removeSuffix "\n" s);
nums = map toInt (lines (builtins.readFile ./input1));
solve = nums: n: let
go = nums: n: xs: if n == 0 then
optional (sum xs == 2020) xs
else
concatMap (ys:
go (tail ys) (n - 1) ([ (head ys) ] ++ xs)
) (tails nums);
in product (head (go nums n []));
in
map (solve nums) [ 2 3 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment