advent_of_code/2025/utils.nix
RingOfStorms (Joshua Bell) e9f2b69924 day 2 part 2
2025-12-08 23:57:23 -06:00

45 lines
1.3 KiB
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);
reduce = foldl'; # foldl' (acc: elem: acc + elem) 0 [1 2 3]
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: reduce (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 =
# @param s: The input string.
# @param len: The desired length of each part.
s: len:
let
# Recursive helper function
splitRec =
currentString: acc:
if stringLength currentString <= 0 then
reverseList acc
else
let
chunk = substring 0 len currentString;
remaining = substring len (stringLength currentString - len) currentString;
in
splitRec remaining ([ chunk ] ++ acc);
in
splitRec s [ ];
}