diff --git a/2025/day_1.nix b/2025/day_1.nix index 9bf69d5..766eaf0 100644 --- a/2025/day_1.nix +++ b/2025/day_1.nix @@ -9,7 +9,7 @@ with builtins; with import ./utils.nix; let # Split into non-empty lines - lines = builtins.filter (s: builtins.isString s && s != "") (builtins.split "\n" input); + lines = split "\n" input; # Parse a line like "L68" or "R14" parseLine = line: { diff --git a/2025/day_2.nix b/2025/day_2.nix new file mode 100644 index 0000000..8c9a67c --- /dev/null +++ b/2025/day_2.nix @@ -0,0 +1,51 @@ +# nix eval -f ./day_2.nix part1 +# nix eval -f ./day_2.nix part1 --arg input 'builtins.readFile ./puzzle_cache/day_2_1' +# nix eval -f ./day_2.nix part2 +# nix eval -f ./day_2.nix part2 --arg input 'builtins.readFile ./puzzle_cache/day_2_1' +{ + input ? (builtins.readFile ./puzzle_cache/day_2_0), +}: +with builtins; +with import ./utils.nix; +let + # reversedChars = + # str: + # let + # len = length str; + # in + # builtins.genList (i: builtins.elemAt str (len - 1 - i)) len; + isStrPatternSplit = + str: + let + strLen = stringLength str; + in + ( + if mod strLen 2 == 0 then + let + half = strLen / 2; + firstHalf = substring 0 half str; + secondHalf = substring half strLen str; + # secondReversed = reversedChars secondHalf; + in + firstHalf == secondHalf + else + false + ); + + 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; +in +{ + part1 = invalidIdSum; + part2 = 0; +} diff --git a/2025/puzzle_cache/day_2_0 b/2025/puzzle_cache/day_2_0 new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/2025/puzzle_cache/day_2_0 @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 diff --git a/2025/puzzle_cache/day_2_1 b/2025/puzzle_cache/day_2_1 new file mode 100644 index 0000000..f189b93 --- /dev/null +++ b/2025/puzzle_cache/day_2_1 @@ -0,0 +1 @@ +96952600-96977512,6599102-6745632,32748217-32835067,561562-594935,3434310838-3434398545,150-257,864469-909426,677627997-677711085,85-120,2-19,3081-5416,34-77,35837999-36004545,598895-706186,491462157-491543875,5568703-5723454,6262530705-6262670240,8849400-8930122,385535-477512,730193-852501,577-1317,69628781-69809331,2271285646-2271342060,282-487,1716-2824,967913879-967997665,22-33,5722-11418,162057-325173,6666660033-6666677850,67640049-67720478,355185-381658,101543-146174,24562-55394,59942-93946,967864-1031782 diff --git a/2025/utils.nix b/2025/utils.nix index 4921b82..406dc82 100644 --- a/2025/utils.nix +++ b/2025/utils.nix @@ -1,6 +1,20 @@ -{ - toIntBase10 = str: builtins.fromJSON "${str}"; - mod = a: b: a - b * (builtins.floor (a / b)); +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 != "") (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; }