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

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

18 Diciembre, 2022, 02:21 pm
Leído 890 veces

Eparoh

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

Los enunciados del día de hoy (Boiling Boulders) son:

Parte 1:
Spoiler
You and the elephants finally reach fresh air. You've emerged near the base of a large volcano that seems to be actively erupting! Fortunately, the lava seems to be flowing away from you and toward the ocean.

Bits of lava are still being ejected toward you, so you're sheltering in the cavern exit a little longer. Outside the cave, you can see the lava landing in a pond and hear it loudly hissing as it solidifies.

Depending on the specific compounds in the lava and speed at which it cools, it might be forming obsidian! The cooling rate should be based on the surface area of the lava droplets, so you take a quick scan of a droplet as it flies past you (your puzzle input).

Because of how quickly the lava is moving, the scan isn't very good; its resolution is quite low and, as a result, it approximates the shape of the lava droplet with 1x1x1 cubes on a 3D grid, each given as its x,y,z position.

To approximate the surface area, count the number of sides of each cube that are not immediately connected to another cube. So, if your scan were only two adjacent cubes like 1,1,1 and 2,1,1, each cube would have a single side covered and five sides exposed, a total surface area of 10 sides.

Here's a larger example:

2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5

In the above example, after counting up all the sides that aren't connected to another cube, the total surface area is 64.

What is the surface area of your scanned lava droplet?
[cerrar]
Parte 2:
Spoiler
Something seems off about your calculation. The cooling rate depends on exterior surface area, but your calculation also included the surface area of air pockets trapped in the lava droplet.

Instead, consider only cube sides that could be reached by the water and steam as the lava droplet tumbles into the pond. The steam will expand to reach as much as possible, completely displacing any air on the outside of the lava droplet but never expanding diagonally.

In the larger example above, exactly one cube of air is trapped within the lava droplet (at 2,2,5), so the exterior surface area of the lava droplet is 58.

What is the exterior surface area of your scanned lava droplet?
[cerrar]

Dejo mi código en Maple:

Código: [Seleccionar]
Datos:=proc(F)
   local L, i, L_aux, j, s;
      L:=[];
      for i from 1 to nops(F) do
         L_aux:=[];
         j:=1;
         s:="";
         while j <= length(F[i]) do
            if F[i][j] <> "," or j = nops(F[i]) then
               s:=cat(s,F[i][j]);
               j:=j+1;
            else
               j:=j+1;
               L_aux:=[op(L_aux),parse(s)+2];
               s:="";
            fi;
         od;
         L_aux:=[op(L_aux),parse(s)+2];
         L:=[op(L),L_aux];
      od;
   RETURN(L);
end:

F:=readdata(cat(currentdir(),"\\Template 18.txt"),string):
F:=Datos(F):

ConstruyeGrid:=proc(F)
   local N, d1, d2, d3, A, i;
      N:=nops(F);
      d1:=max([seq(F[i][1],i=1..N)]);
      d2:=max([seq(F[i][2],i=1..N)]);
      d3:=max([seq(F[i][3],i=1..N)]);
      A:=Array(1..d1+1,1..d2+1,1..d3+1);
      for i from 1 to N do
         A[F[i][1],F[i][2],F[i][3]]:=1;
      od;
   RETURN(A);
end:
CarasComun:=proc(A,pos)
   local T, S, i;
      T:=[[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]];
      S:=0;
         for i from 1 to 6 do
            if A[op(pos+T[i])] <> 0 then
               S:=S+1;
            fi;
         od;
   RETURN(S);
end:   
Superficie:=proc(F)
   local A, S, i;
      A:=ConstruyeGrid(F);
      S:=6*nops(F);
      for i from 1 to nops(F) do
         S:=S-CarasComun(A,F[i]);
      od;
   RETURN(S);
end:
Rellena:=proc(A,pos)
   local Size, A_aux, R;
      Size:=[upperbound(A)];
      A_aux:=A(..,..,..);
      A_aux[op(pos)]:=2;
      if pos[1]-1 > 0 and A_aux[op(pos+[-1,0,0])]<>1 and A_aux[op(pos+[-1,0,0])]<>2 then
         A_aux:=Rellena(A_aux,pos+[-1,0,0]);
      fi;
      if pos[1]+1 <= Size[1] and A_aux[op(pos+[1,0,0])]<>1 and A_aux[op(pos+[1,0,0])]<>2 then
         A_aux:=Rellena(A_aux,pos+[1,0,0]);
      fi;
      if pos[2]-1 > 0 and A_aux[op(pos+[0,-1,0])]<>1 and A_aux[op(pos+[0,-1,0])]<>2 then
         A_aux:=Rellena(A_aux,pos+[0,-1,0]);
      fi;
      if pos[2]+1 <= Size[2] and A_aux[op(pos+[0,1,0])]<>1 and A_aux[op(pos+[0,1,0])]<>2 then
         A_aux:=Rellena(A_aux,pos+[0,1,0]);
      fi;
      if pos[3]-1 > 0 and A_aux[op(pos+[0,0,-1])]<>1 and A_aux[op(pos+[0,0,-1])]<>2 then
         A_aux:=Rellena(A_aux,pos+[0,0,-1]);
      fi;
      if pos[3]+1 <= Size[3] and A_aux[op(pos+[0,0,1])]<>1 and A_aux[op(pos+[0,0,1])]<>2 then
         A_aux:=Rellena(A_aux,pos+[0,0,1]);
      fi;
   RETURN(A_aux);
end:
Atrapadas:=proc(A)
   local Size, Atrap, i, j, k;
      Size:=[upperbound(A)];
      Atrap:=[];
      for i from 3 to Size[1]-2 do
         for j from 3 to Size[2]-2 do
            for k from 3 to Size[3]-2 do
               if A[i,j,k] = 0 then
                  Atrap:=[op(Atrap),[i,j,k]];
               fi;
            od;
         od;
      od;
   RETURN(Atrap);
end:
Superficie2:=proc(F)
   local A, S, i, Atrap, x;
      A:=ConstruyeGrid(F);
      Atrap:=Atrapadas(Rellena(A,[1,1,1]));
      for x in Atrap do
         A[op(x)]:=1;
      od;
      S:=6*nops(F);
      for i from 1 to nops(F) do
         S:=S-CarasComun(A,F[i]);
      od;
   RETURN(S);
end:

Superficie(F);
Superficie2(F);

El de hoy no ha sido complicado, pero me ha gustado bastante la segunda parte. Aunque se podría optimizar bastante mi código tarda menos de 1.5s en ejecutar ambas partes y me conformo con ello  ::)

Un saludo.

19 Diciembre, 2022, 12:13 pm
Respuesta #1

w a y s

  • $$\Large \color{#5e8d56}\pi\,\pi\,\pi$$
  • Mensajes: 256
  • País: es
  • Karma: +0/-0
  • Sexo: Masculino
  • I
Hola.

He estado ocupado con problemas de clase y no he podido seguir resolviendo los problemas, de igual forma me entretengo viendo vuestras soluciones cuando las subís, a ver si conseguís acabarlo.  ;D

19 Diciembre, 2022, 01:41 pm
Respuesta #2

geómetracat

  • Moderador Global
  • Mensajes: 4,065
  • País: es
  • Karma: +0/-0
  • Sexo: Masculino
Yo ya me he desenganchado, me ha venido mucho trabajo y no tengo tiempo para dedicarlo a esto. El único que aguanta ahora es Eparoh. ¡Ánimo, Eparoh!
La ecuación más bonita de las matemáticas: \( d^2=0 \)

20 Diciembre, 2022, 06:24 pm
Respuesta #3

Eparoh

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

Aguanto, pero a trompicones, porque el de ayer tampoco fui capaz de resolverlo. Volvía a ser uno de árboles, parecido en mi opinión al que ya me derrotó, y de nuevo las estadísticas muestran que no solo a mi me resulta complicado ;D

Aún así, de nuevo espero que estos sean los únicos tropiezos hasta completar los cinco días que quedan, y para el próximo año ya sé que tengo que estudiarme en detalle  ::)

Un saludo.

23 Diciembre, 2022, 01:55 pm
Respuesta #4

Eparoh

  • $$\Large \color{#c88359}\pi\,\pi\,\pi\,\pi$$
  • Mensajes: 981
  • País: es
  • Karma: +0/-0
  • Sexo: Masculino
!No me he rendido!  :laugh:

Tengo casi terminado el de ayer (y también el de hoy), pero cuando intenté hacer el post por la noche el foro no me dejó. Tampoco me deja ahora, pero sigo luchando  :P

Un saludo.

23 Diciembre, 2022, 06:57 pm
Respuesta #5

geómetracat

  • Moderador Global
  • Mensajes: 4,065
  • País: es
  • Karma: +0/-0
  • Sexo: Masculino
¡Vamos Eparoh! Ya solo te quedan dos días. Además el último solo es una estrella.
La ecuación más bonita de las matemáticas: \( d^2=0 \)

23 Diciembre, 2022, 07:13 pm
Respuesta #6

manooooh

  • $$\Large \color{#9c57a6}\pi\,\pi\,\pi\,\pi\,\pi\,\pi$$
  • Mensajes: 4,791
  • País: ar
  • Karma: +1/-0
  • Sexo: Masculino
Hola

¡Sigue adelante, Eparoh! Tienes el apoyo de un experto... y de un novato también. :P

Saludos

23 Diciembre, 2022, 08:11 pm
Respuesta #7

Eparoh

  • $$\Large \color{#c88359}\pi\,\pi\,\pi\,\pi$$
  • Mensajes: 981
  • País: es
  • Karma: +0/-0
  • Sexo: Masculino
¡¡Gracias por los animos!!

La verdad es que lo estoy disfrutando, pero también tengo ganas de que acabe ya  :laugh:

En cuanto el foro me deje subiré las soluciones aunque sea ya por orgullo personal y completitud  :P

Un saludo