advent_of_code/2025/utils.nix

28 lines
881 B
Nix

with builtins;
rec {
toIntBase10 = str: fromJSON "${str}";
mod = a: b: a - b * (floor (a / b));
min = a: b: if a < b then a else b;
max = a: b: if a > b then a else b;
split = delim: input: filter (s: isString s && s != "") (builtins.split delim input);
flatten = input: concatMap (x: if isList x then x else [ x ]) input;
rangeInclusive =
min: max:
if min > max then
[ ]
else
let
len = max - min + 1;
in
genList (i: min + i) len;
sum = input: foldl' (sum: v: sum + v) 0 input;
reverseList =
list:
if builtins.length list == 0 then
[ ]
else
# Recursively reverse the tail and append the head to the end
(reverseList (builtins.tail list)) ++ [ (builtins.head list) ];
splitStringByLength =
str: len: map (i: substring (i * len) (len) str) (genList (i: i) (stringLength str / len));
}