from lib import get_data, ints from collections import defaultdict as DD data = get_data(__file__) ons = set() ons2 = DD(int) for line in data.splitlines(): x1, y1, x2, y2 = ints(line) for x in range(x1, x2 + 1): for y in range(y1, y2 + 1): if "off" in line: if (x, y) in ons: ons.remove((x, y)) ons2[(x, y)] = max(ons2[(x, y)] - 1, 0) elif "on" in line: ons.add((x, y)) ons2[(x, y)] += 1 elif "toggle" in line: if (x, y) in ons: ons.remove((x, y)) else: ons.add((x, y)) ons2[(x, y)] += 2 else: assert False print(len(ons)) print(sum(ons2.values()))