aoc/2024/06/solution.nix

279 lines
7.7 KiB
Nix
Raw Permalink Normal View History

2024-12-06 07:13:59 +00:00
{lib, ...}: input: rec {
content = lib.trim input;
2024-12-06 15:12:28 +00:00
chart = toChart content;
toChart = s: s
2024-12-06 07:13:59 +00:00
|> lib.strings.splitString "\n"
2024-12-06 15:12:28 +00:00
|> map (lib.stringToCharacters)
2024-12-06 07:13:59 +00:00
;
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;
2024-12-06 15:12:28 +00:00
getChar = {x, y}: chart: chart
2024-12-06 07:13:59 +00:00
|> index y
|> index x
;
height = builtins.length chart;
width = chart |> index 0 |> builtins.length;
2024-12-06 15:12:28 +00:00
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;
2024-12-06 07:13:59 +00:00
newPos = addVec pos newDir;
2024-12-06 15:12:28 +00:00
colliding = nextChar == "#";
nextChar = getChar fwd charts;
2024-12-06 07:13:59 +00:00
fwd = addVec pos dir;
2024-12-06 15:12:28 +00:00
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;};
2024-12-06 07:13:59 +00:00
in
{
2024-12-06 15:12:28 +00:00
visited = visited // {${hash} = visited.${hash} or [] ++ [dir];};
pos = if colliding then pos else newPos;
2024-12-06 07:13:59 +00:00
dir = newDir;
2024-12-06 15:12:28 +00:00
inherit exited charts;
obs = obs ++ (if canObstruct then [(posToString fwd)] else []);
2024-12-06 07:13:59 +00:00
};
rotate = {x, y}: {x = -y; y = x;};
startingDir = {x = 0; y = -1;};
2024-12-06 15:12:28 +00:00
getVisits = {pos ? startingPos
, dir ? startingDir
, visited ? {}
, obs ? []
, charts ? chart
, ...}:
2024-12-06 07:13:59 +00:00
let
2024-12-06 15:12:28 +00:00
nextStep = progress {inherit pos dir visited obs charts;};
in if nextStep.exited then nextStep else getVisits nextStep;
path = getVisits {};
2024-12-06 07:13:59 +00:00
2024-12-06 15:12:28 +00:00
part1result = path.visited
2024-12-06 07:13:59 +00:00
|> builtins.attrNames
|> builtins.length
;
2024-12-06 15:12:28 +00:00
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;
2024-12-07 17:56:20 +00:00
# FASTER!
startingObsMap = getObsMap chart;
2024-12-08 13:08:57 +00:00
getObsMap = chart: chart
2024-12-07 17:56:20 +00:00
|> lib.imap0 (y: line: line
2024-12-08 13:08:57 +00:00
|> lib.imap0 (x: char: if char != "#" then null else makeObs chart (b: b == "#") {inherit x y;})
2024-12-07 17:56:20 +00:00
)
;
2024-12-08 13:08:57 +00:00
makeObs = chart: matcher: {x, y}: {
right = if y + 1 >= height || x + 1 >= width then null else
searchRight matcher {x = x + 1; y = y + 1;} chart;
2024-12-07 17:56:20 +00:00
2024-12-08 13:08:57 +00:00
left = if y < 1 || x <= 0 then null else
searchLeft matcher {x = x - 1; y = y - 1;} chart;
down = if x <= 0 || y + 1 >= width then null else
searchDown matcher {x = x - 1; y = y + 1;} chart;
up = if x + 1 >= width then null else
searchUp matcher {x = x + 1; y = y - 1;} chart;
};
maybe = f: a: b: if !(builtins.isInt a && builtins.isInt b) then null else f a b;
maybeAdd = maybe builtins.add;
maybeSub = maybe builtins.sub;
column = x: map (index x);
searchLeft = matcher: {x, y}: chart: chart
|> index y
|> lib.sublist 0 (x + 1)
|> lib.reverseList
|> lib.lists.findFirstIndex matcher null
|> maybeSub x
;
searchDown = matcher: {x, y}: chart: chart
|> lib.sublist y height
|> column x
|> lib.lists.findFirstIndex matcher null
|> maybeAdd y
;
searchRight = matcher: {x, y}: chart: chart
|> index y
|> lib.sublist x width
|> lib.lists.findFirstIndex matcher null
|> maybeAdd x
;
2024-12-07 17:56:20 +00:00
searchUp = matcher: {x, y}: chart: chart
|> lib.sublist 0 (y + 1)
2024-12-08 13:08:57 +00:00
|> column x
2024-12-07 17:56:20 +00:00
|> lib.reverseList
|> lib.lists.findFirstIndex matcher null
2024-12-08 13:08:57 +00:00
|> maybeSub y
2024-12-07 17:56:20 +00:00
;
firstOb = {
y = searchUp (i: !isNull i) startingPos startingObsMap;
x = startingPos.x;
};
index2d = {x, y}: m: m |> index y |> index x;
lookRight = obsMap: prev: {
x = (index2d prev obsMap).right;
y = prev.y + 1;
};
lookDown = obsMap: prev: {
x = prev.x - 1;
y = (index2d prev obsMap).down;
};
lookLeft = obsMap: prev: {
x = (index2d prev obsMap).left;
y = prev.y - 1;
};
lookUp = obsMap: prev: {
x = prev.x + 1;
y = (index2d prev obsMap).up;
};
getNextDir = i: [lookRight lookDown lookLeft lookUp]
|> index (lib.mod i 4)
;
getFastpath = {i ? 0, pos ? firstOb, obsMap ? startingObsMap, hist ? []}: let
nextPos = (getNextDir i) obsMap pos;
newHist = hist ++ [point];
point = pos // {dir = lib.mod i 4;};
looped = lib.elem point hist;
in if isNull pos.x || isNull pos.y || looped
then {path = hist ++ [pos]; inherit looped;}
else getFastpath {i = i + 1; pos = nextPos; hist = newHist; inherit obsMap;}
;
fastpath = getFastpath {};
allObs = builtins.genList (i: {
x = lib.mod i width;
y = i / height;
}) (height * width)
2024-12-08 13:08:57 +00:00
|> builtins.filter (pos: index2d pos startingObsMap |> isNull)
|> map (pos: insert pos startingObsMap)
2024-12-07 17:56:20 +00:00
;
2024-12-08 13:08:57 +00:00
replaceColumn = y: lib.zipListsWith (row: new:
replace y row new
);
insert = {x, y}@pos: chart: let
newObs = makeObs chart builtins.isAttrs pos;
# nextL = searchRight
in chart
|> replace2d pos newObs
# update obstacles on rows above and below
|> (if y - 1 < 0 then lib.id else nChart: replace (y - 1) (
nChart |> index (y - 1)
|> lib.imap0 (x: b: if isNull b then null else
makeObs nChart builtins.isAttrs {inherit x; y = y - 1;})
# |> map (o: if builtins.isAttrs o && o.right > x then
# o // {right = x;} else o)
) nChart)
|> (if y + 1 >= width then lib.id else nChart: replace (y + 1) (
nChart |> index (y + 1)
|> lib.imap0 (x: b: if isNull b then null else
makeObs nChart builtins.isAttrs {inherit x; y = y + 1;})
) nChart)
|> (if x - 1 < 0 then lib.id else nChart:
replaceColumn (x - 1) (column (x - 1) nChart
|> lib.imap0 (y: b: if isNull b then null else
makeObs nChart builtins.isAttrs {inherit y; x = x - 1;})
) nChart)
|> (if x + 1 >= width then lib.id else nChart:
replaceColumn (x + 1) (column (x + 1) nChart
|> lib.imap0 (y: b: if isNull b then null else
makeObs nChart builtins.isAttrs {inherit y; x = x + 1;})
) nChart)
;
2024-12-07 17:56:20 +00:00
2024-12-08 13:08:57 +00:00
loopyMaps = allObs
2024-12-07 17:56:20 +00:00
|> builtins.filter (m: (getFastpath {obsMap = m;}).looped);
part2faster = builtins.length loopyMaps;
2024-12-06 07:13:59 +00:00
}