From f49d5630fbc53174fbe3f86199fcf43c196ec75e Mon Sep 17 00:00:00 2001 From: tristan Date: Sat, 14 Dec 2024 12:09:30 +0000 Subject: [PATCH] 2024 day 14 part 1 in nix --- 2024/14/example.txt | 12 ++++++++ 2024/14/solution.nix | 61 +++++++++++++++++++++++++++++++++++++++ 2024/14/solution.test.nix | 37 ++++++++++++++++++++++++ 2024/flake.lock | 4 +-- 2024/flake.nix | 2 +- 5 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 2024/14/example.txt create mode 100644 2024/14/solution.nix create mode 100644 2024/14/solution.test.nix diff --git a/2024/14/example.txt b/2024/14/example.txt new file mode 100644 index 0000000..2455da4 --- /dev/null +++ b/2024/14/example.txt @@ -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 diff --git a/2024/14/solution.nix b/2024/14/solution.nix new file mode 100644 index 0000000..298de7d --- /dev/null +++ b/2024/14/solution.nix @@ -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)) + ; + +} diff --git a/2024/14/solution.test.nix b/2024/14/solution.test.nix new file mode 100644 index 0000000..03edf42 --- /dev/null +++ b/2024/14/solution.test.nix @@ -0,0 +1,37 @@ +{it, describe, ...}: let + pkgs = import {}; + 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; } + ]; + }; + }) +] diff --git a/2024/flake.lock b/2024/flake.lock index 6a78e02..b6026cd 100644 --- a/2024/flake.lock +++ b/2024/flake.lock @@ -3,8 +3,8 @@ "aoc-inputs": { "flake": false, "locked": { - "lastModified": 1734085033, - "narHash": "sha256-UlLhIP9NbAywjOGRkrULOa0e+AtY2Eb7Vgem/o/pN7Y=", + "lastModified": 1734173652, + "narHash": "sha256-jXnmQLoXNcaqYY/uupC8f8JQ27zdwSVBmxAnIxlennk=", "path": "/tmp/aoc-inputs", "type": "path" }, diff --git a/2024/flake.nix b/2024/flake.nix index 0cc8d51..3014a85 100644 --- a/2024/flake.nix +++ b/2024/flake.nix @@ -25,7 +25,7 @@ inherit (pkgs) lib; - in (lib.range 1 13 + in (lib.range 1 14 |> map (i: let id = lib.fixedWidthNumber 2 i; in { name = "day-${id}"; value = let