Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: List of lines "<#>,<#>,<#>" (x,y,z coords of unit cubes)
- # Output: Number of faces reachable from exterior
- cubes = []
- input_data = open(r"c:\users\edward\desktop\personal\aoc2022\18_input.txt")
- for input_line in input_data.read().splitlines():
- input_line = input_line.replace("\n", "")
- coordinates = input_line.split(",")
- x, y, z = int(coordinates[0]), int(coordinates[1]), int(coordinates[2])
- cubes.append([x, y, z])
- # All coordinates happen to be from 0 to 20
- cells = []
- for x in range(0, 21):
- cell_square = []
- for y in range(0, 21):
- cell_line = []
- for z in range(0, 21):
- if x in (0, 20) or y in (0, 20) or z in (0, 20):
- cell_line.append("water") # lava will overwrite this later
- else:
- cell_line.append("unknown")
- cell_square.append(cell_line)
- cells.append(cell_square)
- for cube in cubes:
- cells[cube[0]][cube[1]][cube[2]] = "lava"
- offsets = [
- [1, 0, 0],
- [-1, 0, 0],
- [0, 1, 0],
- [0, -1, 0],
- [0, 0, 1],
- [0, 0, -1]
- ]
- while True:
- expanded_water = False
- for x in range(1, 20):
- for y in range(1, 20):
- for z in range(1, 20):
- if cells[x][y][z] == "unknown":
- for offset in offsets:
- x2, y2, z2 = x + offset[0], y + offset[1], z + offset[2]
- if cells[x2][y2][z2] == "water":
- cells[x][y][z] = "water"
- expanded_water = True
- break
- if not expanded_water:
- break
- number_faces = 0
- for cube in cubes:
- for offset in offsets:
- x, y, z = cube[0] + offset[0], cube[1] + offset[1], cube[2] + offset[2]
- if x < 0 or x > 20 or y < 0 or y > 20 or z < 0 or z > 20:
- number_faces += 1
- else:
- if cells[x][y][z] == "water":
- number_faces += 1
- print (number_faces)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement