AoC-2022-Nix/util.nix
zuckerberg 13f84a3296 Day 3
2022-12-03 19:46:12 -07:00

32 lines
1.2 KiB
Nix

let
lib = import <nixpkgs/lib>;
utils = rec {
inherit lib;
# Passthrough trace for debugging
pTrace = v: lib.traceSeq v v;
# find the total sum of a int list
sum = l: lib.foldr (x: y: x + y) 0 l;
# splits a list of length two into two params then they're passed to a func
split = pair: f: f (lib.head pair) (lib.last pair);
# Finds the max value in a list
maxList = l: lib.foldr lib.max 0 l;
# Sorts a int list. Greatest value first
sortList = l: lib.sort (x: y: x > y) l;
# Returns the top cnt items of the list
topOfList = l: cnt: lib.take cnt l;
# Cuts a list in half and returns the two parts in a list
cutInHalf = l: [(lib.take (lib.length l / 2) l) (lib.drop (lib.length l / 2) l)];
# Splits a list into a list of lists with length cnt
chunksOf = cnt: l:
if lib.length l >= cnt then
[(lib.take cnt l)] ++ chunksOf cnt (lib.drop cnt l)
else [];
# same as intersectLists but takes an array of lists to intersect instead of just two
intersectManyLists = l:
if lib.length l == 0 then []
else if lib.length l == 1 then lib.head l
else lib.intersectLists (lib.head l) (intersectManyLists (lib.tail l));
};
in
lib // lib.strings // builtins // utils