2024 day 6 part 1

This commit is contained in:
tristan 2024-12-06 07:13:59 +00:00
parent 1f2171f3a5
commit 66b7646869
5 changed files with 294 additions and 0 deletions

10
2024/06/example.txt Normal file
View file

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

130
2024/06/input.txt Normal file
View file

@ -0,0 +1,130 @@
..........#.............................#...............#...........#....................#...........#............................
..............................#.......#...#.....#.....#...............#.#..#.......................#............#.................
.............#...........#.#........#............#..........................#.........#.......#......#............................
......................................................#....#..#.............#................................#...#............##..
...............#.....#....................................#......................................................#................
..................##............#......#...............................................................#...................#......
.#......#...........................................................#............................#................................
#..#.............#...............................#.....#....#.............................#....#..............................#...
#...##..........#.........................#.................................................#............#...............#........
............#...............#..............................................#.................................#..........##........
..............#...................#......##...#.....#....................#.......................................................#
.#.....#....#......................#...#........................................#.#.............#.........#.......................
#..#...............#........##.........#.....#..........#.....##.........................#.....#.#................................
...............#...............................#.....#.................................................#..##......................
......#...................#..........#...........................................#....#...........................................
...............................................#...#....##.................................................#.....#................
.................#.........#..............................#........#...#...#...................................#..................
...........#..#.....#...#.........................#..........#.#.............#....#...................##..........................
.........................................#.........................#.#............#...#...##............................#.........
.....#......................................#....................................................#..............#.................
..................................................#..................#................#.............#.#..#..................#.....
............................................#.....#......................#........#..........................................#....
.......#..........................#...#...................................#.........................#...#..........#......#.......
............#...........##........................................................................#......................#........
#.#...............................................#........#..........#.#..#...#................................#.................
..#.........................#.#........................#....##..................................................#............#....
.....................................................................................#...............#...........#..........#.....
....................#..........#................................#...............................................#.................
#..............#.#...........#.........#..........#..........................#......................##........#.........#.........
....#....#...............................................................................#...#...........#............#...#...#...
.......................................................##......................#...#................................#.............
.......#...........##...........................#..............#..............................#...................#......#........
......#.........#..............................................................#........#..#..........#.......#..#...#.....#....#.
....#.............................................#........#....#..................................#..............................
..........#......#...........#....................................#..............#.......#...#............#.......................
...........................................................................................#.......#..............................
.#..................................................#.............................#..................#...................#........
......#..#.#................#.....................#..................................#.............#...............#..............
...............................#..................#........#......................................................................
......................#..................................................................................#.........#..............
...............................................................................................................................#..
.......#..............#...........................................................#......................#.......................#
#...............................#..............................#.......#..............................#...#...#........#..........
............................#....#...#.........#....#................................................................#............
.....................#.................................................................#..#....#..................#...............
......................#.............#.............................#...................................................#...........
....#.................#.#.........##................#.....#....................................................#.............#....
...............................#...#...................#.......................................#......#...........................
....#.............................................................................................................................
...#.......................................................................................................#............#.......#.
..#.............##.............................................................#...#............................#............#....
......................#.......#............#........................................................#..................#......#...
...................#.............#..................#..............................#............#........#...................#....
....................#...........#.........................................................#......#.......#...........#.....##.....
...........#.........................................#.............................#..............#..................#............
......................#.........#...................#......#...........................................#..........................
..#....#...#.............................#..............#.....................#............#...................................#..
.....#.............................#....#..................................#...................#...#.............#................
...................#...#...........#........................#.............#........................#...#.............#..........#.
...#...........#......#.......#..............#......#..........#.....#............................................................
..#......................................................#......#...........................#...........#......#..#...............
........#............................#.#....................#...#...#......#.....#...............................................#
.............#....#........#.........................................................................................#.........#..
....................................................................................................#.............................
..#........................#....#............................................................................#....................
.......................................#.....#.#.......................................#......#........#.#...................#....
.................#........#....................................#...#..#................................#..........................
...........#.................................................................................................#....................
......................#..................#...#.............#................##................#.......#...........#.........#.....
............#..#................#.............................................#............##..................##..........#......
......#.#.......................#............#...........................................................#........................
#......................................#.#......^...............................................#.................................
........#...........................................................#...............#.............................................
...#.............................................#.................#...............#.............................#...#.#..........
....................#........#...........##.......................#.................#..................#................#.........
#.................................................................................................................................
............#.......................................................................................#.#.........#..............#..
...............#....#............#..................................................#......##.................................#...
.............................#.........#...............#......#.......#.................#........................#........#.......
......................................................#........................................................#.................#
.................#.#...............#......................................................................................#....#..
........#.............#......................#...........#...........#......................................#..............#......
......................#......................#...........#.....#.....##............................................#....#.........
.............#................................................................................#..........#.....#..................
............##...........................#.................................#..#.#....................................#............
..............................................................................................#............................#......
...#.............................#..#..#....................................#..............#....#.................................
.....#..........................................................#.....#......#.........#..........................................
....##......#.....................#..........#..............#..................#.#..............#........#...........#....#.......
.....#...#.........#............#............#.............................................................#......................
.........#...................................#...........#.....................................##.....#............#..............
.....##.#............##.....................#..........................#.................................#........#...............
.......................#..............##...#...................................................#.........#........................
..#........#.......#...#..##............#....................................................................#...........#.....#..
..#.....................................................................#..............#.............#...................#........
...........#..........#..........#.....#.................................................................#..#.....................
..........#................#........................................................##.#...............................#.#......#.
#............................................#...........................##...#.......................#.............#.............
..#............#........................................................................................................#.........
....................................................#....................#............#...............#..........................#
........................................................................#.......#.....................................#.#.........
................#..#......................#...........................................#...........................................
........................##............................................#.............##..........................#.......#....#....
.........#.......#..............................#......#....#............#..................#...#.................................
#.........#............................#....#.#..#.........................................................##.....................
.#..##...............#....#.............................#...............#........#.......................................#......#.
...#............................................................#.....................#.................#.....#...................
...#...........#..#.............................#.....#.............................................................#.............
...#.........................#..............................................................................................##....
.........................#...........................#.......................#...........#........................................
.#..................................#.............................................................................................
....................................................................#...........##.....#.#.........#.....#........................
................#...............................#........#........................................................................
......#....#.................................#...................................#.#.........#.....#....................#.........
................#..............................#.......................#...#.....#.......#................#.......................
....................#......................................#..#...........#.#.................#...#.......................#.......
.............#..#...#............................#.#...........................................#.......................#..........
.........#..........#.................#....................................#.#..........................................#.........
....#.............#......................................................#...........#...........................#................
........#.#............................#...........................................#.................#..............#..........#..
..............#..........................#............#.........#....#...#.....#.........................#....#...............##..
...................#.........................................................#.......#........#..............#..........#.........
.#........#....#..................................#..#.#...............#............#...........#.................................
...............................................................................................#.....#....#.............#.........
...........#..........#...........................................#.........#..............#................#..........#..........
............#.................................###..............#.#...............................................................#
......#...................................................................................#............#...#..........#.....#.....
.............#..................#...................#......#.........#..#.......................#....#................#..........#
..#....##................................#.................#...........................#.......#....#...#..........#..............
...............................#......................................#.........#.............#......................#..........#.

62
2024/06/solution.nix Normal file
View file

@ -0,0 +1,62 @@
{lib, ...}: input: rec {
content = lib.trim input;
chart = content
|> lib.strings.splitString "\n"
|> map (lib.strings.splitString "")
|> map (line: lib.sublist 1 (builtins.length line - 2) line)
;
startingPos = let
xs = map (lib.lists.findFirstIndex (char: char == "^") null) chart;
y = lib.lists.findFirstIndex (x: !isNull x) null xs;
x = builtins.elemAt xs y;
in
{inherit x y;}
;
posToString = {x, y}: "${toString x},${toString y}";
addVec = a: b: {x = a.x + b.x; y = a.y + b.y;};
index = i: list: builtins.elemAt list i;
getChar = {x, y}: chart
|> index y
|> index x
;
height = builtins.length chart;
width = chart |> index 0 |> builtins.length;
progress = {visited ? {}, pos, dir, ...}: let
nextChar = getChar (addVec pos dir);
newDir = if nextChar == "#" then rotate dir else dir;
newPos = addVec pos newDir;
fwd = addVec pos dir;
in
{
visited = visited // {${posToString pos} = true;};
exited = fwd.y >= height || fwd.x >= width || fwd.y < 0 || fwd.x < 0;
pos = newPos;
dir = newDir;
};
rotate = {x, y}: {x = -y; y = x;};
startingDir = {x = 0; y = -1;};
getVisits = {pos ? startingPos, dir ? startingDir, visited ? {}, ...}:
let
nextStep = progress {inherit pos dir visited;};
in if nextStep.exited then visited else getVisits nextStep;
part1result = getVisits {}
|> builtins.attrNames
|> builtins.length
# the last position
|> (v: v + 1)
;
}