Autor Tema: Advent of code, solución día 17

0 Usuarios y 1 Visitante están viendo este tema.

18 Diciembre, 2022, 01:46 am
Leído 376 veces

Eparoh

  • $$\Large \color{#c88359}\pi\,\pi\,\pi\,\pi$$
  • Mensajes: 981
  • País: es
  • Karma: +0/-0
  • Sexo: Masculino
Hola a todos.

Después del borrado de los días 12, 13 y 14 por los problemas técnicos del foro y de la derrota ante el día 16 sufrida ayer, volvemos a la carga con el día 17  8^)

Los enunciados de este día 17 (Pyroclastic Flow) son:

Parte 1:
Spoiler
Your handheld device has located an alternative exit from the cave for you and the elephants. The ground is rumbling almost continuously now, but the strange valves bought you some time. It's definitely getting warmer in here, though.

The tunnels eventually open into a very tall, narrow chamber. Large, oddly-shaped rocks are falling into the chamber from above, presumably due to all the rumbling. If you can't work out where the rocks will fall next, you might be crushed!

The five types of rocks have the following peculiar shapes, where # is rock and . is empty space:

####

.#.
###
.#.

..#
..#
###

#
#
#
#

##
##

The rocks fall in the order shown above: first the - shape, then the + shape, and so on. Once the end of the list is reached, the same order repeats: the - shape falls first, sixth, 11th, 16th, etc.

The rocks don't spin, but they do get pushed around by jets of hot gas coming out of the walls themselves. A quick scan reveals the effect the jets of hot gas will have on the rocks as they fall (your puzzle input).

For example, suppose this was the jet pattern in your cave:

>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>

In jet patterns, < means a push to the left, while > means a push to the right. The pattern above means that the jets will push a falling rock right, then right, then right, then left, then left, then right, and so on. If the end of the list is reached, it repeats.

The tall, vertical chamber is exactly seven units wide. Each rock appears so that its left edge is two units away from the left wall and its bottom edge is three units above the highest rock in the room (or the floor, if there isn't one).

After a rock appears, it alternates between being pushed by a jet of hot gas one unit (in the direction indicated by the next symbol in the jet pattern) and then falling one unit down. If any movement would cause any part of the rock to move into the walls, floor, or a stopped rock, the movement instead does not occur. If a downward movement would have caused a falling rock to move into the floor or an already-fallen rock, the falling rock stops where it is (having landed on something) and a new rock immediately begins falling.

Drawing falling rocks with @ and stopped rocks with #, the jet pattern in the example above manifests as follows:

The first rock begins falling:
|..@@@@.|
|.......|
|.......|
|.......|
+-------+

Jet of gas pushes rock right:
|...@@@@|
|.......|
|.......|
|.......|
+-------+

Rock falls 1 unit:
|...@@@@|
|.......|
|.......|
+-------+

Jet of gas pushes rock right, but nothing happens:
|...@@@@|
|.......|
|.......|
+-------+

Rock falls 1 unit:
|...@@@@|
|.......|
+-------+

Jet of gas pushes rock right, but nothing happens:
|...@@@@|
|.......|
+-------+

Rock falls 1 unit:
|...@@@@|
+-------+

Jet of gas pushes rock left:
|..@@@@.|
+-------+

Rock falls 1 unit, causing it to come to rest:
|..####.|
+-------+

A new rock begins falling:
|...@...|
|..@@@..|
|...@...|
|.......|
|.......|
|.......|
|..####.|
+-------+

Jet of gas pushes rock left:
|..@....|
|.@@@...|
|..@....|
|.......|
|.......|
|.......|
|..####.|
+-------+

Rock falls 1 unit:
|..@....|
|.@@@...|
|..@....|
|.......|
|.......|
|..####.|
+-------+

Jet of gas pushes rock right:
|...@...|
|..@@@..|
|...@...|
|.......|
|.......|
|..####.|
+-------+

Rock falls 1 unit:
|...@...|
|..@@@..|
|...@...|
|.......|
|..####.|
+-------+

Jet of gas pushes rock left:
|..@....|
|.@@@...|
|..@....|
|.......|
|..####.|
+-------+

Rock falls 1 unit:
|..@....|
|.@@@...|
|..@....|
|..####.|
+-------+

Jet of gas pushes rock right:
|...@...|
|..@@@..|
|...@...|
|..####.|
+-------+

Rock falls 1 unit, causing it to come to rest:
|...#...|
|..###..|
|...#...|
|..####.|
+-------+

A new rock begins falling:
|....@..|
|....@..|
|..@@@..|
|.......|
|.......|
|.......|
|...#...|
|..###..|
|...#...|
|..####.|
+-------+

The moment each of the next few rocks begins falling, you would see this:

|..@....|
|..@....|
|..@....|
|..@....|
|.......|
|.......|
|.......|
|..#....|
|..#....|
|####...|
|..###..|
|...#...|
|..####.|
+-------+

|..@@...|
|..@@...|
|.......|
|.......|
|.......|
|....#..|
|..#.#..|
|..#.#..|
|#####..|
|..###..|
|...#...|
|..####.|
+-------+

|..@@@@.|
|.......|
|.......|
|.......|
|....##.|
|....##.|
|....#..|
|..#.#..|
|..#.#..|
|#####..|
|..###..|
|...#...|
|..####.|
+-------+

|...@...|
|..@@@..|
|...@...|
|.......|
|.......|
|.......|
|.####..|
|....##.|
|....##.|
|....#..|
|..#.#..|
|..#.#..|
|#####..|
|..###..|
|...#...|
|..####.|
+-------+

|....@..|
|....@..|
|..@@@..|
|.......|
|.......|
|.......|
|..#....|
|.###...|
|..#....|
|.####..|
|....##.|
|....##.|
|....#..|
|..#.#..|
|..#.#..|
|#####..|
|..###..|
|...#...|
|..####.|
+-------+

|..@....|
|..@....|
|..@....|
|..@....|
|.......|
|.......|
|.......|
|.....#.|
|.....#.|
|..####.|
|.###...|
|..#....|
|.####..|
|....##.|
|....##.|
|....#..|
|..#.#..|
|..#.#..|
|#####..|
|..###..|
|...#...|
|..####.|
+-------+

|..@@...|
|..@@...|
|.......|
|.......|
|.......|
|....#..|
|....#..|
|....##.|
|....##.|
|..####.|
|.###...|
|..#....|
|.####..|
|....##.|
|....##.|
|....#..|
|..#.#..|
|..#.#..|
|#####..|
|..###..|
|...#...|
|..####.|
+-------+

|..@@@@.|
|.......|
|.......|
|.......|
|....#..|
|....#..|
|....##.|
|##..##.|
|######.|
|.###...|
|..#....|
|.####..|
|....##.|
|....##.|
|....#..|
|..#.#..|
|..#.#..|
|#####..|
|..###..|
|...#...|
|..####.|
+-------+

To prove to the elephants your simulation is accurate, they want to know how tall the tower will get after 2022 rocks have stopped (but before the 2023rd rock begins falling). In this example, the tower of rocks will be 3068 units tall.

How many units tall will the tower of rocks be after 2022 rocks have stopped falling?
[cerrar]
Parte 2:
Spoiler
The elephants are not impressed by your simulation. They demand to know how tall the tower will be after 1000000000000 rocks have stopped! Only then will they feel confident enough to proceed through the cave.

In the example above, the tower would be 1514285714288 units tall!

How tall will the tower be after 1000000000000 rocks have stopped?
[cerrar]

Dejo mi solución en Maple:

Código: [Seleccionar]
F:=op(readdata(cat(currentdir(),"\\Template 17.txt"),string)):
Cham:=Array([seq([seq(0,j=1..9)],i=1..201)]):
Cham[201,..]:=8:
Cham[..,1]:=8:
Cham[..,9]:=8:

Amplia_Array:=proc(F)
   local fila, t;
      t:=Array([seq([8,seq(0,j=1..7),8],i=1..100)]);
      RETURN(ArrayTools[Concatenate](1,t,F));
end: 
Comprueba:=proc(Camara,tipo,pos)
   local comp, x;
      if tipo=1 then
         comp:=[seq(pos-[0,i],i=1..3),pos];
      elif tipo=2 then
         comp:=[seq(pos-[0,i],i=1..2),pos,pos-[1,1],pos+[1,-1]];
      elif tipo=3 then
         comp:=[seq(pos-[0,i],i=1..2),pos,seq(pos-[i,0],i=1..2)];
      elif tipo=4 then
         comp:=[seq(pos-[i,0],i=1..3),pos];
      else
         comp:=[pos-[0,1],pos,pos-[1,0],pos-[1,1]];
      fi;
      for x in comp do
         if Camara[op(x)]<>0 then
            RETURN(true);
         fi;
      od;
   RETURN(false);
end:
Dibuja:=proc(Camara,tipo,pos)
   local comp, x, Camara_aux;
      Camara_aux:=Camara;
      if tipo=1 then
         comp:=[seq(pos-[0,i],i=1..3),pos];
      elif tipo=2 then
         comp:=[seq(pos-[0,i],i=1..2),pos,pos-[1,1],pos+[1,-1]];
      elif tipo=3 then
         comp:=[seq(pos-[0,i],i=1..2),pos,seq(pos-[i,0],i=1..2)];
      elif tipo=4 then
         comp:=[seq(pos-[i,0],i=1..3),pos];
      else
         comp:=[pos-[0,1],pos,pos-[1,0],pos-[1,1]];
      fi;
      for x in comp do
         Camara_aux[op(x)]:=1;
      od;
   RETURN(Camara_aux);
end:
Start:=proc(Altura,tipo)
      if tipo=1 then
         RETURN([Altura-4,7]);
      elif tipo=2 then
         RETURN([Altura-5,6]);
      elif tipo=3 then
         RETURN([Altura-4,6]);
      elif tipo=4 then
         RETURN([Altura-4,4]);
      else
         RETURN([Altura-4,5]);
      fi;
end:
Height:=proc(Altura,tipo,pos)
      if tipo=1 then
         RETURN(Altura-pos[1]);
      elif tipo=2 or tipo=5 then
         RETURN(Altura-(pos[1]-1));
      elif tipo=3 then
         RETURN(Altura-(pos[1]-2));
      elif tipo=4 then
         RETURN(Altura-(pos[1]-3));
      fi;
end:
Mov:=proc(s)
   if s=">" then
      RETURN([0,1]);
   else
      RETURN([0,-1]);
   fi;
end:
Modulo:=proc(x,n)
   if x mod n = 0 then
      RETURN(n);
   else
      RETURN(x mod n);
   fi;
end:
Simulacion:=proc(F,Camara,n,h1:=0)
   local l, Chamber, h, h_aux, Dif, d, j ,U, Cam, cont, i, tipo, pos, Tot, F_aux, pasos_extra, resto;
      l:=length(F);
      Chamber:=Camara(1..,1..);
      h:=h1;
      j:=0;
      U:=[-1];
      Cam:=[Array([0,0])];
      Tot:=[];
      cont:=0;
      for i from 1 to n do
         tipo:=Modulo(i,5);
         pos:=Start(upperbound(Chamber)[1]-h,tipo);
         while 1=1 do
            j:=Modulo(j+1,l);
            if not Comprueba(Chamber,tipo,pos+Mov(F[j])) then
               pos:=pos+Mov(F[j]);
            fi;
            if Comprueba(Chamber,tipo,pos+[1,0]) then
               Chamber:=Dibuja(Chamber,tipo,pos);
               if Height(upperbound(Chamber)[1],tipo,pos) > h then
                  h:=Height(upperbound(Chamber)[1],tipo,pos);
               fi;
               break;
            else
               pos:=pos+[1,0];
            fi;
         od;
         if upperbound(Chamber)[1]-h<15 then
            Chamber:=Amplia_Array(Chamber);
         fi;
         if h>2000 and (i mod 100) = 0 then
            if j=U[1] and ArrayTools[IsEqual](Chamber(upperbound(Chamber)[1]-h..upperbound(Chamber)[1]-(h-99),..),
                                              Cam[1]) then
               h_aux:=[op(h_aux),h];
               Dif:=[seq(h_aux[i+1]-h_aux[i],i=1..nops(h_aux)-1)];
               d:=floor((n-i)/100);
               resto:=d mod nops(Dif);
               h:=h+(floor(d/nops(Dif)))*add(x, x in Dif)+add(x, x in [seq(Dif[i],i=1..resto)]);
               pasos_extra:=n-(i+100*d);
               if pasos_extra > 0 then
                  U:=[op(U),U[1]];
                  Tot:=[op(Tot),Tot[1]];
                  F_aux:=cat(F,F);
                  F_aux:=F_aux[U[resto+1]+1..l+U[resto+1]];
                  RETURN(h+Simulacion(F_aux,Tot[resto+1],pasos_extra,h_aux[resto+1])-h_aux[resto+1]);
               else
                  RETURN(h);
               fi;
            fi;
            if cont=0 then
               U:=[j];
               Cam:=[Chamber(upperbound(Chamber)[1]-h..upperbound(Chamber)[1]-(h-99),..)];
               h_aux:=[h];
               Tot:=[Chamber(1..,1..)];
               cont:=1;
            else
               U:=[op(U),j];
               Cam:=[op(Cam),Chamber(upperbound(Chamber)[1]-h..upperbound(Chamber)[1]-(h-99),..)];
               h_aux:=[op(h_aux),h];
               Tot:=[op(Tot),Chamber(1..,1..)];
            fi;
         fi;
      od;
   RETURN(h);   
end:

Simulacion(F,Cham,2022);
Simulacion(F,Cham,1000000000000);

Aunque no es el código más bonito del mundo creo que la idea es sencilla (aunque me costó verla), pues al final lo que hago es buscar un punto a partir del cual el patrón de rocas comience a repetirse para ya poder calcular cualquier número por grande que sea desde dicho patrón. Además, es relativamente rápido pues corre ambas partes en unos 2.4s. Así que, al igual que ayer me sentí derrotado e impotente de no poder hacer nada, hoy estoy contento y me ha gustado mucho el problema, pues la primera parte es una simulación bonita de un "tetris" y la segunda te obliga a ser ingenioso para tratar con un número tan monstruoso  ;D

Un saludo.

PD: Se que técnicamente me he saltado un día y hoy lo posteo pasado también el día, pero aunque lo tenía hecho desde esta mañana no he podido publicarlo hasta ahora aquí porque me pasé el día fuera y, aunque fallara ayer, quiero seguir intentando completar el resto de días en sus tiempos ::)