2024 day 6 part 2 (but very slow)

This commit is contained in:
tristan 2024-12-06 15:12:28 +00:00
parent 66b7646869
commit 69be745c60
4 changed files with 4973 additions and 18 deletions

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

@ -0,0 +1,130 @@
..........#.............................#...............#...........#....................#...........#............................
..............................#.......#...#.....#.....#...............#.#..#.......................#............#.................
.............#...........#.#........#............#..........................#.........#.......#......#............................
......................................................#....#..#.............#................................#...#............##..
...............#.....#....................................#.................O.OOO...OOO.OO.........O..OO.........#................
..................##............#......#..................O.O.......O..OOO...O.OO...OO..OO.........O...#...................#......
.#......#...........OO..OOO...O......O...O.OOO..O.OO..OO....O.......#............................#................................
#..#.............#...............................#.....#....#.............................#....#..............................#...
#...##..........#.........................#.................................................#............#...............#........
............#...............#.............................O................#.......................OO..O.....#..........##........
..............#...................#......##...#.....#.....O..............#..O...............O....................................#
.#.....#....#......................#...#..O..O..O.....O.....................O..O#.#.............#.........#.......................
#..#..............O#........##.........#.....#..........#.....##............O............#..O..#.#................................
...............#..O............................#.....#....O.................O..........................#..##......................
......#...........O.......#..........#...................O..O..........O.O..O...O#....#.....O.....................................
.............................................O.#...#....##.................................................#.....#................
.................#.........#..............................#........#...#...#...OOO...O..O.....O.O.O...O.....O..#..................
...........#..#...O.#...#.....................O...#..........#.#.............#....#...................##....O...O.................
.................O.......O...............#....O......O.............#.#............#...#...##..O.O.......................#.........
.....#............O.........................#.O.......................OO.O..O...O...OO.......O...#..........O...#.................
..................O.....................O.....O...#..O.............O.#..........O.....#.............#.#..#..................#.....
............................................#.....#..O...................#........#..........................................#....
.......#..........O...............#...#.......OO...O.....O..O.......O.OO.O#.........................#...#..........#......#.......
............#.....O.....##.........O.....O...O.....O................O.............................#......................#........
#.#..............O...........O.......O..OOO..OOO..#........#.......O..#.#O.#...#O...............O...............#.................
..#.........O.O...O.........#.#...........O.O........O.#....##.....O..O..O.....OO...OO.......O...........O.OO...#............#....
....................................................................O.OO.......O.....#...............#......O....#..........#.....
..............O...O.#..........#................................#...............................................#.................
#...........O..#.#O..........#.........#..........#...................O......#......................##........#.........#.........
....#....#..O.....O.....................O...................O......O..O..................#...#...........#............#...#...#...
........................................O.....O..O.....##................O.....#O..#................................#.............
.......#..........O##.....................O.O...#....O......O..#...O..O.......................#...................#......#........
......#.........#.O.................................................O..O.......#........#..#..........#.....O.#..#...#.....#....#.
....#.............O..............................O#........#....#..................................#..............................
..........#.O.O..#O..........#...................O...O............#..............#.......#...#............#.O..O..................
............O................O..O............OOO...O..O....O........O..O.OO....O.....O.O..O#....O..#..............................
.#................O.................................#O.............O..O...........#..................#......O..O.........#........
......#..#.#......O.........#...........O.....O..O#..O..........................O....#.............#...............#..............
...................O....O...O..#........O...O....O#..O.....#.......O..O.....................................O..O..................
......................#.............................................O.OO.O.....O....OO....O.O.OO......O..#..O..O...#..............
...............................................................................................................................#..
.......#.O........O...#............................................O..O...........#......................#.......................#
#........O........O.............#...........O..................#...O..O#..........O..O......O.........#...#...#........#..........
............................#....#...#.........#....#.....OOO.O..OO.OO.O.OO.O...OO......O.O.....O....................#............
.........OO.O.O...O..#...........O.............O.....OO....O....O...O.OOO.O....O..O..O.#..#....#............O.....#...............
.........OO.O.O...O...#.......O.....#............O................#.OOOO..O......OO..........O...O.O.....O...O........#...........
....#.............O...#.#.........##O...OOO.OOOOOOO.#.....#...........O..O......O.....O.........O...........O..#.............#....
..............................O#...#....O.....O..O.....#........O...O.OOOO.....O.....O......O.O#O.....#.O...O.....................
....#..............................................O..............................................................................
...#...............O..OOO...O.OO.O.OO..O.OOOOOOOOOOOOOOOOOO.OO.OO.OOO.OOOOOO.OOOOO..OOOOO.O.OOOOOO..O.OO.OO#O...........#.......#.
..#.O....OO.O.O.##OOOO.OOOOO..O....................O...........................#...#........................O...#............#....
....O....O............#.......#............#OO.O.O.O.....O.O..O..OO.OO.O..O......O....O..O...O......#..................#......#...
....O..............#.............#..O............OOO#.................O..O......O..#..O.........#........#O.O................#....
....................#O..........#...O..........O................O...O.OOOO.....OO....O..O.#......#.......#O.O........#.....##.....
....O....OO#O.O.........................O........O.O.#..........................O..#..O...........#.......O.O........#............
......................#.........#................O.O#......#...........................................#..........................
..#.O..#...#OO.O.........O.O....O...O...O#.......O.O....#................O....#.O...O.O..O.#..O......O..O......................#..
....O#.O.O....O....O..OOO......O..O#O...#.....O..O.O.......................#...................#...#......O.O....#................
..............O....#...#...........#O..............O........#.............#........................#...#O.O.O........#..........#.
...#O..O.OO...O#O.....#O....O.#...O.O........#....OO#.....O....#.....#O......O..OO....O...O..O.OO..O....O.........................
..#..................O............O.....O......O......O..#......#........O......O.....O.....#...........#.O.O..#..#...............
........#...................O.O..O..O#.#..........O.......O.#...#...#......#.....#O.......................O.O....................#
....O..O.OO..#O.O.#....O...#OOO..OO.OO.........O................O...OO.O.O...........O........O.OO..O.....O.O........#.........#..
..................................O............O................O...OO.O........O....O..........OO..#.....O.O.....................
..#.O..................O...#....#...O.O..O...O.O.........O.OO.OO.O.OO.O...O..O..OO....O...O...............O.O#....................
.....................O.......O....O.O..#.....#.#.O.O....O................O......O.....O#.O....#........#.#O.O.......O........#....
.................#.....O..#.........O............O.O....O......#...#..#..O......O...O.O................#....O.......O.............
....O..O.OO#..O......O.O.....O...........................................O......O.....O..O...........O.......#....................
......................#......O....O.O....#...#...O.O.......#................##................#.......#...O.O.....#.O.......#.....
....O.......#..#.............O..#...O.........O..OOO....O.O..............O....#.O.O...O..O.##.............O.O..##..........#......
....O.#.#............O.O.....O..#.O..........#...OOO....O.O..............O........O......................#..........O.............
#.....................O.O..O.....OOO...#.#.....O^O.O........O...................O.....O..O......#..O.O....O.O.......O.............
......OO#O..OO.O.O..O..OO..O.O..OOO.O.OO.O.OOOOO.O.O....O...........#....O......O...#.O..O...........O....O.O.......O.............
...#OO..OO..OO.O.O.OO.O.OOOO.O....O...O..........#.O...............#.....O......O..#.............................#...#.#..........
....................#........#...........##........O..............#......O......O...#..................#................#.........
#.............O.O......O....O.O...O.OOO...........OO......O.......................O...............................................
............#..........O....O.O.....O.............O.......O.........................................#.#.........#..............#..
...............#....#O...........#O...O............O....O................O......O...#.O..O.##......O.OOO.O.O.OOOO....OO...OOO.#...
......OO..O...O.O....O.O....O#O...O.O.O#......O...OO...#..O...#.......#...........O.....#...........O............#........#.......
.......................O..........O...O...........OO..#...O..............O......O.O...O.O......OO...OO.O..O.O.O#.................#
.......O..O...O..#.#...O...........#O.OO...OOO.O.OOO..OO.OOOOOOO..O.OOOOO.OO.OOO.OO....OO....O..OO..O....OO...............#..O.#..
......OO#.O...O.......#O..............O......#....OO.....#...........#....................................O.#..............#......
......O.........O.....#O....O.O..............#....OO....O#O....#.....##..O......O.....O..O..........OO....O........#....#.........
.............#.O.O.....O...O.O....O.O.O....OOO.O.O.O.OOOOO.OOO.O..O..OOO.OO..O..O.O...O.OO....#.....OO...#O...O#....O.............
......OO..O.##..O......O....O.O...O...O..#........OO......O................#..#.#...................O.........O......#.......O....
......OO..O............O..........O...O........O..O.......O..........O...O......O.............#......O....O...O.....O......#.O....
...#...O...............O.........#..#..#....................................#..............#....#.................................
.....#O.OO.OOO.O.O..O..OOO.O.O........O............O............#.....#......#....O....#O...........OO....O...O.....O........O....
....##......#.....................#...O......#..............#............O.....#.#....O..O...O..#...OO...#O...O......#....#..O....
.....#.O.#......O.O#.O.O....O.O.#....OO..O...#....OO....O................O............O..O...O......OO....O#..O.....O.............
.......O.#O.O..O.O....................O......#....OO....O#O..............O........O...O..O...O.##..OOO#...O...O....#..............
.....##.#............##OOOOO.O.OOOOOO.O.....#......O....O..............#.O............O..O..........OO...#........#..........O....
.......................#..............##...#......OO....O.O..............O........O...O........#....O....#O...O...................
..#....O...#.......#...#..##............#...OO.O...O.OO.....OO...................O....OOO....OOOO....O....O..#O.....O....#...O.#..
..#.............O.O........O.........O.OO.O..OOO.OOOOOO.OOO..O..OOO..OOO#O........O...O#.O...O......O#....O.........O....#........
...........#....O.O...#O......O..#.....#..........O.......O..............................O...O...........#O.#.....................
.......O..#.....O.O....O...#OOOO....OOO.O.O.OO.O.O.O.OOO.O..OOOO..O.OO.O............##.#.....O..O........O...OO........#.#......#.
#............................................#.....O...................O.##...#..........OO..OO......O#...O...O.....#.............
..#....O.......#.O...O.O..OOOO.OO...O.O.O.O.OO.O.O.O.OOOOO..OOOO..O.OO.O..OOOO.O.OO.....................................#.........
.......O...........................................O#..................O.#............#..............O#...O......................#
........................................................................#.......#....................O...O.OOO.O.OO.OO#.#.........
................#..#......................#...........................................#OO..O.OOOOO.O.....OO..OOO.....O............
.......O................##.........................O....O.............#O............##...............O..........#....O..#....#....
.........#.......#...O.O..O....O................#......#....#..........O.#..................#...#...O.O...........................
#......O..#............O...............#....#.#..#...OO................O............................OOO...O##.O......O............
.#..##.O.............#.O..#...................O.O.OOO.O.#..............O#........#.....O.....OO.O...O....O...............#......#.
...#......................O............O......O...OOO.O.........#......O..............#..O...O.......OO.#.O...#......O............
...#...........#..#.............................#.....#.................................O.O...O..O...OO....O.O......#O............
...#.........................#.OO...O...O.O..........O.OOO..OOO...O.O.....O......O.....O.............O....O..........O......##....
.........................#.........................O.#.O.......O.......O.....#...........#...........O....O........O.O............
.#..................................#.......................O................................O........O...........................
..............................................O....O...O.......O....#..O........##.....#.#...O.....#.O...#O........O.O............
.......O........#...............................#........#..OOO...O.O.....O......O.....O.............OO............O.O............
......#....#...........O.....................#O..O.....OOOO.OOOO..O.OO.O.........#.#.........#.....#O.....O........O.O..#.........
................#..............................#.......O...............#...#.....#.......#...........O....#........O.O............
....................#O....OO.O.OOOO.O...O.O...O..O.........#..#...........#.#.................#...#................O.O....#.......
.......O.....#..#...#............................#.#...O..............................O........#......O..............O.#..........
.........#..........#.................#................O...................#.#.......................O.............O.O..#.........
....#.............#.......................O....OOO.O..OOO.......OOO...O.O#...........#.O...O..OOO..O..O..........#...O............
.......O#.#............................#...........................................#.................#O.............#..........#..
..............#..........................#............#O........#....#..O#.....#......................O..#....#.....OOO.OO....##..
...................#OOO..OOO.O.OOOO.O..OOOOOOO...O.O...O................O....#.......#........#..............#.....O.O..#.........
.#.....O..#....#.......O..........................#..#.#...............#O...........#...........#..................O.O............
...............................................................O........O......................#.....#....#........O.O..#....O....
...........#..........#..OOO.O.OOOO.O.....O.O....O..O.OO.O..O.....#.........#..............#................#......OOO.#..........
............#.................................###..............#.#......O...........................................O............#
......#O.O....O.O..OO.OO.OOOOO.OOOO.OO.O.OOOOO...O.OO.OO.O..O.O...O..OO...................#............#...#........O.#.....#.....
.............#..................#...................#......#.........#..#.......................#....#.OO.O..OO.O.....#......O...#
..#....##................................#.................#...........................#.......#....#...#..........#.O............
...............................#......................................#.........#.............#......................#.......O..#.

View file

@ -2,10 +2,11 @@
content = lib.trim input;
chart = content
chart = toChart content;
toChart = s: s
|> lib.strings.splitString "\n"
|> map (lib.strings.splitString "")
|> map (line: lib.sublist 1 (builtins.length line - 2) line)
|> map (lib.stringToCharacters)
;
startingPos = let
@ -22,7 +23,7 @@
index = i: list: builtins.elemAt list i;
getChar = {x, y}: chart
getChar = {x, y}: chart: chart
|> index y
|> index x
;
@ -30,33 +31,101 @@
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;
replace2d = {x, y}: updated: chart: let
newRow = chart |> index y |> replace x updated;
in
replace y newRow chart;
replace = i: updated: list:
let before = lib.sublist 0 i list;
after = lib.sublist (i + 1) (builtins.length list - i) list;
in
if i < 0 || i > builtins.length list then list else
before ++ [updated] ++ after;
progress = {visited ? {}, pos, dir, obs ? [], charts ? chart, ...}: let
newDir = if colliding then rotate dir else dir;
newPos = addVec pos newDir;
colliding = nextChar == "#";
nextChar = getChar fwd charts;
fwd = addVec pos dir;
hash = posToString pos;
exited = fwd.y >= height || fwd.x >= width || fwd.y < 0 || fwd.x < 0;
canObstruct =
# unique
(!lib.elem (posToString fwd) obs)
# inside map
&& (!exited)
# can't put an obstacle when there already is one
&& (!colliding)
# if I already visited the place in front, putting an obstacle there won't work, as I can't get to where I am now!
&& (!lib.hasAttr (posToString fwd) visited)
# see if I will end up in a loop turning right here
&& getLoop {inherit pos visited dir; charts = replace2d fwd "#" charts;};
in
{
visited = visited // {${posToString pos} = true;};
exited = fwd.y >= height || fwd.x >= width || fwd.y < 0 || fwd.x < 0;
pos = newPos;
visited = visited // {${hash} = visited.${hash} or [] ++ [dir];};
pos = if colliding then pos else newPos;
dir = newDir;
inherit exited charts;
obs = obs ++ (if canObstruct then [(posToString fwd)] else []);
};
rotate = {x, y}: {x = -y; y = x;};
startingDir = {x = 0; y = -1;};
getVisits = {pos ? startingPos, dir ? startingDir, visited ? {}, ...}:
getVisits = {pos ? startingPos
, dir ? startingDir
, visited ? {}
, obs ? []
, charts ? chart
, ...}:
let
nextStep = progress {inherit pos dir visited;};
in if nextStep.exited then visited else getVisits nextStep;
nextStep = progress {inherit pos dir visited obs charts;};
in if nextStep.exited then nextStep else getVisits nextStep;
part1result = getVisits {}
path = getVisits {};
part1result = path.visited
|> builtins.attrNames
|> builtins.length
# the last position
|> (v: v + 1)
;
getLoop = state:
let
nextStep = progress state;
hash = posToString state.pos;
enteredLoop = lib.elem state.dir (state.visited.${hash} or []);
in if enteredLoop then true
else if nextStep.exited then false
else getLoop nextStep;
part2result = builtins.trace "This is very slow!" (path.obs |> builtins.length)
;
# visualisation stuff
hashToVec = s: s |> lib.splitString "," |> map lib.strings.toInt |> (v: {x = index 0 v; y = index 1 v;});
annotateObs = chart:
builtins.foldl'
(c: obs: let pos = hashToVec obs; in replace2d pos "O" c) chart path.obs;
annotatePath = chart:
builtins.foldl'
(c: hash: let
pos = hashToVec hash;
dir = index 0 path.visited.${hash};
sym = if dir.x == 1 then ">" else if dir.x == -1 then "<" else if dir.y == 1 then "v" else "^";
in replace2d pos sym c) chart
(builtins.attrNames path.visited);
chartToStr = c: c
|> map (lib.concatStringsSep "")
|> lib.concatStringsSep "\n"
;
annotated = chart |> annotatePath |> annotateObs |> chartToStr;
}

View file

@ -15,8 +15,19 @@
example = (pkgs.lib.readFile ./06/example.txt);
input = (pkgs.lib.readFile ./06/input.txt);
in {
part1example = (solution example);
part1 = (solution input);
example = (solution example);
sample1 = (solution ''
.#..
.^#.
.#..'');
sample2 = (solution ''
.#......
........
.....#..
#.......
..#.#...
...^....'');
real = (solution input);
};
packages.x86_64-linux.watch = tix.watch {

4745
2024/results.nix Normal file

File diff suppressed because it is too large Load diff