Skip to content

Commit 5dfb1b5

Browse files
committed
[2024/14] p2 solved (visually)
1 parent b9a1a41 commit 5dfb1b5

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

.aoc_tiles/tiles/2024/14.png

54 Bytes
Loading

2024/14/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*_robots/

2024/14/script.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from GhostyUtils.grid import Grid
44
from collections import Counter
55
import math
6+
import pathlib
7+
from PIL import Image, ImageDraw
68

79

810
aoc.argparser.add_argument("-d", "--dimensions", type=str, default="101,103",
@@ -52,8 +54,15 @@ def count_quadrants(robots: list[Robot], grid: Grid) -> list[int]:
5254
return count
5355

5456

55-
def render_robots(robots: list[Robot], grid: Grid) -> str:
56-
return grid.render_with_overlays([Counter(robot.pos.as_tuple() for robot in robots)])
57+
def render_robots(robots: list[Robot], grid: Grid, seconds: int):
58+
image = Image.new(mode="1", size=(grid.width(), grid.height()), color=0)
59+
draw = ImageDraw.Draw(image)
60+
[draw.point(robot.pos.as_tuple(), fill=1) for robot in robots]
61+
pathlib.Path(f"{aoc.args.filename}_robots").mkdir(parents=True, exist_ok=True)
62+
image.save(f"{aoc.args.filename}_robots/{seconds}.png")
63+
64+
# print(grid.render_with_overlays([Counter(robot.pos.as_tuple() for robot in robots)]))
65+
print(f"{seconds}/{math.prod(map(int, aoc.args.dimensions.split(',')))}")
5766

5867

5968
def main():
@@ -66,19 +75,38 @@ def main():
6675
dimensions = Vec2.from_str(aoc.args.dimensions, ',')
6776
grid = Grid(['.' * dimensions.x] * dimensions.y)
6877

69-
print("Initial state:")
70-
print(render_robots(robots, grid))
71-
print()
78+
if aoc.args.progress or aoc.args.verbose:
79+
print("Initial state:")
80+
render_robots(robots, grid, seconds=0)
81+
print()
82+
7283
for s in range(aoc.args.seconds):
7384
for robot in robots:
7485
robot.step(grid)
7586

76-
print(f"After {s+1} second{'s' if s > 1 else ''}:")
77-
print(render_robots(robots, grid))
78-
print()
87+
if aoc.args.progress or aoc.args.verbose:
88+
print(f"After {s+1} second{'s' if s > 1 else ''}:")
89+
render_robots(robots, grid, seconds=s+1)
90+
print()
7991

8092
print(f"p1: {math.prod(count_quadrants(robots, grid))}")
8193

94+
most_horz_robots = 0
95+
best_second = 0
96+
for s in range(aoc.args.seconds, dimensions.x * dimensions.y):
97+
horz_robots = [0] * dimensions.y
98+
for robot in robots:
99+
robot.step(grid)
100+
horz_robots[robot.pos.y] += 1
101+
102+
if max(horz_robots) > most_horz_robots:
103+
most_horz_robots = max(horz_robots)
104+
best_second = s
105+
if aoc.args.progress or aoc.args.verbose:
106+
render_robots(robots, grid, seconds=s+1)
107+
108+
print(f"p2: {best_second} {most_horz_robots}")
109+
82110

83111
if __name__ == "__main__":
84112
main()

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 188/478 ⭐
6+
Advent of Code - 189/478 ⭐
77
</h1>
88
<h1 align="center">
9-
2024 - 27 ⭐ - Python
9+
2024 - 28 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">

0 commit comments

Comments
 (0)