Hola a todos

Los enunciados del día 24 (Blizzard Basin) son:
Parte 1:
Spoiler
With everything replanted for next year (and with elephants and monkeys to tend the grove), you and the Elves leave for the extraction point.
Partway up the mountain that shields the grove is a flat, open area that serves as the extraction point. It's a bit of a climb, but nothing the expedition can't handle.
At least, that would normally be true; now that the mountain is covered in snow, things have become more difficult than the Elves are used to.
As the expedition reaches a valley that must be traversed to reach the extraction site, you find that strong, turbulent winds are pushing small blizzards of snow and sharp ice around the valley. It's a good thing everyone packed warm clothes! To make it across safely, you'll need to find a way to avoid them.
Fortunately, it's easy to see all of this from the entrance to the valley, so you make a map of the valley and the blizzards (your puzzle input). For example:
#.#####
#.....#
#>....#
#.....#
#...v.#
#.....#
#####.#
The walls of the valley are drawn as #; everything else is ground. Clear ground - where there is currently no blizzard - is drawn as .. Otherwise, blizzards are drawn with an arrow indicating their direction of motion: up (^), down (v), left (<), or right (>).
The above map includes two blizzards, one moving right (>) and one moving down (v). In one minute, each blizzard moves one position in the direction it is pointing:
#.#####
#.....#
#.>...#
#.....#
#.....#
#...v.#
#####.#
Due to conservation of blizzard energy, as a blizzard reaches the wall of the valley, a new blizzard forms on the opposite side of the valley moving in the same direction. After another minute, the bottom downward-moving blizzard has been replaced with a new downward-moving blizzard at the top of the valley instead:
#.#####
#...v.#
#..>..#
#.....#
#.....#
#.....#
#####.#
Because blizzards are made of tiny snowflakes, they pass right through each other. After another minute, both blizzards temporarily occupy the same position, marked 2:
#.#####
#.....#
#...2.#
#.....#
#.....#
#.....#
#####.#
After another minute, the situation resolves itself, giving each blizzard back its personal space:
#.#####
#.....#
#....>#
#...v.#
#.....#
#.....#
#####.#
Finally, after yet another minute, the rightward-facing blizzard on the right is replaced with a new one on the left facing the same direction:
#.#####
#.....#
#>....#
#.....#
#...v.#
#.....#
#####.#
This process repeats at least as long as you are observing it, but probably forever.
Here is a more complex example:
#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#
Your expedition begins in the only non-wall position in the top row and needs to reach the only non-wall position in the bottom row. On each minute, you can move up, down, left, or right, or you can wait in place. You and the blizzards act simultaneously, and you cannot share a position with a blizzard.
In the above example, the fastest way to reach your goal requires 18 steps. Drawing the position of the expedition as E, one way to achieve this is:
Initial state:
#E######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#
Minute 1, move down:
#.######
#E>3.<.#
#<..<<.#
#>2.22.#
#>v..^<#
######.#
Minute 2, move down:
#.######
#.2>2..#
#E^22^<#
#.>2.^>#
#.>..<.#
######.#
Minute 3, wait:
#.######
#<^<22.#
#E2<.2.#
#><2>..#
#..><..#
######.#
Minute 4, move up:
#.######
#E<..22#
#<<.<..#
#<2.>>.#
#.^22^.#
######.#
Minute 5, move right:
#.######
#2Ev.<>#
#<.<..<#
#.^>^22#
#.2..2.#
######.#
Minute 6, move right:
#.######
#>2E<.<#
#.2v^2<#
#>..>2>#
#<....>#
######.#
Minute 7, move down:
#.######
#.22^2.#
#<vE<2.#
#>>v<>.#
#>....<#
######.#
Minute 8, move left:
#.######
#.<>2^.#
#.E<<.<#
#.22..>#
#.2v^2.#
######.#
Minute 9, move up:
#.######
#<E2>>.#
#.<<.<.#
#>2>2^.#
#.v><^.#
######.#
Minute 10, move right:
#.######
#.2E.>2#
#<2v2^.#
#<>.>2.#
#..<>..#
######.#
Minute 11, wait:
#.######
#2^E^2>#
#<v<.^<#
#..2.>2#
#.<..>.#
######.#
Minute 12, move down:
#.######
#>>.<^<#
#.<E.<<#
#>v.><>#
#<^v^^>#
######.#
Minute 13, move down:
#.######
#.>3.<.#
#<..<<.#
#>2E22.#
#>v..^<#
######.#
Minute 14, move right:
#.######
#.2>2..#
#.^22^<#
#.>2E^>#
#.>..<.#
######.#
Minute 15, move right:
#.######
#<^<22.#
#.2<.2.#
#><2>E.#
#..><..#
######.#
Minute 16, move right:
#.######
#.<..22#
#<<.<..#
#<2.>>E#
#.^22^.#
######.#
Minute 17, move down:
#.######
#2.v.<>#
#<.<..<#
#.^>^22#
#.2..2E#
######.#
Minute 18, move down:
#.######
#>2.<.<#
#.2v^2<#
#>..>2>#
#<....>#
######E#
What is the fewest number of minutes required to avoid the blizzards and reach the goal?
Parte 2:
Spoiler
As the expedition reaches the far side of the valley, one of the Elves looks especially dismayed:
He forgot his snacks at the entrance to the valley!
Since you're so good at dodging blizzards, the Elves humbly request that you go back for his snacks. From the same initial conditions, how quickly can you make it from the start to the goal, then back to the start, then back to the goal?
In the above example, the first trip to the goal takes 18 minutes, the trip back to the start takes 23 minutes, and the trip back to the goal again takes 13 minutes, for a total time of 54 minutes.
What is the fewest number of minutes required to reach the goal, go back to the start, then reach the goal again?
Dejo aquí mi ALTAMENTE ineficiente código en Maple:
F:=readdata(cat(currentdir(),"\\Template 24.txt"),string):
F:=Array([seq([seq(F[i][j],j=1..length(F[i]))],i=1..nops(F))]):
F:=ArrayTools[Concatenate](1,Vector[row]([seq("#",i=1..upperbound(F)[2])]),
F,Vector[row]([seq("#",i=1..upperbound(F)[2])])):
Start:=[2,ListTools[Search](".",convert(F(2,..),list))]:
Exit:=[upperbound(F)[1]-1,ListTools[Search](".",convert(F(-2,..),list))]:
Modulo:=proc(x,n)
if x mod n = 0 then
RETURN(n);
else
RETURN(x mod n);
fi;
end:
Mov_Snow:=proc(F,n)
local N, R, i, j, R_aux;
N:=upperbound(F);
R:=Array([seq([seq(".",j=1..N[2]-2)],i=1..N[1]-4)]);
for i from 3 to N[1]-2 do
for j from 2 to N[2]-1 do
if F[i,j] = ">" then
if R[i-2,Modulo(j-1+n,N[2]-2)] = "." then
R[i-2,Modulo(j-1+n,N[2]-2)]:=">";
else
R[i-2,Modulo(j-1+n,N[2]-2)]:=cat(R[i-2,Modulo(j-1+n,N[2]-2)],">");
fi;
elif F[i,j] = "<" then
if R[i-2,Modulo(j-1-n,N[2]-2)] = "." then
R[i-2,Modulo(j-1-n,N[2]-2)]:="<";
else
R[i-2,Modulo(j-1-n,N[2]-2)]:=cat(R[i-2,Modulo(j-1-n,N[2]-2)],"<");
fi;
elif F[i,j] = "^" then
if R[Modulo(i-2-n,N[1]-4),j-1] = "." then
R[Modulo(i-2-n,N[1]-4),j-1]:="^";
else
R[Modulo(i-2-n,N[1]-4),j-1]:=cat(R[Modulo(i-2-n,N[1]-4),j-1],"^");
fi;
elif F[i,j] = "v" then
if R[Modulo(i-2+n,N[1]-4),j-1] = "." then
R[Modulo(i-2+n,N[1]-4),j-1]:="v";
else
R[Modulo(i-2+n,N[1]-4),j-1]:=cat(R[Modulo(i-2+n,N[1]-4),j-1],"v");
fi;
fi;
od;
od;
R_aux:=F(..,..);
R_aux[3..N[1]-2,2..N[2]-1]:=R;
RETURN(R_aux);
end:
Menor_camino:=proc(F,s,e,pasos:=0)
local Rep, visitados, steps, mov, mov_aux, direcciones, F_snow, p, j;
Rep:=lcm(upperbound(F)[1]-3,upperbound(F)[2]-2);
visitados:={};
mov:=[s];
mov_aux:={};
direcciones:=[[0,0],[1,0],[-1,0],[0,1],[0,-1]];
steps:=pasos;
while 1=1 do
F_snow:=Mov_Snow(F,steps+1);
for p in mov do
if member([p,steps mod Rep],visitados) then
next;
else
visitados:=visitados union {[p,steps mod Rep]};
fi;
if p = e then
RETURN(steps);
fi;
for j from 1 to 5 do
if F_snow(op(p+direcciones[j])) = "." then
mov_aux:=mov_aux union {p+direcciones[j]};
fi;
od;
od;
mov:=mov_aux;
mov_aux:={};
steps:=steps+1;
od;
end:
Ida:=Menor_camino(F,Start,Exit);
Vuelta:=Menor_camino(F,Exit,Start,Ida):
ReIda:=Menor_camino(F,Start,Exit,Vuelta);
Otro problema de árboles, ¡pero está vez no me derrotó!

Aunque... Tarda unos 40 minutos en ejecutar ambas partes

Realmente estoy orgulloso del código (de la última parte, la simulación del movimiento de la tormenta de nieve no es nada bonita), porque tras dos derrotas con problemas de árboles he podido conseguir este último. Además, sinceramente no se como se podría optimizar más el código sin utilizar técnicas que involucren procesos más avanzados que utilicen memoria caché y esas cosas. Que se que existen, pero desconozco totalmente como funcionan o se puedan implementar. Así que, aun con 40 minutos de espera estoy contento y con fuerzas para afrontar mañana el último.
Un saludo.