Advertisement
Guest User

Untitled

a guest
Dec 18th, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. # Input: List of lines "<#>,<#>,<#>" (x,y,z coords of unit cubes)
  2. # Output: Number of faces reachable from exterior
  3.  
  4. cubes = []
  5.  
  6. input_data = open(r"c:\users\edward\desktop\personal\aoc2022\18_input.txt")
  7. for input_line in input_data.read().splitlines():
  8.     input_line = input_line.replace("\n", "")
  9.     coordinates = input_line.split(",")
  10.     x, y, z = int(coordinates[0]), int(coordinates[1]), int(coordinates[2])
  11.     cubes.append([x, y, z])
  12.  
  13. # All coordinates happen to be from 0 to 20
  14. cells = []
  15. for x in range(0, 21):
  16.     cell_square = []
  17.     for y in range(0, 21):
  18.         cell_line = []
  19.         for z in range(0, 21):
  20.             if x in (0, 20) or y in (0, 20) or z in (0, 20):
  21.                 cell_line.append("water") # lava will overwrite this later
  22.             else:
  23.                 cell_line.append("unknown")
  24.         cell_square.append(cell_line)
  25.     cells.append(cell_square)
  26.  
  27. for cube in cubes:
  28.     cells[cube[0]][cube[1]][cube[2]] = "lava"
  29.  
  30. offsets = [
  31.     [1, 0, 0],
  32.     [-1, 0, 0],
  33.     [0, 1, 0],
  34.     [0, -1, 0],
  35.     [0, 0, 1],
  36.     [0, 0, -1]
  37. ]
  38.  
  39. while True:
  40.     expanded_water = False
  41.     for x in range(1, 20):
  42.         for y in range(1, 20):
  43.             for z in range(1, 20):
  44.                 if cells[x][y][z] == "unknown":
  45.                     for offset in offsets:
  46.                         x2, y2, z2 = x + offset[0], y + offset[1], z + offset[2]
  47.                         if cells[x2][y2][z2] == "water":
  48.                             cells[x][y][z] = "water"
  49.                             expanded_water = True
  50.                             break
  51.     if not expanded_water:
  52.         break
  53.  
  54. number_faces = 0
  55. for cube in cubes:
  56.     for offset in offsets:
  57.         x, y, z = cube[0] + offset[0], cube[1] + offset[1], cube[2] + offset[2]
  58.         if x < 0 or x > 20 or y < 0 or y > 20 or z < 0 or z > 20:
  59.             number_faces += 1
  60.         else:
  61.             if cells[x][y][z] == "water":
  62.                 number_faces += 1
  63.  
  64. print (number_faces)
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement