Advent-of-Nix-2024/utils.nix

19 lines
638 B
Nix
Raw Normal View History

2024-12-01 15:50:11 +01:00
{ lib, ... }:
with lib;
with builtins; rec {
2024-12-02 10:00:36 +01:00
readLines = f: init (filter isString (split "\n" (readFile f)));
2024-12-03 10:51:06 +01:00
reSplit = re: s: filter isString (split re s);
matchAll = re: s: filter isList (split re s);
splitWhitespace = reSplit "[[:space:]]+";
2024-12-01 15:50:11 +01:00
abs = i: if i >= 0 then i else i * -1;
delta = x: y: abs (x - y);
listSum = foldl' add 0;
2024-12-04 13:12:45 +01:00
listSumWith = f: l: foldl' add 0 (map f l);
2024-12-02 10:05:27 +01:00
deleteAt = l: i: (take i l) ++ (drop (i + 1) l);
2024-12-01 15:59:47 +01:00
eq = x: y: x == y;
2024-12-02 10:00:36 +01:00
greaterThan = x: y: x > y;
2024-12-04 13:12:45 +01:00
listRange = l: range 0 ((length l) - 1);
transpose = l: map (i: map ((flip elemAt) i) l) (listRange (head l));
invert = f: x: !(f x);
2024-12-01 15:50:11 +01:00
}