September 18, 2023

Solution for Advent of Code 2015 - Day 6: Probably a Fire Hazard

Link to the puzzle text

Part 1

In this puzzle, multiple operations are performed on a grid: turnon, turnoff and toggle. To simulate the grid, I used an numpy array initialized with all zeroes. Since we each operation always effects a rectangle in our grid, we can use the numpy array indexing to simplify the access. The turnon operation sets the brightness to 1, turnoff to 0 and toggle switches between these two states.

if instruction == "turnon":
grid[start_x:end_x+1, start_y:end_y+1] = 1
if instruction == "turnoff":
grid[start_x:end_x+1, start_y:end_y+1] = 0
if instruction == "toggle":
grid[start_x:end_x+1, start_y:end_y+1] = 1 - grid[start_x:end_x+1, start_y:end_y+1]
No examples were given, so I just ran the program with my input.

Part 2

For the second part, the operations have a different effect. The grid handling can stay the same, so we just replace the operation handling. Turnon now increases the brightness by 1, toggle increases by 2 , while turnoff decreases by 1. After the decrease, we set the brightness to a minimum of 0 with the clip function.
if instruction == "turnon":
grid[start_x:end_x+1, start_y:end_y+1] += 1
if instruction == "turnoff":
grid[start_x:end_x+1, start_y:end_y+1] -= 1
grid = grid.clip(0)
if instruction == "toggle":
grid[start_x:end_x+1, start_y:end_y+1] += 2

Again, no examples were given, so I just ran the program with my input.

Link to my solutions

No comments:

Post a Comment