Optimized solution
This commit is contained in:
@@ -32,8 +32,6 @@ from collections import defaultdict
|
||||
with open("files/P6.txt") as f:
|
||||
instructions = [line for line in f.read().strip().split("\n")]
|
||||
|
||||
# print(instructions)
|
||||
|
||||
|
||||
def get_coordinates(s: str) -> tuple[str, int, int, int, int]:
|
||||
if s.startswith(("turn off", "turn on")):
|
||||
@@ -54,39 +52,38 @@ def apply_action(
|
||||
action: str,
|
||||
coords: list[int],
|
||||
grid: dict[tuple[int, int], int],
|
||||
) -> dict[tuple[int, int], int]:
|
||||
) -> None:
|
||||
x_start, y_start, x_end, y_end = coords
|
||||
|
||||
if action == "off":
|
||||
for x in range(coords[0], coords[2] + 1):
|
||||
for y in range(coords[1], coords[3] + 1):
|
||||
for x in range(x_start, x_end + 1):
|
||||
for y in range(y_start, y_end + 1):
|
||||
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):
|
||||
return
|
||||
|
||||
if action == "on":
|
||||
for x in range(x_start, x_end + 1):
|
||||
for y in range(y_start, y_end + 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):
|
||||
if grid[x, y] == 1:
|
||||
grid[x, y] = 0
|
||||
elif grid[x, y] == 0:
|
||||
grid[x, y] = 1
|
||||
return grid
|
||||
return
|
||||
|
||||
# action == "toggle"
|
||||
for x in range(x_start, x_end + 1):
|
||||
for y in range(y_start, y_end + 1):
|
||||
grid[x, y] ^= 1
|
||||
|
||||
|
||||
def part_1():
|
||||
# Initially, all lights are off("0")
|
||||
grid = defaultdict(int)
|
||||
for x in range(1000):
|
||||
for y in range(1000):
|
||||
grid[x, y] = 0
|
||||
# Initially, all lights are at brightness 0 (implicit via defaultdict(int))
|
||||
grid: defaultdict[tuple[int, int], int] = defaultdict(int)
|
||||
|
||||
for instruction in instructions:
|
||||
action, *coords = get_coordinates(instruction)
|
||||
grid = apply_action(action, coords, grid)
|
||||
# in-place mutation of grid
|
||||
apply_action(action, coords, grid)
|
||||
|
||||
lit = sum(grid.values())
|
||||
|
||||
lit = sum(1 for value in grid.values() if value == 1)
|
||||
print(f"There are {lit} ligths lit")
|
||||
|
||||
|
||||
@@ -121,39 +118,37 @@ def apply_action_(
|
||||
action: str,
|
||||
coords: list[int],
|
||||
grid: dict[tuple[int, int], int],
|
||||
) -> dict[tuple[int, int], int]:
|
||||
) -> None:
|
||||
x_start, y_start, x_end, y_end = coords
|
||||
|
||||
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):
|
||||
for x in range(x_start, x_end + 1):
|
||||
for y in range(y_start, y_end + 1):
|
||||
grid[x, y] = max(grid[x, y] - 1, 0)
|
||||
return
|
||||
|
||||
if action == "on":
|
||||
for x in range(x_start, x_end + 1):
|
||||
for y in range(y_start, y_end + 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
|
||||
return
|
||||
|
||||
# action == "toggle"
|
||||
for x in range(x_start, x_end + 1):
|
||||
for y in range(y_start, y_end + 1):
|
||||
grid[x, y] += 2
|
||||
|
||||
|
||||
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
|
||||
# Initially, all lights are at brightness 0 (implicit via defaultdict(int))
|
||||
grid: defaultdict[tuple[int, int], int] = defaultdict(int)
|
||||
|
||||
for instruction in instructions:
|
||||
action, *coords = get_coordinates(instruction)
|
||||
grid = apply_action_(action, coords, grid)
|
||||
# in-place mutation of grid
|
||||
apply_action_(action, coords, grid)
|
||||
|
||||
brightness = sum(value for value in grid.values())
|
||||
brightness = sum(grid.values())
|
||||
print(f"The total brightness is {brightness}")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user