AoC-2022-Nix/util.nix
zuckerberg 030af6ed8f Day 5
2022-12-05 13:07:10 -07:00

39 lines
1.4 KiB
Nix

let
lib = import <nixpkgs/lib>;
utils = with lib; rec {
# Passthrough trace for debugging
pTrace = v: traceSeq v v;
# find the total sum of a int list
sum = foldr (x: y: x + y) 0;
# splits a list of length two into two params then they're passed to a func
splitPair = f: pair: f (head pair) (last pair);
# Finds the max value in a list
maxList = foldr max 0;
# Sorts a int list. Greatest value first
sortList = sort (x: y: x > y);
# Cuts a list in half and returns the two parts in a list
cutInHalf = l: [(take (length l / 2) l) (drop (length l / 2) l)];
# Splits a list into a list of lists with length cnt
chunksOf = cnt: l:
if length l > 0 then
[(take cnt l)] ++ chunksOf cnt (drop cnt l)
else [];
# same as intersectLists but takes an array of lists to intersect instead of just two
intersectManyLists = l:
if length l == 0 then []
else if length l == 1 then head l
else intersectLists (head l) (intersectManyLists (tail l));
# converts a boolean to a int (c style)
boolToInt = b: if b then 1 else 0;
# drops the last element of a list
dropLast = l: take (length l - 1) l;
# transposes a matrix
transpose = ll:
let
outerSize = length ll;
innerSize = length (elemAt ll 0);
in genList (i: genList (j: elemAt (elemAt ll j) i) outerSize) innerSize;
};
in
lib // lib.strings // builtins // utils