2025 day 1
This commit is contained in:
parent
902917426f
commit
9caa01f1a7
6 changed files with 4570 additions and 26 deletions
44
2025/day_1.nix
Normal file
44
2025/day_1.nix
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# nix eval -f ./day_1.nix -I 'input=(builtins.readFile ./puzzle_cache/day_1_1)' countZero
|
||||
{ input ? (builtins.readFile ./puzzle_cache/day_1_0) }:
|
||||
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);
|
||||
|
||||
# Parse a line like "L68" or "R14"
|
||||
parseLine = line: {
|
||||
dir = builtins.substring 0 1 line;
|
||||
dist = toIntBase10 (builtins.substring 1 (builtins.stringLength line - 1) line);
|
||||
};
|
||||
|
||||
rotations = map parseLine lines;
|
||||
|
||||
# Fold over the list to simulate the dial
|
||||
step =
|
||||
state: rot:
|
||||
let
|
||||
position =
|
||||
if rot.dir == "L" then
|
||||
mod (state.position - rot.dist) 100
|
||||
else if rot.dir == "R" then
|
||||
mod (state.position + rot.dist) 100
|
||||
else
|
||||
builtins.trace "Invalid direction: ${rot.dir}" state.position;
|
||||
countZero = state.countZero + (if position == 0 then 1 else 0);
|
||||
in
|
||||
{
|
||||
inherit position countZero;
|
||||
};
|
||||
|
||||
initialState = {
|
||||
position = 50;
|
||||
countZero = 0;
|
||||
};
|
||||
|
||||
finalState = builtins.foldl' step initialState rotations;
|
||||
in
|
||||
{
|
||||
inherit (finalState) position countZero;
|
||||
}
|
||||
10
2025/puzzle_cache/day_1_0
Normal file
10
2025/puzzle_cache/day_1_0
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
4503
2025/puzzle_cache/day_1_1
Normal file
4503
2025/puzzle_cache/day_1_1
Normal file
File diff suppressed because it is too large
Load diff
4
2025/utils.nix
Normal file
4
2025/utils.nix
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
toIntBase10 = str: builtins.fromJSON "${str}";
|
||||
mod = a: b: a - b * (builtins.floor (a / b));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue