diff --git a/2025/day_1.nix b/2025/day_1.nix index e9c48d2..9bf69d5 100644 --- a/2025/day_1.nix +++ b/2025/day_1.nix @@ -1,12 +1,13 @@ -# nix eval -f ./day_1.nix countZero -# nix eval -f ./day_1.nix countZero --arg input 'builtins.readFile ./puzzle_cache/day_1_1' +# nix eval -f ./day_1.nix part1 +# nix eval -f ./day_1.nix part1 --arg input 'builtins.readFile ./puzzle_cache/day_1_1' +# nix eval -f ./day_1.nix part2 +# nix eval -f ./day_1.nix part2 --arg input 'builtins.readFile ./puzzle_cache/day_1_1' { input ? (builtins.readFile ./puzzle_cache/day_1_0), }: +with builtins; +with import ./utils.nix; let - utils = import ./utils.nix; - inherit (utils) toIntBase10 mod; - # Split into non-empty lines lines = builtins.filter (s: builtins.isString s && s != "") (builtins.split "\n" input); @@ -18,6 +19,15 @@ let rotations = map parseLine lines; + countZerosDuring = + position: dir: dist: + if dir == "R" then + floor ((position + dist) / 100.0) - floor (position / 100.0) + else if dir == "L" then + floor ((position -1) / 100.0) - floor ((position - dist - 1) / 100.0) + else + builtins.throw "Invalid direction: ${dir}"; + # Fold over the list to simulate the dial step = state: rot: @@ -29,19 +39,23 @@ let mod (state.position + rot.dist) 100 else builtins.trace "Invalid direction: ${rot.dir}" state.position; + countZeroPassed = state.countZeroPassed + (countZerosDuring state.position rot.dir rot.dist); countZero = state.countZero + (if position == 0 then 1 else 0); in { - inherit position countZero; + inherit position countZero countZeroPassed; }; initialState = { position = 50; countZero = 0; + countZeroPassed = 0; }; finalState = builtins.foldl' step initialState rotations; in { - inherit (finalState) position countZero; + part1 = finalState.countZero; + part2 = finalState.countZeroPassed; + # 6689 should be part 2 } diff --git a/2025/utils.nix b/2025/utils.nix index a0f3bca..4921b82 100644 --- a/2025/utils.nix +++ b/2025/utils.nix @@ -1,4 +1,6 @@ { toIntBase10 = str: builtins.fromJSON "${str}"; mod = a: b: a - b * (builtins.floor (a / b)); + min = a: b: if a < b then a else b; + max = a: b: if a > b then a else b; }