solution.py 963 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. def process_command(command, cube):
  5. print(f'processing {command}')
  6. for z in range(command[1][4], command[1][5] + 1):
  7. for y in range(command[1][2], command[1][3] + 1):
  8. for x in range(command[1][0], command[1][1] + 1):
  9. cube.add((x, y, z)) if command[0] else cube.discard((x, y, z))
  10. initialization_size = 50
  11. commands = []
  12. cube = set()
  13. for line in sys.stdin:
  14. match = re.match(r'(on|off) x=(-?[0-9]+)..(-?[0-9]+),y=(-?[0-9]+)..(-?[0-9]+),z=(-?[0-9]+)..(-?[0-9]+)', line.rstrip('\n'))
  15. if not match or len([group for group in match.groups()[1:] if abs(int(group)) > 50]) > 0:
  16. continue
  17. commands.append((match.groups()[0] == 'on', [int(group) for group in match.groups()[1:]]))
  18. while len(commands) > 0:
  19. command = commands.pop(0)
  20. process_command(command, cube)
  21. print(f'{len(cube)} cubes are turned on')
  22. # TODO: try calculating intersections instead