from collections import defaultdict with open("input") as f: puzzle_input = [n.strip() for n in f.readlines()] lights = [[False for _ in range(1000)] for _ in range(1000)] for instruction in puzzle_input: from_coords = instruction.split(" ")[-1::-1][2] to_coords = instruction.split(" ")[-1::-1][0] from_coords = [int(x) for x in from_coords.split(",")] to_coords = [int(x) for x in to_coords.split(",")] [x_1, y_1] = from_coords [x_2, y_2] = to_coords toggle_mode = instruction.startswith("toggle") set_to = instruction.startswith("turn on") for x in range(x_1, x_2 + 1): for y in range(y_1, y_2 + 1): if toggle_mode: lights[x][y] = not lights[x][y] else: lights[x][y] = set_to print(instruction, from_coords, to_coords) lights_on = 0 for row in lights: for light in row: if light: lights_on += 1 print(lights_on)