summary refs log tree commit diff stats
path: root/day6.py
blob: 86c4abcf86462765276a1fcf68703490b682a93d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/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))