summary refs log blame commit diff stats
path: root/day6.py
blob: 86c4abcf86462765276a1fcf68703490b682a93d (plain) (tree)






































                                                                               
#!/usr/bin/env python

import numpy as np
import re

num_parse = re.compile(r'(toggle|off|on) (\d+,\d+) through (\d+,\d+)$')

with open('day6.txt') as data:
    instructions = [num_parse.search(line).groups((1, 2, 3)) for line in data]

lights = np.zeros((1000, 1000), dtype=bool)
for inst in instructions:
    x1, y1 = map(int, inst[1].split(','))
    x2, y2 = map(int, inst[2].split(','))
    match inst[0]:
        case 'toggle':
            lights[x1:x2+1, y1:y2+1] = np.logical_not(lights[x1:x2+1, y1:y2+1])
        case 'on':
            lights[x1:x2+1, y1:y2+1] = True
        case 'off':
            lights[x1:x2+1, y1:y2+1] = False

print(np.count_nonzero(lights))

lights = np.zeros((1000, 1000), dtype=int)
for inst in instructions:
    x1, y1 = map(int, inst[1].split(','))
    x2, y2 = map(int, inst[2].split(','))
    match inst[0]:
        case 'toggle':
            lights[x1:x2+1, y1:y2+1] += 2
        case 'on':
            lights[x1:x2+1, y1:y2+1] += 1
        case 'off':
            region = lights[x1:x2+1, y1:y2+1]
            region -= 1
            region[region < 0] = 0

print(np.sum(lights))