-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_06_solution_part_02.py
40 lines (32 loc) · 1.34 KB
/
day_06_solution_part_02.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
39
40
import numpy as np
def read_input(file_path: str) -> list[str]:
'''Load instructions'''
with open(file_path) as f:
instructions = [line.rstrip() for line in f]
return instructions
def count_brightness(instructions: list[str], dim: tuple[int, int]) -> int:
'''Count the total brightness of all lights'''
lights = np.zeros(dim, dtype=int)
for ins in instructions:
ins_list = ins.split(" ")
if ins_list[0] == "turn":
start = [int(num) for num in ins_list[2].split(",")]
end = [int(num) for num in ins_list[-1].split(",")]
if ins_list[1] == "on":
lights[start[0]:end[0]+1, start[1]:end[1]+1] += 1
elif ins_list[1] == "off":
lights[start[0]:end[0]+1, start[1]:end[1]+1] -= 1
lights[lights < 0] = 0
elif ins_list[0] == "toggle":
start = [int(num) for num in ins_list[1].split(",")]
end = [int(num) for num in ins_list[-1].split(",")]
lights[start[0]:end[0]+1, start[1]:end[1]+1] += 2
return np.sum(lights)
def main() -> None:
file_path = "./day_06_input.txt"
instructions = read_input(file_path)
dimensions = (1000, 1000)
brightness = count_brightness(instructions, dimensions)
print(f"Total brightness = {brightness}")
if __name__ == "__main__":
main()