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