This is my go at Advent of Code (2024) in pure nix.
In general, nix eval '.#dayX' (where 'X' is the number of the day, padded to
length 2, such as nix eval '.#day03') will display the answer to a given day.
Note, many solutions require increasing the max call depth and increasing the stack size. For all solutions, consider running them as:
$ ulimit -s unlimited
$ nix eval --option max-call-depth 4294967295 ...
You also likely want to run parts one at a time, such as nix eval '.#day01.part2' in order to reduce total resource usage; I only ever run them
like such, so some solutions may OOM if both parts are run together.
I'm also running this on a machine with 64GiB of memory, so solutions may take up to that amount of memory.
I also have experimental-features = ca-derivations dynamic-derivations flakes nix-command recursive-nix set in my nix show-config, though I think only
flakes nix-command are used in practice in this repo.
If for some reason you want to see how slow tvix is on something, you can do that too:
$ time ./run-tvix.sh day06.part1
19.77s
max memory: 2879 MB
# as compared to nix
$ time nix eval --option max-call-depth 4294967295 '.#day06.part1'
3.07s
max memory: 2139 MB
In general tvix seems to be significantly slower, but it's interesting to compare!
Since tvix doesn't support fetchGit yet, run-tvix will populate a
'nixpkgs' symlink in the repository to use automatically.
This is done using nix, so you need nix installed as well.
On my machine, running all solutions serially takes about 2 hours:
$ time nix run --option max-call-depth 4294967295 '.#check.all'
6537.53s user 55.99s system 99% cpu 1:50:16.46 total
max memory: 40045 MB
This is on a decently fast desktop cpu (Ryzen 7 7700X), so YMMV.
I'll update these numbers if I ever bother to make them much faster, but it's all dominated by running reasonably efficient graph algorithms (dijkstra's) just being really slow in nix, so I feel like I'd need a breakthrough on how to implemnet Dijkstra's in a way that doesn't tank nix's performance in order to get the runtime under an hour.
Part 2 takes an hour and 20GiB of memory to run on my machine.
I know there's a more optimal solution, but you know, it's fine.
Both parts take around 12GiB of memory and 1.5 minutes.
~10 minutes, but not much memory!
Thank gosh for builtins.deepSeq to avoid 60+ GiBs of thunks!
Part1 is 5 minutes.
Part 1 is ~5 minutes.
Part 1 took 37GiB of memory on my machine
Both parts take ~7.5 minutes (with 20GiB of memory usage).
| Day | CPU Time (s) | Wall Time (s) | Peak Memory (GB) |
|---|---|---|---|
| day01 | .04 | 0.1 | .04 |
| day02 | .07 | 0.07 | .05 |
| day03 | .03 | 0.04 | .04 |
| day04 | .13 | 0.14 | .07 |
| day05 | .36 | 0.37 | .23 |
| day06 | 3657.90 | 2383.72 | 4.88 |
| day07 | .15 | 0.16 | .08 |
| day08 | .09 | 0.1 | .05 |
| day09 | 180.70 | 143.12 | 8.44 |
| day10 | .07 | 0.08 | .06 |
| day11 | 561.84 | 562.52 | .45 |
| day12 | 703.25 | 540.01 | 2.74 |
| day13 | .12 | 0.12 | .07 |
| day14 | 42.51 | 32.9 | 2.05 |
| day15 | 370.38 | 332.77 | 11.75 |
| day16 | 605.60 | 529.05 | 19.95 |
| day17 | .06 | 0.07 | .05 |
| day18 | 244.87 | 223.38 | 25.93 |
| day19 | 4.37 | 4.24 | .44 |
| day20 | 930.12 | 819.36 | 24.76 |
| day21 | .04 | 0.05 | .05 |
| day22 | 170.40 | 67.83 | .54 |
| day23 | .63 | 0.63 | .32 |
| day24 | 7.40 | 7.34 | .44 |
| day25 | .16 | 0.17 | .11 |
xychart-beta
title "CPU Time (User + System)"
x-axis [day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13, day14, day15, day16, day17, day18, day19, day20, day21, day22, day23, day24, day25]
y-axis "Time (seconds)" 0 --> 3667
bar [.04,.07,.03,.13,.36,3657.90,.15,.09,180.70,.07,561.84,703.25,.12,42.51,370.38,605.60,.06,244.87,4.37,930.12,.04,170.40,.63,7.40,.16]
xychart-beta
title "Wall Clock Time"
x-axis [day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13, day14, day15, day16, day17, day18, day19, day20, day21, day22, day23, day24, day25]
y-axis "Time (seconds)" 0 --> 2393
bar [0.1,0.07,0.04,0.14,0.37,2383.72,0.16,0.1,143.12,0.08,562.52,540.01,0.12,32.9,332.77,529.05,0.07,223.38,4.24,819.36,0.05,67.83,0.63,7.34,0.17]
xychart-beta
title "Peak Memory Usage"
x-axis [day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13, day14, day15, day16, day17, day18, day19, day20, day21, day22, day23, day24, day25]
y-axis "Memory (GB)" 0 --> 26
bar [.04,.05,.04,.07,.23,4.88,.08,.05,8.44,.06,.45,2.74,.07,2.05,11.75,19.95,.05,25.93,.44,24.76,.05,.54,.32,.44,.11]