Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

3 anos atrás
3 anos atrás
12345678910111213141516171819202122232425262728293031323334
  1. import re
  2. def net_volume(cubes):
  3. if not cubes:
  4. return 0
  5. (is_on, cube), *rest = cubes
  6. if not is_on:
  7. return net_volume(rest)
  8. return volume(cube) - net_volume(intersect(cube, rest)) + net_volume(rest)
  9. def intersect(cube, rest):
  10. return [
  11. (True, new)
  12. for _, other in rest
  13. for new in [[f(a, b) for a, b, f in zip(cube, other, [max, min] * 3)]]
  14. if volume(new)
  15. ]
  16. def volume(cube):
  17. xA, xB, yA, yB, zA, zB = cube
  18. return max(xB - xA + 1, 0) * max(yB - yA + 1, 0) * max(zB - zA + 1, 0)
  19. cubes = tuple(
  20. (line.split()[0] == 'on', [int(n) for n in re.findall(r'-?\d+', line)])
  21. for line in open(0).read().splitlines()
  22. )
  23. print(net_volume(cubes[:20]))
  24. print(net_volume(cubes))