From e1c30064569d39765a39cc2f62d47ef19f4e1408 Mon Sep 17 00:00:00 2001 From: tristan Date: Sun, 8 Dec 2024 15:01:56 +0000 Subject: [PATCH] 2024 day 8 part 2 --- 2024/08/solution.nix | 63 +++++++++++++++++++++++++++++++++----- 2024/08/solution.test.nix | 64 +++++++++++++++++++++++++++++++++++++++ 2024/flake.nix | 16 ++-------- 3 files changed, 121 insertions(+), 22 deletions(-) diff --git a/2024/08/solution.nix b/2024/08/solution.nix index 87a0a8a..b8b5176 100644 --- a/2024/08/solution.nix +++ b/2024/08/solution.nix @@ -1,15 +1,19 @@ -{lib, ...}: let +{lib, ...}: input: let + + abs = n: if n > 0 then n else -n; addVec = a: b: {x = a.x + b.x; y = a.y + b.y;}; subVec = a: b: {x = a.x - b.x; y = a.y - b.y;}; + multVec = m: vec: {x = vec.x * m; y = vec.y * m;}; + modVec = m: vec: {x = lib.mod vec.x m; y = lib.mod vec.y m;}; toCharList = builtins.foldl' ({y, x, chars}: char: if char == "." || char == "#" then {inherit y; x = x + 1; inherit chars;} - else if char == "\n" then {y = y + 1; x = 1; inherit chars;} + else if char == "\n" then {y = y + 1; x = 0; inherit chars;} else {inherit y; x = x + 1; chars = chars ++ [{inherit x y char;}];} ) - {y = 1; x = 1; chars = [];} # yeah starting at 1,1 what you gonna do about it + {y = 0; x = 0; chars = [];} ; toNodes = {chars, ...}@amap: amap // { @@ -25,17 +29,44 @@ }; getUniqueNodes = amap: let - size = amap.x; + # assumes width == height ¯\_(ツ)_/¯ + size = assert amap.x == amap.y + 1; amap.x; in amap.nodes |> lib.concatLists - |> builtins.filter ({x, y}: x > 0 && y > 0 && x < size && y < size) + |> builtins.filter ({x, y}: x >= 0 && y >= 0 && x < size && y < size) |> lib.unique ; -in { - inherit addVec subVec toCharList toNodes; + cut = i: list: lib.sublist i (builtins.length list) list; - part1result = s: s + toDeltas = {chars, ...}@amap: amap // { + deltas = chars |> lib.imap0 (i: antenna: { + pos = {inherit (antenna) x y;}; + deltas = chars |> cut i |> builtins.foldl' (acc: antenna2: + if antenna == antenna2 then acc + else if antenna.char != antenna2.char then acc + else let + delta = subVec antenna antenna2; + in acc ++ [delta] + ) []; + }); + }; + + deltaToNodes = size: {pos, delta}: + builtins.genList + (i: delta |> multVec i |> subVec (minNode size pos delta)) + (size / (lib.max (abs delta.x) (abs delta.y)) + 1) + |> builtins.filter ({x, y}: x >= 0 && y >= 0 && x < size && y < size) + ; + + minNode = size: pos: delta: let sub = addVec pos delta; in + if sub.x < 0 || sub.y < 0 || sub.x >= size || sub.y >= size then pos + else minNode size sub delta; + +in { + inherit addVec subVec toCharList toNodes toDeltas multVec modVec deltaToNodes minNode; + + part1result = input |> lib.trim |> lib.stringToCharacters |> toCharList @@ -44,5 +75,21 @@ in { |> builtins.length ; + part2result = input + |> lib.trim + |> lib.stringToCharacters + |> toCharList + |> toDeltas + |> ({deltas,x,...}: deltas |> map (antenna: + antenna.deltas |> map (delta: deltaToNodes x { + inherit delta; + inherit (antenna) pos; + } + ))) + |> lib.concatLists + |> lib.concatLists + |> lib.unique + |> builtins.length + ; } diff --git a/2024/08/solution.test.nix b/2024/08/solution.test.nix index 726d00a..dafaa4a 100644 --- a/2024/08/solution.test.nix +++ b/2024/08/solution.test.nix @@ -1,9 +1,34 @@ {describe, it, ...}: let pkgs = import {}; + lib = pkgs.lib; solution = import ./solution.nix pkgs; exampleInput = pkgs.lib.readFile ./example; sample1Input = pkgs.lib.readFile ./sample1; + + charmap = { + chars = [ + { char = "0"; x = 8; y = 1; } + { char = "0"; x = 5; y = 2; } + { char = "0"; x = 7; y = 3; } + { char = "0"; x = 4; y = 4; } + { char = "A"; x = 6; y = 5; } + { char = "A"; x = 8; y = 8; } + { char = "A"; x = 9; y = 9; } + ]; + x = 12; + y = 11; + }; + + deltamap = [ + { deltas = [ { x = 3; y = -1; } { x = 1; y = -2; } { x = 4; y = -3; } ]; pos = { x = 8; y = 1; }; } + { deltas = [ { x = -2; y = -1; } { x = 1; y = -2; } ]; pos = { x = 5; y = 2; }; } + { deltas = [ { x = 3; y = -1; } ]; pos = { x = 7; y = 3; }; } + { deltas = []; pos = { x = 4; y = 4; }; } + { deltas = [ { x = -2; y = -3; } { x = -3; y = -4; } ]; pos = { x = 6; y = 5; }; } + { deltas = [ { x = -1; y = -1; } ]; pos = { x = 8; y = 8; }; } + { deltas = []; pos = { x = 9; y = 9; }; } + ]; in [ (describe "part 1" [ (it "get's the answer with example input" { @@ -23,4 +48,43 @@ in [ expected = {x = 3; y = 1;}; }) ]) + (describe "part 2" [ + (it "get's char list" { + actual = exampleInput |> lib.trim |> lib.stringToCharacters + |> solution.toCharList; + expected = charmap; + }) + (it "gets deltas" { + actual = (charmap |> solution.toDeltas).deltas; + expected = deltamap; + }) + (it "gets nodes" { + /* + .#... + ..... + ..A.. + ..... + ...#. + */ + actual = solution.deltaToNodes 5 {pos = {x=2;y=2;}; delta = {x=-1;y=-2;};}; + expected = [ + {x = 1; y = 0;} + {x = 2; y = 2;} + {x = 3; y = 4;} + ]; + }) + (it "gets min node" { + # ..... + # #.... + # .#... + # ..A.. + # ...#. + actual = solution.minNode 5 {x = 2; y = 3;} {x = -1; y = -1;}; + expected = {x = 0; y = 1;}; + }) + (it "gets result" { + actual = solution.part2result exampleInput; + expected = 34; + }) + ]) ] diff --git a/2024/flake.nix b/2024/flake.nix index e3402c5..61a086b 100644 --- a/2024/flake.nix +++ b/2024/flake.nix @@ -16,18 +16,6 @@ input = (pkgs.lib.readFile ./06/input.txt); in { example = (solution example); - sample1 = (solution '' -.#. -..# -#^. -.#.''); - sample2 = (solution '' -.#.... -...... -.....# -#..... -..#.#. -...^..''); real = (solution input); }; @@ -48,8 +36,8 @@ example = (pkgs.lib.readFile ./08/example); input = (pkgs.lib.readFile ./08/input); in { - solution = solution; - part1result = solution.part1result input; + example = solution example; + real = solution input; test = tix.run [ ./08/solution.test.nix ];