Guest User

Untitled

a guest
Dec 22nd, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. cubes = process_input('test2.txt')
  2. X, Y, Z = [], [], []
  3. for cube in cubes:
  4.     x0, x1, y0, y1, z0, z1 = cube.coords
  5.     X.append(x0)
  6.     X.append(x1 + 1)
  7.     Y.append(y0)
  8.     Y.append(y1 + 1)
  9.     Z.append(z0)
  10.     Z.append(z1 + 1)
  11. X = list(set(X))
  12. Y = list(set(Y))
  13. Z = list(set(Z))
  14. X.sort()
  15. Y.sort()
  16. Z.sort()
  17. N = len(X)
  18. grid = defaultdict(lambda: 0)
  19. for cube in cubes:
  20.     x0 = X.index(cube.x0)
  21.     x1 = X.index(cube.x1 + 1)
  22.     y0 = Y.index(cube.y0)
  23.     y1 = Y.index(cube.y1 + 1)
  24.     z0 = Z.index(cube.z0)
  25.     z1 = Z.index(cube.z1 + 1)
  26.     value = int(cube.state == 'on')
  27.     for x in range(x0, x1):
  28.         for y in range(y0, y1):
  29.             for z in range(z0, z1):
  30.                 grid[(x, y, z)] = value
  31.  
  32. total = 0
  33. for x in range(N-1):
  34.     for y in range(N-1):
  35.         for z in range(N-1):
  36.             total += grid[(x, y, z)] * (X[x+1] - X[x])  * (Y[y+1] - Y[y]) * (Z[z+1] - Z[z])
  37.  
  38. print(total)
Advertisement
Add Comment
Please, Sign In to add comment