Advent of code

 18 décembre 2022 

  Boiling Boulders : Calculer la surface totale, puis externe, de cubes agglutinés
4,8,5
   
10,14,18
   
8,17,6
   
15,14,15
   
8,15,3
   
    
  1. code.py
f = open("input.txt", 'r')
lines = [line[:-1] for line in f.readlines()]

cubes = []
for line in lines:
    cubes.append(tuple( map(int, line.split(','))))




### Première partie ###
nb = 0
for i in range(len(cubes) - 1):
    for j in range(i + 1, len(cubes)):
        if (cubes[i][0]-cubes[j][0])**2 + (cubes[i][1]-cubes[j][1])**2 + (cubes[i][2]-cubes[j][2])**2 == 1:
            nb += 1
print("Réponse partie 1:", 6*len(cubes) - 2*nb)



### Deuxième partie ###

DEPLACEMENTS = ((0,0,1), (0,0,-1), (0,1,0), (0,-1,0), (1,0,0), (-1,0,0))

# on soustrait/ajoute 1 pour avoir l'enveloppe d'air autour du cube maximal de lave
minx = (min(c[0] for c in cubes)) - 1
maxx = (max(c[0] for c in cubes)) + 1
miny = (min(c[1] for c in cubes)) - 1
maxy = (max(c[1] for c in cubes)) + 1
minz = (min(c[2] for c in cubes)) - 1
maxz = (max(c[2] for c in cubes)) + 1


# on part d'un point extérieur et on liste les blocs vides atteignables
cubesVidesExternes = [(minx, miny, minz)]

aFaire = cubesVidesExternes[:]
while len(aFaire) != 0:
    (x,y,z) = aFaire.pop()
    for (dx,dy,dz) in DEPLACEMENTS:
        nx = x+dx
        ny = y+dy
        nz = z+dz
        if nx < minx or nx > maxx or ny < miny or ny > maxy or nz < minz or nz > maxz:
            continue
        if (nx,ny,nz) in cubes or (nx,ny,nz) in cubesVidesExternes:
            continue
        cubesVidesExternes.append((nx,ny,nz))
        aFaire.append((nx,ny,nz))

print("Cubes vides externes calculés")


# On compte les faces touchant un cube vide externe
nb = 0
for (x,y,z) in cubes:
    for dep in DEPLACEMENTS:
        if (x+dep[0], y+dep[1], z+dep[2]) in cubesVidesExternes:
            nb += 1
print("Réponse partie 2:", nb)