2024 day 8 part 1

This commit is contained in:
tristan 2024-12-08 13:08:57 +00:00
parent d9b2d7990b
commit 47b5c84bc6
7 changed files with 238 additions and 37 deletions

12
2024/08/example Normal file
View file

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

50
2024/08/input Normal file
View file

@ -0,0 +1,50 @@
....h.....Q..............Y........................
...............................Y........C.........
...............m..........x................B......
........................Y..............qB.........
......g4.........................h..Y.....q...c...
................n.....R...........................
.......................................w........5.
........g...m...........................w5........
..n...........R.1................W.......q.5......
.........h...n.................e..................
...............................R..........B....C..
.........4................................5.e.....
.......0..4......n.......x..w.....................
.......g.....m........x..b.....W.....B.......w....
..............m........................3......C...
........q...0.......h....................C.3......
..................3.....................D.........
...............R..........3.............X.........
..............................W............k2.....
..........7............................2..........
...............A.............................X...2
.......................c...x......................
....................................d.............
.....1......................d.....................
...........1...........................e..........
.........0.7K.........................2.........W.
...b......0.....A.................................
......................1....ic.....................
......b......................i....................
..Q.....b..........................A..E...........
...7.........................V....................
........A.....................v......d............
........v............c...................8E.......
..............................V........8.....E..N.
......................6...........................
.......I....M....................V................
...G......................a.......8...............
.........r.9........a...i..................X......
...............r..i...............e............N..
.....H...........k....9.....6...............8.....
.v.....................6................V.........
.........v.......a........k..........D............
Ha..........k.........K........E.......d..........
...............y.MG..............6....D...........
.........H..G...M......9.K..............N.........
.......G.........................K................
...............M.........I.......D................
..................................................
....r....y................9.......................
....y................................N............

10
2024/08/sample1 Normal file
View file

@ -0,0 +1,10 @@
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......#...
..........
..........

48
2024/08/solution.nix Normal file
View file

@ -0,0 +1,48 @@
{lib, ...}: let
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;};
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 {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
;
toNodes = {chars, ...}@amap: amap // {
nodes = map (antenna:
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 ++ [(addVec antenna delta)]
) [] chars
) chars;
};
getUniqueNodes = amap: let
size = amap.x;
in amap.nodes
|> lib.concatLists
|> builtins.filter ({x, y}: x > 0 && y > 0 && x < size && y < size)
|> lib.unique
;
in {
inherit addVec subVec toCharList toNodes;
part1result = s: s
|> lib.trim
|> lib.stringToCharacters
|> toCharList
|> toNodes
|> getUniqueNodes
|> builtins.length
;
}

26
2024/08/solution.test.nix Normal file
View file

@ -0,0 +1,26 @@
{describe, it, ...}:
let
pkgs = import <nixpkgs> {};
solution = import ./solution.nix pkgs;
exampleInput = pkgs.lib.readFile ./example;
sample1Input = pkgs.lib.readFile ./sample1;
in [
(describe "part 1" [
(it "get's the answer with example input" {
actual = solution.part1result exampleInput;
expected = 14;
})
(it "get's the answer with other sample" {
actual = solution.part1result sample1Input;
expected = 4;
})
(it "adds vectors" {
actual = solution.addVec {x = 1; y = 2;} {x = 3; y = 4;};
expected = {x = 4; y = 6;};
})
(it "subtracts vectors" {
actual = solution.subVec {x = 4; y = 3;} {x = 1; y = 2;};
expected = {x = 3; y = 1;};
})
])
]