day 2 part 1

This commit is contained in:
RingOfStorms (Joshua Bell) 2025-12-08 22:32:17 -06:00
parent 1f92f032ca
commit 6a41f0587c
5 changed files with 71 additions and 4 deletions

View file

@ -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: {

51
2025/day_2.nix Normal file
View file

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

View file

@ -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

View file

@ -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

View file

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