2 Commits

Author SHA1 Message Date
5e9a24c8d7 Document Nix solution 2025-12-01 21:13:50 +01:00
1468c87347 Implement 2025 day 1 part 2 2025-12-01 20:47:21 +01:00
2 changed files with 37 additions and 14 deletions

17
2025/day01/README.md Normal file
View File

@@ -0,0 +1,17 @@
# Day 01: Nix
Nix is a functional language made for the Nix package manager. As
To run the solution program, start the `nix` repl with the solution program and call the `solve`
function with the path to the input file.
```console
$ nix repl --option max-call-depth 10000 --file solve.nix
nix-repl> solve ./sample.txt
```
Some observations:
- The `max-call-depth` needs to be bumped to at least 10k for the main input files, otherwise you
hit the limit in the recursion.
- The standard library is lacking several basics but most of those you can build yourself

View File

@@ -4,28 +4,34 @@
data = builtins.readFile input_file; data = builtins.readFile input_file;
lines = builtins.filter (s: s != "" && builtins.isString s) (builtins.split "\n" data); lines = builtins.filter (s: s != "" && builtins.isString s) (builtins.split "\n" data);
# Basic maths because the standard library doesn't have it
mod = a: b: a - (a / b) * b; mod = a: b: a - (a / b) * b;
abs = n: if n < 0 then -n else n;
recurse = list: position: recurse = list: position: score1: score2:
let let
first = builtins.head list; first = builtins.head list;
# This is not guaranteed to work but it does # This is not guaranteed to work but it's good enough
num_part = builtins.substring 1 999 first; num_part = builtins.substring 1 999 first;
num = builtins.fromJSON num_part; num = builtins.fromJSON num_part;
next = if (builtins.substring 0 1 first) == "R" delta = mod (if (builtins.substring 0 1 first) == "R" then num else -num) 100;
then next = mod (position + delta + 100) 100;
position + num
else
position - num;
new_position = mod ((mod next 100) + 100) 100;
score = if new_position == 0 then 1 else 0; score = if next == 0 then 1 else 0;
in circles = num / 100;
if list == [] then
passed = if position == 0 then
0 0
else else
score + (recurse (builtins.tail list) new_position); if delta < 0 && delta + position <= 0 || delta > 0 && delta + position >= 100 then
1
else
0;
in
if list == [] then
[score1 score2]
else
recurse (builtins.tail list) next (score + score1) (score2 + passed + circles);
in in
recurse lines 50; recurse lines 50 0 0;
} }