20 lines
859 B
Python
Executable File
20 lines
859 B
Python
Executable File
#!/usr/bin/env python3
|
|
import sys, re
|
|
pattern = re.compile(r"(.+) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)")
|
|
instructions = [pattern.search(line.strip()).groups() for line in sys.stdin.readlines()]
|
|
lights = [[False] * 1000 for _ in range(1000)]
|
|
for ins in instructions:
|
|
if ins[0] == 'turn on':
|
|
for x in range(int(ins[1]), int(ins[3]) + 1):
|
|
for y in range(int(ins[2]), int(ins[4]) + 1):
|
|
lights[x][y] = True
|
|
elif ins[0] == 'turn off':
|
|
for x in range(int(ins[1]), int(ins[3]) + 1):
|
|
for y in range(int(ins[2]), int(ins[4]) + 1):
|
|
lights[x][y] = False
|
|
elif ins[0] == 'toggle':
|
|
for x in range(int(ins[1]), int(ins[3]) + 1):
|
|
for y in range(int(ins[2]), int(ins[4]) + 1):
|
|
lights[x][y] = not lights[x][y]
|
|
print(sum(sum(line) for line in lights))
|