Skip to content

Instantly share code, notes, and snippets.

@yuesha-yc
Last active November 3, 2022 00:25
Show Gist options
  • Save yuesha-yc/58524b57aa5462ec6506f3bd593dd3f6 to your computer and use it in GitHub Desktop.
Save yuesha-yc/58524b57aa5462ec6506f3bd593dd3f6 to your computer and use it in GitHub Desktop.
import numpy as np
# Use recursion to check insulation
def insulate(space, pos):
# pos already discovered, skip
if space[pos] == 2:
pass
# pos is on boundary
if pos[0] <= 0 or pos[0] >= space.shape[0]-1 \
or pos[1] <= 0 or pos[1] >= space.shape[1]-1 \
or pos[2] <= 0 or pos[2] >= space.shape[2]-1:
# if pos is 0 (air), implies air path to boundary, hence not insulated
if space[pos] == 0:
return 1
# if pos is 1 (insulation), implies no air path to boundary, hence insulated
else:
return 0
# pos not discovered, mark as discovered
space[pos] = 2
# check pos neighbors
pos1 = pos[0], pos[1], pos[2] + 1
pos2 = pos[0], pos[1], pos[2] - 1
pos3 = pos[0], pos[1] + 1, pos[2]
pos4 = pos[0], pos[1] - 1, pos[2]
pos5 = pos[0] + 1, pos[1], pos[2]
pos6 = pos[0] - 1, pos[1], pos[2]
# if neighbor is 0 (air), recurse
result = 0
if space[pos1] == 0:
result += insulate(space, pos1)
if space[pos2] == 0:
result += insulate(space, pos2)
if space[pos3] == 0:
result += insulate(space, pos3)
if space[pos4] == 0:
result += insulate(space, pos4)
if space[pos5] == 0:
result += insulate(space, pos5)
if space[pos6] == 0:
result += insulate(space, pos6)
return result
def test(space, start=(4, 4, 4)):
space[start] = 0 # center
if insulate(space, (4, 4, 4)) == 0:
print("Insulated")
else:
print("Not insulated")
if __name__ == "__main__":
# examples
space = np.zeros((10, 10, 10))
space[0, :, :] = 1
space[9, :, :] = 1
space[:, 0, :] = 1
space[:, 9, :] = 1
space[:, :, 0] = 1
space[:, :, 9] = 1
test(space)
space = np.zeros((10, 10, 10))
space[1, :, :] = 1
space[8, :, :] = 1
space[:, 1, :] = 1
space[:, 8, :] = 1
space[:, :, 1] = 1
space[:, :, 8] = 1
test(space)
space = np.zeros((10, 10, 10))
space[2, :, :] = 1
space[7, :, :] = 1
space[:, 2, :] = 1
space[:, 7, :] = 1
space[:, :, 2] = 1
space[:, :, 7] = 1
test(space)
space = np.zeros((10, 10, 10))
space[3, :, :] = 1
space[6, :, :] = 1
space[:, 3, :] = 1
space[:, 6, :] = 1
space[:, :, 3] = 1
space[:, :, 6] = 1
test(space)
space = np.zeros((10, 10, 10))
space[4, :, :] = 1
space[5, :, :] = 1
space[:, 4, :] = 1
space[:, 5, :] = 1
space[:, :, 4] = 1
space[:, :, 5] = 1
test(space)
# non-examples
space = np.zeros((10, 10, 10))
space[0, :, :] = 1
space[9, :, :] = 1
space[:, 0, :] = 1
space[:, 9, :] = 1
space[:, :, 0] = 1
space[:, :, 9] = 1
space[0, 4, 4] = 0 # hole on boundary
test(space)
space = np.zeros((10, 10, 10))
space[0, :, :] = 1
space[9, :, :] = 1
space[:, 0, :] = 1
space[:, 9, :] = 1
space[:, :, 0] = 1
# space[:, :, 9] = 1 # no roof
test(space)
space = np.zeros((10, 10, 10))
space[0, :, :] = 1
space[9, :, :] = 1
space[:, 0, :] = 1
# space[:, 9, :] = 1 # no wall
space[:, :, 0] = 1
space[:, :, 9] = 1
test(space)
space = np.zeros((10, 10, 10))
# nothing
test(space)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment