2024 day 16 part 2 not working

This commit is contained in:
Tristan 2024-12-18 16:51:57 +00:00
parent 4cc73313ae
commit dbe990e782

View file

@ -111,6 +111,7 @@
${key init.pos} = { ${key init.pos} = {
dir = "east"; dir = "east";
steps = 0; steps = 0;
dist = 0;
turns = 0; turns = 0;
pos = key init.pos; pos = key init.pos;
}; };
@ -132,18 +133,18 @@
corners.${prev.pos} corners.${prev.pos}
|> mapAttrs (dir: edge: let |> mapAttrs (dir: edge: let
turns = if prev.dir == dir then prev.turns else prev.turns + 1; turns = if prev.dir == dir then prev.turns else prev.turns + 1;
existing = scores.${edge.pos} or null; existing = scores.${edge.pos} or null;
steps = prev.steps + edge.dist; steps = prev.steps + edge.dist;
# can't evaluate same turns
# because we don't know where we will turn next.
sameSteps = notNull existing sameSteps = notNull existing
&& existing.steps == steps && existing.steps == steps
# && turns == existing.turns
; ;
isBetter = isNull existing || isBetter = isNull existing ||
(existing.steps > steps); (existing.steps > steps);
newScore = { newScore = {
inherit steps dir turns; inherit steps dir turns;
inherit (edge) pos; inherit (edge) pos dist;
prev = prev.pos; prev = prev.pos;
}; };
in { in {
@ -177,30 +178,50 @@
# WIP: # WIP:
# go through the path, add each pos to visited # go through the path, add each pos to visited
# if point has alt, find points on that path not visited yet. # if point has alt,
# TODO: and the alt turns add up to the correct path.
# find points on that path.
# add length of filtered alt, (-1 for the overlap at end?). # add length of filtered alt, (-1 for the overlap at end?).
# add alt to visited. # add alt to visited.
pathToList = scores: pos: pathToList = scores: {pos ? key init.goal, visited ? []}:
let let
res = scores.${pos}; res = scores.${pos};
prevPath = if res ? prev then pathToList scores res.prev else []; prevPath = if res ? prev
fullaltPath = pathToList scores res.alt.prev; then pathToList scores {pos = res.prev; visited = visited;}
joinIndex = findFirstIndex ({pos,...}: !elem pos (map ({pos, ...}: pos) prevPath)) null fullaltPath; else {path = []; visited = [pos];};
altPath = lib.sublist (joinIndex - 1) (length fullaltPath) fullaltPath; a = pathToList scores {pos = res.alt.prev; visited = visited ++ prevPath.visited;};
altPath = if res ? alt
then {path = [thisAltSpot] ++ a.path; inherit (a) visited;}
else {path = []; visited = [];}
;
thisSpot = {inherit (res) pos steps turns dir; }; thisSpot = {inherit (res) pos steps turns dir dist;
alt = altPath.path;
};
thisAltSpot = {inherit (res.alt) pos steps turns dir dist;
alt = [];
};
in in {
prevPath visited = [pos] ++ prevPath.visited ++ altPath.visited;
++ path =
[(thisSpot // { if elem pos visited then [] else
alt = if res ? alt then altPath ++ [thisSpot] else null; [thisSpot]
})] ++
; prevPath.path
;
};
cornerScores = fastest init.goal {}; cornerScores = fastest init.goal {};
part1path = (pathToList cornerScores.scores (key init.goal)); addDist = foldl' (acc: {dist,alt,dir,...}:
acc + dist + (if addDist alt == 1 then 0 else (addDist alt) - 2)
) 1;
part1path = (pathToList cornerScores.scores {});
# not 520, too high
part2resultWrong = addDist part1path.path;
} }