#!/usr/bin/python import numpy as np def solve_partn(partn, lines): # In part 1, we treat an int32 as a boolean: 1 = True, 0 = False grid = np.zeros((1000, 1000), dtype="int32") for line in lines: words = line.split() if words[0] == "turn": xi, yi = [int(n) for n in words[2].split(",")] xf, yf = [int(n) + 1 for n in words[4].split(",")] if partn == 1: if words[1] == "on": grid[xi : xf, yi : yf] = 1 elif words[1] == "off": grid[xi : xf, yi : yf] = 0 else: # partn == 2 if words[1] == "on": grid[xi : xf, yi : yf] += 1 elif words[1] == "off": grid[xi : xf, yi : yf] -= 1 grid = np.clip(grid, 0, 1000000) elif words[0] == "toggle": xi, yi = [int(n) for n in words[1].split(",")] xf, yf = [int(n) + 1 for n in words[3].split(",")] if partn == 1: grid[xi : xf, yi : yf] = 1 - grid[xi : xf, yi : yf] # NOT else: # partn == 2 grid[xi : xf, yi : yf] += 2 return np.sum(grid) def main(): # Read instructions from input text file with open("input.txt", "r") as f: lines = f.read().splitlines() print("Part 1 solution:", solve_partn(1, lines)) # 400410 for me print("Part 2 solution:", solve_partn(2, lines)) # 15343601 for me if __name__ == "__main__": main()