-
Notifications
You must be signed in to change notification settings - Fork 1
/
06.py
38 lines (30 loc) · 1.06 KB
/
06.py
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
"""--- Day 6: Probably a Fire Hazard ---"""
import re
with open("input/06.txt") as f:
size = 1000
lit = brightness = 0
# create matrix
grid = [[[0, 0] for _ in range(size)] for _ in range(size)]
for line in f:
action = re.findall("(on|off|toggle)", line)[0]
from_x, from_y, to_x, to_y = map(int, re.findall("([0-9]+)", line))
for x in range(from_x, to_x + 1):
for y in range(from_y, to_y + 1):
if action == "on":
grid[x][y][0] = 1 # switch on
grid[x][y][1] += 1 # increase brightness
elif action == "off":
grid[x][y][0] = 0 # switch off
if grid[x][y][1] > 0:
grid[x][y][1] -= 1 # decrease brightness
else:
grid[x][y][0] = 1 - grid[x][y][0] # toggle
grid[x][y][1] += 2 # increase brightness by 2
# calculate brightness
for x in range(size):
for y in range(size):
brightness += grid[x][y][1]
if grid[x][y][0] == 1:
lit += 1
print(lit)
print(brightness)