Last active
December 2, 2020 18:14
-
-
Save ncfavier/3f84c5e0ecb05495aa2832fb4ade67bf to your computer and use it in GitHub Desktop.
Advent of Code 2020 in Nix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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