Compare commits

...

2 commits

Author SHA1 Message Date
tristan ef621d0fdb 2024 day 14 part 2 in nix 2024-12-14 12:48:29 +00:00
tristan f49d5630fb 2024 day 14 part 1 in nix 2024-12-14 12:09:30 +00:00
5 changed files with 136 additions and 3 deletions

12
2024/14/example.txt Normal file
View file

@ -0,0 +1,12 @@
p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3

84
2024/14/solution.nix Normal file
View file

@ -0,0 +1,84 @@
{lib, ...}: input: let
inherit (lib.strings) trim splitString toIntBase10;
inherit (builtins) match elemAt filter length genList concatStringsSep;
# real
width = 101;
height = 103;
# example
# width = 11;
# height = 7;
getQuadrants = {width, height}: robots: let
mid.x = width / 2;
mid.y = height / 2;
tl = robots |> filter ({pos, ...}: pos.x < mid.x && pos.y < mid.y);
tr = robots |> filter ({pos, ...}: pos.x > mid.x && pos.y < mid.y);
dl = robots |> filter ({pos, ...}: pos.x < mid.x && pos.y > mid.y);
dr = robots |> filter ({pos, ...}: pos.x > mid.x && pos.y > mid.y);
in {inherit tl tr dl dr;}
;
robots = input
|> trim
|> splitString "\n"
|> map (line: line
|> match ''p=([0-9]+),([0-9]+) v=(-?[0-9]+),(-?[0-9]+)''
|> (nums: {
pos = {
x = elemAt nums 0 |> toIntBase10;
y = elemAt nums 1 |> toIntBase10;
};
vel = {
x = elemAt nums 2 |> toIntBase10;
y = elemAt nums 3 |> toIntBase10;
};
})
)
;
pass = time: {pos, vel}: let
x = lib.mod (pos.x + (vel.x * time)) width;
y = lib.mod (pos.y + (vel.y * time)) height;
in {
pos = {
x = if x < 0 then width + x else x;
y = if y < 0 then height + y else y;
};
};
printMap = robots: genList (y:
genList (x:
robots
|> filter ({pos,...}: pos == {inherit x y;})
|> length
|> (amt: if amt == 0 then "." else toString amt)
) width
|> concatStringsSep ""
) height
|> concatStringsSep "\n";
anim = {start, period}: 100 |> genList (i: let
seconds = (i * period) + start;
in robots |> map (pass (seconds)) |>
(room: ''
seconds: ${toString seconds}
${printMap room}
'')
) |> concatStringsSep "\n";
in {
inherit width height pass getQuadrants robots printMap;
part1result = robots
|> map (pass 100)
|> getQuadrants {inherit width height;}
|> ({tl, tr, dr, dl}: (length tl) * (length tr) * (length dr) * (length dl))
;
part2result = # figured these settings out after observing the first 500 seconds:
anim {start = 14; period = 101;}
|> builtins.toFile "robots-anim";
}

37
2024/14/solution.test.nix Normal file
View file

@ -0,0 +1,37 @@
{it, describe, ...}: let
pkgs = import <nixpkgs> {};
solution = import ./solution.nix pkgs;
in [
(it "splits the stupid thing into quadrants" {
actual = (solution "").getQuadrants {width = 7; height = 5;} [
{ pos.x = 2; pos.y = 1; }
{ pos.x = 4; pos.y = 1; }
{ pos.x = 2; pos.y = 3; }
{ pos.x = 4; pos.y = 3; }
{ pos.x = 0; pos.y = 2; }
{ pos.x = 1; pos.y = 2; }
{ pos.x = 2; pos.y = 2; }
{ pos.x = 3; pos.y = 2; }
{ pos.x = 4; pos.y = 2; }
{ pos.x = 3; pos.y = 0; }
{ pos.x = 3; pos.y = 1; }
{ pos.x = 3; pos.y = 2; }
{ pos.x = 3; pos.y = 3; }
{ pos.x = 3; pos.y = 4; }
];
expected = {
dl = [
{ pos.x = 2; pos.y = 3; }
];
dr = [
{ pos.x = 4; pos.y = 3; }
];
tr = [
{ pos.x = 4; pos.y = 1; }
];
tl = [
{ pos.x = 2; pos.y = 1; }
];
};
})
]

View file

@ -3,8 +3,8 @@
"aoc-inputs": { "aoc-inputs": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1734085033, "lastModified": 1734173652,
"narHash": "sha256-UlLhIP9NbAywjOGRkrULOa0e+AtY2Eb7Vgem/o/pN7Y=", "narHash": "sha256-jXnmQLoXNcaqYY/uupC8f8JQ27zdwSVBmxAnIxlennk=",
"path": "/tmp/aoc-inputs", "path": "/tmp/aoc-inputs",
"type": "path" "type": "path"
}, },

View file

@ -25,7 +25,7 @@
inherit (pkgs) lib; inherit (pkgs) lib;
in (lib.range 1 13 in (lib.range 1 14
|> map (i: let id = lib.fixedWidthNumber 2 i; in { |> map (i: let id = lib.fixedWidthNumber 2 i; in {
name = "day-${id}"; name = "day-${id}";
value = let value = let