2024 day 11 part1 in nix

This commit is contained in:
tristan 2024-12-11 12:18:32 +00:00
parent a4cd13c24a
commit 506b111226
5 changed files with 117 additions and 4 deletions

1
2024/11/example.txt Normal file
View file

@ -0,0 +1 @@
125 17

48
2024/11/solution.nix Normal file
View file

@ -0,0 +1,48 @@
{lib, ...}: input: let
inherit (builtins) stringLength concatLists substring foldl';
initStones = input
|> lib.trim
|> lib.splitString " "
|> map lib.strings.toIntBase10
;
blinkStone = stone:
if stone == 0 then 1
else let
strStone = toString stone;
len = stringLength strStone;
in
if lib.mod len 2 == 0 then [
(substring 0 (len / 2) strStone |> lib.strings.toIntBase10)
(substring (len / 2) (len / 2) strStone |> lib.strings.toIntBase10)
]
else stone * 2024
;
blinkStones = stones:
if !builtins.isList stones then blinkStone stones
else map blinkStones stones
;
blinkTimes = count: stones: lib.range 1 count |>
foldl' (stones: i: blinkStones stones) stones
;
flattenStones = {count ? 0, stones}: if builtins.isList stones then
map (stone: flattenStones {stones = stone;}) stones |> foldl' (a: b: a + b) 0
else 1;
in {
inherit initStones blinkStone blinkTimes blinkStones flattenStones;
part1result = blinkTimes 25 initStones
|> (stones: flattenStones {inherit stones;})
;
part2result = blinkTimes 75 initStones
|> (stones: flattenStones {inherit stones;})
;
}

64
2024/11/solution.test.nix Normal file
View file

@ -0,0 +1,64 @@
{it, describe, ...}: let
pkgs = import <nixpkgs> {};
lib = pkgs.lib;
solution = import ./solution.nix pkgs;
in [
(describe "part 1" [
(it "gets the stones" {
actual = (solution "125 17").initStones;
expected = [125 17];
})
(it "blinks a 0 to a 1" {
actual = (solution "").blinkStone 0;
expected = 1;
})
(it "blinks a even len num to two nums" {
actual = (solution "").blinkStone 1000;
expected = [10 0];
})
(it "blinks a other nums to 2024" {
actual = (solution "").blinkStone 125;
expected = 253000;
})
(it "blinks stones once" {
actual = (solution "").blinkStones [125 17];
# |> builtins.concatLists;
expected = [253000 [1 7]];
})
(it "blinks stones list of lists" {
actual = (solution "").blinkStones [253000 [1 7]];
expected = [[253 0] [2024 14168]];
})
(it "blinks stones 2 times" {
actual = (solution "").blinkTimes 2 [125 17];
expected = [[253 0] [2024 14168]];
})
(it "counts 1 stone" {
actual = (solution "").flattenStones {stones = 17;};
expected = 1;
})
(it "counts 2 stones" {
actual = (solution "").flattenStones {stones = [1 2];};
expected = 2;
})
(it "flattens stones" {
actual = (solution "").flattenStones {stones = [[125 1 [1 2 3]] 17];};
expected = builtins.length [125 1 1 2 3 17];
})
(it "gets the result" {
actual = (solution "125 17").part1result;
expected = 55312;
})
])
]

View file

@ -3,8 +3,8 @@
"aoc-inputs": {
"flake": false,
"locked": {
"lastModified": 1733814937,
"narHash": "sha256-hF+/daBl8ud8FYSvkhOn1QnpmXEYVfoXDpPM7RKW8Zg=",
"lastModified": 1733919427,
"narHash": "sha256-9QvGJVeFT1BfuoGcFUWSr6iq4VHiaEd9SKUrl6k2tzw=",
"path": "/tmp/aoc-inputs",
"type": "path"
},

View file

@ -25,7 +25,7 @@
inherit (pkgs) lib;
in (lib.range 1 8
in (lib.range 1 11
|> map (i: let id = lib.fixedWidthNumber 2 i; in {
name = "day-${id}";
value = let
@ -36,7 +36,7 @@
example = solution example;
real = solution input;
test = tix.run [
"./${id}/solution.test.nix"
./${id}/solution.test.nix
];
};
})