mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Compare commits
2 Commits
1dc59c18eb
...
5e9a24c8d7
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e9a24c8d7 | |||
| 1468c87347 |
17
2025/day01/README.md
Normal file
17
2025/day01/README.md
Normal 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
|
||||
@@ -4,28 +4,34 @@
|
||||
data = builtins.readFile input_file;
|
||||
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;
|
||||
abs = n: if n < 0 then -n else n;
|
||||
|
||||
recurse = list: position:
|
||||
recurse = list: position: score1: score2:
|
||||
let
|
||||
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 = builtins.fromJSON num_part;
|
||||
next = if (builtins.substring 0 1 first) == "R"
|
||||
then
|
||||
position + num
|
||||
else
|
||||
position - num;
|
||||
|
||||
new_position = mod ((mod next 100) + 100) 100;
|
||||
delta = mod (if (builtins.substring 0 1 first) == "R" then num else -num) 100;
|
||||
next = mod (position + delta + 100) 100;
|
||||
|
||||
score = if new_position == 0 then 1 else 0;
|
||||
in
|
||||
if list == [] then
|
||||
score = if next == 0 then 1 else 0;
|
||||
circles = num / 100;
|
||||
|
||||
passed = if position == 0 then
|
||||
0
|
||||
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
|
||||
recurse lines 50;
|
||||
recurse lines 50 0 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user