Advent of Code 2015 day 6 part2: solved

This commit is contained in:
Fabian Tessmer 2025-03-23 11:14:31 +01:00
parent 99193e344c
commit 778af31306
3 changed files with 134 additions and 2 deletions

View File

@ -0,0 +1,38 @@
--- Day 6: Probably a Fire Hazard ---
Because your neighbors keep defeating you in the holiday house decorating contest year after year, you've decided to deploy one million lights in a 1000x1000 grid.
Furthermore, because you've been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration.
Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square. The lights all start turned off.
To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order.
For example:
turn on 0,0 through 999,999 would turn on (or leave on) every light.
toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off.
turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights.
After following the instructions, how many lights are lit?
Your puzzle answer was 400410.
--- Part Two ---
You just finish implementing your winning light pattern when you realize you mistranslated Santa's message from Ancient Nordic Elvish.
The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero.
The phrase turn on actually means that you should increase the brightness of those lights by 1.
The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero.
The phrase toggle actually means that you should increase the brightness of those lights by 2.
What is the total brightness of all lights combined after following Santa's instructions?
For example:
turn on 0,0 through 0,0 would increase the total brightness by 1.
toggle 0,0 through 999,999 would increase the total brightness by 2000000.
Your puzzle answer was 15343601.
Both parts of this puzzle are complete! They provide two gold stars: **

View File

@ -94,5 +94,3 @@ if __name__ == '__main__':
puzzle_input = open("input.txt", "r").read().splitlines()
print("solution: ", run_all_instructions_print_on(puzzle_input))
print("Part2: ")
print("solution: ", )

View File

@ -0,0 +1,96 @@
class Grid:
def __init__(self):
self.grid = [[Light() for j in range(1000)] for i in range(1000)]
def toggle(self, sx, ex, sy, ey) -> None:
for i in range(sx, ex+1):
for j in range(sy, ey+1):
self.grid[i][j].toggle()
def turn_on(self, sx, ex, sy, ey) -> None:
for i in range(sx, ex+1):
for j in range(sy, ey+1):
self.grid[i][j].turn_on()
def turn_off(self, sx, ex, sy, ey) -> None:
for i in range(sx, ex+1):
for j in range(sy, ey+1):
self.grid[i][j].turn_off()
def num_on(self) -> int:
num_on = 0
for x in self.grid:
for y in x:
num_on += y.is_on()
return num_on
def print_grid(self):
for x in self.grid:
line = ""
for y in x:
line += str(int(y.is_on()))
print(line)
class Light:
def __init__(self):
self.lit = 0
def toggle(self):
self.lit += 2
def turn_on(self):
self.lit += 1
def turn_off(self):
self.lit = max(self.lit - 1, 0)
def is_on(self):
return self.lit
def instruction_to_action(instruction: str, grid: Grid):
def to_coords(coord_string: str) -> [int, int, int, int]:
coords = coord_string.split(' through ')
_sx, _sy = map(int, coords[0].split(','))
_ex, _ey = map(int, coords[1].split(','))
return _sx, _ex, _sy, _ey
if instruction.startswith('turn on'):
sx, ex, sy, ey = to_coords(instruction[8:])
grid.turn_on(sx, ex, sy, ey)
return
if instruction.startswith('turn off'):
sx, ex, sy, ey = to_coords(instruction[9:])
grid.turn_off(sx, ex, sy, ey)
return
if instruction.startswith('toggle'):
sx, ex, sy, ey = to_coords(instruction[7:])
grid.toggle(sx, ex, sy, ey)
return
Exception("instruction couldn't be parsed, instruction :", instruction)
def run_all_instructions_print_on(instructions: list) -> int:
grid = Grid()
for instruction in instructions:
instruction_to_action(instruction, grid)
return grid.num_on()
if __name__ == '__main__':
assert run_all_instructions_print_on(["turn on 0,0 through 999,999"]) == 1000000, \
"Error: Example 1 couldn't be solved"
assert run_all_instructions_print_on(["toggle 0,0 through 999,0"]) == 2000, \
"Error: Example 2 couldn't be solved"
assert run_all_instructions_print_on(["turn on 0,0 through 999,999", "turn off 499,499 through 500,500"]) == 1000000-4, \
"Error: Example 3 couldn't be solved"
print("All test passed")
puzzle_input = open("input.txt", "r").read().splitlines()
print("solution: ", run_all_instructions_print_on(puzzle_input))