AdventOfCode/2022/day18.py

60 lines
1.6 KiB
Python
Raw Normal View History

2022-12-19 00:09:24 +10:30
from helpers import *
from skimage.morphology import flood
coords = [tuple(line_to_numbers(line)) for line in read_day(18).split('\n')]
sample_coords = [tuple(line_to_numbers(line)) for line in '''
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
'''.strip().split('\n')]
def count_faces(coords: list[tuple[int,int,int]]):
# print(coords)
surface_area = 0
for (x,y,z) in coords:
if (x+1,y,z) not in coords:
surface_area += 1
if (x-1,y,z) not in coords:
surface_area += 1
if (x,y+1,z) not in coords:
surface_area += 1
if (x,y-1,z) not in coords:
surface_area += 1
if (x,y,z+1) not in coords:
surface_area += 1
if (x,y,z-1) not in coords:
surface_area += 1
return surface_area
print(f'Part 1 (sample): {count_faces(sample_coords)}')
print(f'Part 1: {count_faces(coords)}')
def count_faces_2(coords: list[tuple[int,int,int]]):
# we pad to ensure the outer edges are fine
field = np.zeros((25,25,25), dtype=np.int8)
for (x,y,z) in coords:
field[x+1,y+1,z+1] = 1
field[flood(field, (0,0,0), connectivity=1)] = -1
2022-12-19 01:03:21 +10:30
surface_area = np.logical_and(field[:-1,:,:] == 1, field[1:,:,:] == -1).sum()
surface_area += np.logical_and(field[1:,:,:] == 1, field[:-1,:,:] == -1).sum()
surface_area += np.logical_and(field[:,:-1,:] == 1, field[:,1:,:] == -1).sum()
surface_area += np.logical_and(field[:,1:,:] == 1, field[:,:-1,:] == -1).sum()
surface_area += np.logical_and(field[:,:,:-1] == 1, field[:,:,1:] == -1).sum()
surface_area += np.logical_and(field[:,:,1:] == 1, field[:,:,:-1] == -1).sum()
2022-12-19 00:09:24 +10:30
return surface_area
print(f'Part 2 (sample): {count_faces_2(sample_coords)}')
print(f'Part 2: {count_faces_2(coords)}')