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.
No examples were given, so I just ran the program with my input.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]
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.
No comments:
Post a Comment