You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.9 KiB
97 lines
2.9 KiB
2 years ago
|
#reading input
|
||
|
with open('input18.txt','r') as f:
|
||
|
inp = f.read().splitlines(keepends=False)
|
||
|
|
||
|
cubes = []
|
||
|
sides = {}
|
||
|
surface = 0
|
||
|
maxX = None
|
||
|
maxY = None
|
||
|
maxZ = None
|
||
|
minX = None
|
||
|
minY = None
|
||
|
minZ = None
|
||
|
bubbles = []
|
||
|
bubbleSides = {}
|
||
|
insideSurface = 0
|
||
|
|
||
|
|
||
|
for line in inp:
|
||
|
x, y, z = line.split(',')
|
||
|
x = int(x)
|
||
|
y = int(y)
|
||
|
z = int(z)
|
||
|
if maxX == None or x > maxX:
|
||
|
maxX = x
|
||
|
if minX == None or x < minX:
|
||
|
minX = x
|
||
|
if maxY == None or y > maxY:
|
||
|
maxY = y
|
||
|
if minY == None or y < minY:
|
||
|
minY = y
|
||
|
if maxZ == None or z > maxZ:
|
||
|
maxZ = z
|
||
|
if minZ == None or z < minZ:
|
||
|
minZ = z
|
||
|
cubes.append((x, y, z))
|
||
|
|
||
|
|
||
|
#main function
|
||
|
for cube in cubes:
|
||
|
x, y, z = cube
|
||
|
sides[((x - 1, x),(y, y),(z, z))] = sides.get(((x - 1, x),(y, y),(z, z)), 0) + 1
|
||
|
sides[((x, x + 1),(y, y),(z, z))] = sides.get(((x, x + 1),(y, y),(z, z)), 0) + 1
|
||
|
sides[((x, x),(y - 1, y),(z, z))] = sides.get(((x, x),(y - 1, y),(z, z)), 0) + 1
|
||
|
sides[((x, x),(y, y + 1),(z, z))] = sides.get(((x, x),(y, y + 1),(z, z)), 0) + 1
|
||
|
sides[((x, x),(y, y),(z - 1, z))] = sides.get(((x, x),(y, y),(z - 1, z)), 0) + 1
|
||
|
sides[((x, x),(y, y),(z, z + 1))] = sides.get(((x, x),(y, y),(z, z + 1)), 0) + 1
|
||
|
|
||
|
for side in sides:
|
||
|
if sides[side] == 1:
|
||
|
surface += 1
|
||
|
|
||
|
for x in range(minX, maxX + 1):
|
||
|
for y in range(minY, maxY + 1):
|
||
|
for z in range(minZ, maxZ + 1):
|
||
|
bubbles.append((x, y, z))
|
||
|
|
||
|
for cube in cubes:
|
||
|
if cube in bubbles:
|
||
|
bubbles.remove(cube)
|
||
|
outsideBubbles = []
|
||
|
for bubble in bubbles:
|
||
|
x, y, z = bubble
|
||
|
if x in [minX, maxX] or y in [minY, maxY] or z in [minZ, maxZ]:
|
||
|
outsideBubbles.append((x, y, z))
|
||
|
|
||
|
done = False
|
||
|
while not done:
|
||
|
done = True
|
||
|
for bubble in outsideBubbles:
|
||
|
bubbleSides.setdefault(bubble, 1)
|
||
|
for bubble in bubbles:
|
||
|
x, y, z = bubble
|
||
|
if any(i in bubbleSides for i in((x - 1, y, z), (x + 1, y, z), (x, y - 1, z), (x, y + 1, z), (x, y, z - 1), (x, y, z + 1))):
|
||
|
if (x, y, z) not in outsideBubbles:
|
||
|
outsideBubbles.append((x, y, z))
|
||
|
done = False
|
||
|
|
||
|
for bubble in outsideBubbles:
|
||
|
if bubble in bubbles:
|
||
|
bubbles.remove(bubble)
|
||
|
|
||
|
bubbleSides = {}
|
||
|
for bubble in bubbles:
|
||
|
x, y, z = bubble
|
||
|
bubbleSides[((x - 1, x),(y, y),(z, z))] = bubbleSides.get(((x - 1, x),(y, y),(z, z)), 0) + 1
|
||
|
bubbleSides[((x, x + 1),(y, y),(z, z))] = bubbleSides.get(((x, x + 1),(y, y),(z, z)), 0) + 1
|
||
|
bubbleSides[((x, x),(y - 1, y),(z, z))] = bubbleSides.get(((x, x),(y - 1, y),(z, z)), 0) + 1
|
||
|
bubbleSides[((x, x),(y, y + 1),(z, z))] = bubbleSides.get(((x, x),(y, y + 1),(z, z)), 0) + 1
|
||
|
bubbleSides[((x, x),(y, y),(z - 1, z))] = bubbleSides.get(((x, x),(y, y),(z - 1, z)), 0) + 1
|
||
|
bubbleSides[((x, x),(y, y),(z, z + 1))] = bubbleSides.get(((x, x),(y, y),(z, z + 1)), 0) + 1
|
||
|
|
||
|
for side in bubbleSides:
|
||
|
if bubbleSides[side] == 1:
|
||
|
insideSurface += 1
|
||
|
|
||
|
print(surface - insideSurface)
|