Advent of Code 2022 Day 18

Author

Nathan Moore

— Day 18: Boiling Boulders —

The volcano is spitting out lava cubes and your scan (at very low resolution) tracks the size of these cubes.

What is the surface area of your scanned lava droplet?

import numpy as np

with open('data-2022-18.txt', 'r') as f:
    inp = f.read().splitlines()

I prefer these ones that have a distinct answer (probably).

cubes = [list(map(int, x.split(','))) for x in inp]

cubes[0]

min(cubes)
max(cubes)

cc = np.zeros((22,22,22)) # position of cubes
dd = np.zeros((22,22,22)) # count of sides at each position

for w in cubes: 
    x,y,z = w
    cc[x,y,z] = 1


def assess_cube(x,y,z):
    s = 0
    # x negative
    if x == 0: 
        s += 1
    else: 
        if cc[x-1,y,z] == 0:
            s += 1
    # x positive
    if x == 21: 
        s += 1        
    else: 
        if cc[x+1,y,z] == 0:
            s += 1
    # y negative
    if y == 0:
        s += 1
    else: 
        if cc[x,y-1,z] == 0:
            s += 1
    # y positive
    if y == 21: 
        s += 1
    else:
        if cc[x,y+1,z] == 0:
            s += 1
    # z negative
    if z == 0:
        s += 1
    else: 
        if cc[x,y,z-1] == 0:
            s += 1
    # z positive
    if z == 21:
        s += 1
    else:
        if cc[x,y,z+1] == 0:
            s += 1
    return s

    
for x in range(22): 
    for y in range(22):
        for z in range(22): 
            if cc[x,y,z] == 1:
                dd[x,y,z] = assess_cube(x,y,z)

np.sum(dd)
np.float64(4302.0)

— Part Two —

There are pockets of air which were counted in part one, but we don’t want to count them.

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

cubes = [list(map(int, x.split(','))) for x in inp]
cc = np.zeros((22,22,22)) # position of cubes
dd = np.zeros((22,22,22)) # count of sides at each position

for w in cubes: 
    x,y,z = w
    cc[x,y,z] = 1

def pockets(x,y,z):
    if (
      sum(cc[0:(x-1) ,y,     z     ]) >= 1 and 
      sum(cc[(x+1):22,y,     z     ]) >= 1 and 
      sum(cc[x,     0:(y-1), z     ]) >= 1 and 
      sum(cc[x,     (y+1):22,z     ]) >= 1 and 
      sum(cc[x,     y,     0:(z-1) ]) >= 1 and 
      sum(cc[x,     y,     (z+1):22]) >= 1):
        return 1
    else:
        return 0

# change the cube array 
for x in range(22): 
    for y in range(22):
        for z in range(22): 
            if cc[x,y,z] == 0:
                cc[x,y,z] = pockets(x,y,z)


for x in range(22): 
    for y in range(22):
        for z in range(22): 
            if cc[x,y,z] == 1:
                dd[x,y,z] = assess_cube(x,y,z)
                
np.sum(dd)

# 2594 too high
np.float64(2594.0)