2024 day 11 in nix

This commit is contained in:
tristan 2024-12-11 22:14:48 +00:00
parent 7b0b6393a8
commit ad5b412c3a
2 changed files with 43 additions and 103 deletions

View file

@ -1,48 +1,52 @@
{lib, ...}: input: let
inherit (builtins) stringLength concatLists substring foldl';
{lib, ...}: let
inherit (builtins) stringLength concatLists substring foldl' add attrValues;
inherit (lib.strings) toIntBase10;
in input: rec {
initStones = input
blinkStone = stone: count:
if toIntBase10 stone == 0 then [{ "1" = count; }]
else let
strStone = toString (toIntBase10 stone);
len = stringLength strStone;
left = (substring 0 (len / 2) strStone );
right = (substring (len / 2) (len / 2) strStone);
in
if lib.mod len 2 == 0 then
if left == right then [{${left} = count * 2;}] else [
{ ${left} = count; ${right} = count; }
]
else [{
${toString ((toIntBase10 stone) * 2024)} = count;
}]
;
bfast = stones: stones
|> (builtins.mapAttrs (stone: blinkStone stone))
|> attrValues
|> concatLists
|> addStones
;
bfastt = times: stones:
if times == 0 then stones |> attrValues |> foldl' add 0
else bfastt (times - 1) (bfast stones)
;
addStones = builtins.zipAttrsWith (stone: foldl' add 0);
initialStones = input
|> lib.trim
|> lib.splitString " "
|> map lib.strings.toIntBase10
|> map toIntBase10
|> toStones
;
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
toStones = numbers: numbers
|> map (number: {name = toString number; value = 1;})
|> builtins.listToAttrs
;
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;})
;
part1result = bfastt 25 initialStones;
part2result = bfastt 75 initialStones;
}

View file

@ -1,64 +0,0 @@
{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;
})
])
]