advent_of_code/2025/day_2.nix
RingOfStorms (Joshua Bell) 6a41f0587c day 2 part 1
2025-12-08 22:32:17 -06:00

51 lines
1.3 KiB
Nix

# 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;
}