1
0
Fork 0

update 2024/day14

This commit is contained in:
Sebastian Mark 2024-12-14 15:55:22 +01:00
parent fde8a0968f
commit af38974a4c
2 changed files with 9 additions and 9 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -32,7 +32,7 @@ def get_robot_pos_after_time(area: dict, robot: tuple, seconds: int) -> tuple:
return (finx, finy)
def get_quadrant(area: dict, position: tuple) -> int:
def get_quadrant_for_pos(area: dict, position: tuple) -> int:
middle_x = area["width"] // 2
middle_y = area["height"] // 2
@ -51,17 +51,17 @@ def get_quadrant(area: dict, position: tuple) -> int:
return 4
def assign_qadrants(area: dict, positions: dict) -> list:
def count_positions_in_quadrants(area: dict, positions: dict) -> list:
quadrant_count = {}
for pos, count in positions.items():
quadrant = get_quadrant(area, pos)
quadrant = get_quadrant_for_pos(area, pos)
if quadrant != 0:
quadrant_count[quadrant] = quadrant_count.get(quadrant, 0) + count
return quadrant_count
def draw_positions_in_area(area: dict, positions: dict, filename: str):
def draw_area(area: dict, positions: dict, filename: str):
grid = []
for _ in range(area["height"]):
row = [0] * area["width"]
@ -72,9 +72,9 @@ def draw_positions_in_area(area: dict, positions: dict, filename: str):
grid[y][x] = 1
plt.figure()
plt.imshow(grid, cmap="gray", interpolation="nearest")
plt.imshow(grid, cmap="gray")
plt.axis("off")
plt.savefig(f"{filename}.jpg", format="jpg", bbox_inches="tight", pad_inches=0)
plt.savefig(f"{filename}.jpg", bbox_inches="tight")
plt.close()
@ -91,7 +91,7 @@ def main():
positions[pos] = positions.get(pos, 0) + 1
safety_factor = 1
quadrant_count = assign_qadrants(area, positions)
quadrant_count = count_positions_in_quadrants(area, positions)
for c in quadrant_count.values():
safety_factor *= c
@ -104,8 +104,8 @@ def main():
for robot in robots:
pos = get_robot_pos_after_time(area, robot, i)
positions[pos] = positions.get(pos, 0) + 1
print(f"\rSaving image for position after {i} seconds", end="", flush=True)
draw_positions_in_area(area, positions, str("%05d" % i))
print(f"\rSaving image for positions after {i} seconds", end="", flush=True)
draw_area(area, positions, str("%05d" % i))
if __name__ == "__main__":