Solution to problem 6 in Python
This commit is contained in:
parent
a0ac7b9a1d
commit
0fe90e6970
@ -90,5 +90,73 @@ def part_1():
|
||||
print(f"There are {lit} ligths lit")
|
||||
|
||||
|
||||
# --- 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.
|
||||
|
||||
|
||||
def apply_action_(
|
||||
action: str,
|
||||
coords: list[int],
|
||||
grid: dict[tuple[int, int], int],
|
||||
) -> dict[tuple[int, int], int]:
|
||||
if action == "off":
|
||||
for x in range(coords[0], coords[2] + 1):
|
||||
for y in range(coords[1], coords[3] + 1):
|
||||
if grid[x, y] != 0:
|
||||
grid[x, y] -= 1
|
||||
else:
|
||||
grid[x, y] = 0
|
||||
return grid
|
||||
elif action == "on":
|
||||
for x in range(coords[0], coords[2] + 1):
|
||||
for y in range(coords[1], coords[3] + 1):
|
||||
grid[x, y] += 1
|
||||
return grid
|
||||
elif action == "toggle":
|
||||
for x in range(coords[0], coords[2] + 1):
|
||||
for y in range(coords[1], coords[3] + 1):
|
||||
grid[x, y] += 2
|
||||
return grid
|
||||
|
||||
|
||||
def part_2():
|
||||
# Initially, all lights are off("0")
|
||||
grid = defaultdict(int)
|
||||
for x in range(1000):
|
||||
for y in range(1000):
|
||||
grid[x, y] = 0
|
||||
|
||||
for instruction in instructions:
|
||||
action, *coords = get_coordinates(instruction)
|
||||
grid = apply_action_(action, coords, grid)
|
||||
|
||||
brightness = sum(value for value in grid.values())
|
||||
print(f"The total brightness is {brightness}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
||||
|
Loading…
Reference in New Issue
Block a user