From e9f2b699244464a042a5841f20c78229ab6f17c9 Mon Sep 17 00:00:00 2001 From: "RingOfStorms (Joshua Bell)" Date: Mon, 8 Dec 2025 23:57:23 -0600 Subject: [PATCH] day 2 part 2 --- 2025/day_2.nix | 52 ++++++++++++++++++++++++++++++++------------------ 2025/utils.nix | 27 +++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/2025/day_2.nix b/2025/day_2.nix index 8c9a67c..112c263 100644 --- a/2025/day_2.nix +++ b/2025/day_2.nix @@ -8,12 +8,7 @@ with builtins; with import ./utils.nix; let - # reversedChars = - # str: - # let - # len = length str; - # in - # builtins.genList (i: builtins.elemAt str (len - 1 - i)) len; + # part 1 simple repeat middle of word isStrPatternSplit = str: let @@ -25,27 +20,46 @@ let half = strLen / 2; firstHalf = substring 0 half str; secondHalf = substring half strLen str; - # secondReversed = reversedChars secondHalf; in firstHalf == secondHalf else false ); + # part 2 any repeat num + isStrPatternRepeated = + str: + let + strLen = stringLength str; + splits = reverseList (genList (i: i + 1) strLen); + in + any ( + split: + if split < strLen && mod strLen split == 0 then + let + parts = splitStringByLength str split; + in + all (e: e == elemAt parts 0) parts + else + false + ) splits; ranges = map (range: map (v: toIntBase10 v) (split "-" range)) (split "," input); fullRanges = map (itr: map (v: toString v) (rangeInclusive (elemAt itr 0) (elemAt itr 1))) ranges; - invalidIds = flatten ( - map ( - range: - (reduce ( - invalidIds: id: invalidIds ++ (if isStrPatternSplit id then [ id ] else [ ]) - ) [ ] range) - ) fullRanges - ); - invalidIdNums = map (v: toIntBase10 v) invalidIds; - invalidIdSum = sum invalidIdNums; + + getSumOfBadIdsGivenBadIdFunc = + badIdFunc: + let + invalidIds = flatten ( + map ( + range: (reduce (invalidIds: id: invalidIds ++ (if badIdFunc id then [ id ] else [ ])) [ ] range) + ) fullRanges + ); + invalidIdNums = map (v: toIntBase10 v) invalidIds; + invalidIdSum = sum invalidIdNums; + in + invalidIdSum; in { - part1 = invalidIdSum; - part2 = 0; + part1 = getSumOfBadIdsGivenBadIdFunc isStrPatternSplit; + part2 = getSumOfBadIdsGivenBadIdFunc isStrPatternRepeated; } diff --git a/2025/utils.nix b/2025/utils.nix index 406dc82..0fe5ee3 100644 --- a/2025/utils.nix +++ b/2025/utils.nix @@ -4,7 +4,7 @@ rec { 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 != "") (split delim input); + 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 = @@ -17,4 +17,29 @@ rec { 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 [ ]; }