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