2024 day 14 part 1 in nix
This commit is contained in:
parent
77390e08f3
commit
f49d5630fb
5 changed files with 113 additions and 3 deletions
12
2024/14/example.txt
Normal file
12
2024/14/example.txt
Normal 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
|
61
2024/14/solution.nix
Normal file
61
2024/14/solution.nix
Normal file
|
@ -0,0 +1,61 @@
|
|||
{lib, ...}: input: let
|
||||
inherit (lib.strings) trim splitString toIntBase10;
|
||||
inherit (builtins) match elemAt filter length;
|
||||
# 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;
|
||||
};
|
||||
# inherit vel;
|
||||
};
|
||||
|
||||
in {
|
||||
|
||||
inherit width height pass getQuadrants robots;
|
||||
|
||||
part1result = robots
|
||||
|> map (pass 100)
|
||||
|> getQuadrants {inherit width height;}
|
||||
|> ({tl, tr, dr, dl}: (length tl) * (length tr) * (length dr) * (length dl))
|
||||
;
|
||||
|
||||
}
|
37
2024/14/solution.test.nix
Normal file
37
2024/14/solution.test.nix
Normal 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; }
|
||||
];
|
||||
};
|
||||
})
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue