2024 day 8 part 2

This commit is contained in:
tristan 2024-12-08 15:01:56 +00:00
parent 47b5c84bc6
commit e1c3006456
3 changed files with 121 additions and 22 deletions

View file

@ -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
;
}

View file

@ -1,9 +1,34 @@
{describe, it, ...}:
let
pkgs = import <nixpkgs> {};
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;
})
])
]

View file

@ -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
];