import sys from pprint import pprint import re from itertools import product from collections import defaultdict instructions = [] with open(sys.argv[1]) as file: tmp = [] for line in file: tmp = [re.match(r'.+?(?=\d)',line).group().strip()] tmp += [int(i) for i in re.findall(r'\d+',line)] instructions.append(tmp) size = 1000 maps = defaultdict(int) for i in instructions: for x in product(range(i[1],i[3]+1),range(i[2],i[4]+1)): if i[0] == 'turn off': if maps[x] != 0: maps[x] -= 1 elif i[0] == 'turn on': maps[x] += 1 else: maps[x] += 2 count = 0 for i in product(range(0,size),range(0,size)): count += maps[i] print(count)