From 68615a9ad2c942254135cffb00cf25a84a3b1356 Mon Sep 17 00:00:00 2001 From: Prefetch Date: Sat, 31 Dec 2022 22:21:39 +0100 Subject: Initial commit --- 06/main.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 06/main.py (limited to '06/main.py') diff --git a/06/main.py b/06/main.py new file mode 100755 index 0000000..cf9f85c --- /dev/null +++ b/06/main.py @@ -0,0 +1,53 @@ +#!/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() -- cgit v1.2.3