2025 day 2

This commit is contained in:
RingOfStorms (Joshua Bell) 2025-12-08 16:40:21 -06:00
parent 5ef662fdcc
commit 1f92f032ca
2 changed files with 23 additions and 7 deletions

View file

@ -1,12 +1,13 @@
# nix eval -f ./day_1.nix countZero # nix eval -f ./day_1.nix part1
# nix eval -f ./day_1.nix countZero --arg input 'builtins.readFile ./puzzle_cache/day_1_1' # 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), input ? (builtins.readFile ./puzzle_cache/day_1_0),
}: }:
with builtins;
with import ./utils.nix;
let let
utils = import ./utils.nix;
inherit (utils) toIntBase10 mod;
# Split into non-empty lines # Split into non-empty lines
lines = builtins.filter (s: builtins.isString s && s != "") (builtins.split "\n" input); lines = builtins.filter (s: builtins.isString s && s != "") (builtins.split "\n" input);
@ -18,6 +19,15 @@ let
rotations = map parseLine lines; 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 # Fold over the list to simulate the dial
step = step =
state: rot: state: rot:
@ -29,19 +39,23 @@ let
mod (state.position + rot.dist) 100 mod (state.position + rot.dist) 100
else else
builtins.trace "Invalid direction: ${rot.dir}" state.position; 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); countZero = state.countZero + (if position == 0 then 1 else 0);
in in
{ {
inherit position countZero; inherit position countZero countZeroPassed;
}; };
initialState = { initialState = {
position = 50; position = 50;
countZero = 0; countZero = 0;
countZeroPassed = 0;
}; };
finalState = builtins.foldl' step initialState rotations; finalState = builtins.foldl' step initialState rotations;
in in
{ {
inherit (finalState) position countZero; part1 = finalState.countZero;
part2 = finalState.countZeroPassed;
# 6689 should be part 2
} }

View file

@ -1,4 +1,6 @@
{ {
toIntBase10 = str: builtins.fromJSON "${str}"; toIntBase10 = str: builtins.fromJSON "${str}";
mod = a: b: a - b * (builtins.floor (a / b)); 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;
} }