Day 6


In [14]:
import csv
import numpy as np

instructions_list = []
with open('inputs/input6.txt', 'rt') as f_input:
    csv_reader = csv.reader(f_input, delimiter=' ')
    for line in csv_reader:
        c, d = tuple(map(int, line.pop().split(',')))
        line.pop()
        a, b = tuple(map(int, line.pop().split(',')))
        action = line.pop()
        instructions_list += [(action, a, b, c, d)]

Day 6.1


In [54]:
def lit(command):
    action = command[0]
    a, b, c, d = command[1:]
    if action == 'on':
        grid[a: c + 1, b: d + 1] = 1
    elif action == 'off':
        grid[a: c + 1, b: d + 1] = 0
    elif action == 'toggle':
        grid[a: c + 1, b: d + 1] = - grid[a: c + 1, b: d + 1] + 1

In [64]:
grid = np.zeros((1000, 1000))
for command in instructions_list:
    lit(command)
int(sum(sum(grid)))


Out[64]:
569999

Day 6.2


In [69]:
def brightness_lit(command):
    action = command[0]
    a, b, c, d = command[1:]
    if action == 'on':
        grid[a: c + 1, b: d + 1] += 1
    elif action == 'off':
        grid[a: c + 1, b: d + 1] -= 1
    elif action == 'toggle':
        grid[a: c + 1, b: d + 1] += 2
    mask = grid < 0
    grid[mask] = 0

In [70]:
grid = np.zeros((1000, 1000))
for command in instructions_list:
    brightness_lit(command)
int(sum(sum(grid)))


Out[70]:
17836115

In [ ]: