Skip to content

Commit a9f8a11

Browse files
committed
[2024/14] p2 solved (programatically)
1 parent 27bcfef commit a9f8a11

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

2024/14/script.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from GhostyUtils import aoc
2-
from GhostyUtils.vec2 import Vec2
2+
from GhostyUtils.vec2 import Vec2, Dir
33
from GhostyUtils.grid import Grid
44
from collections import Counter
55
import math
@@ -94,18 +94,24 @@ def main():
9494
most_horz_robots = 0
9595
best_second = 0
9696
for s in range(aoc.args.seconds, dimensions.x * dimensions.y):
97-
horz_robots = [0] * dimensions.y
9897
for robot in robots:
9998
robot.step(grid)
100-
horz_robots[robot.pos.y] += 1
10199

102-
if max(horz_robots) > most_horz_robots:
103-
most_horz_robots = max(horz_robots)
104-
best_second = s
100+
robots.sort(key=lambda robot: robot.pos.as_tuple()[::-1])
101+
horz_robots = 1
102+
for r1, r2 in zip(robots, robots[1:]):
103+
if r1.pos + Dir.RIGHT == r2.pos:
104+
horz_robots += 1
105+
else:
106+
if horz_robots > most_horz_robots:
107+
most_horz_robots = horz_robots
108+
best_second = s+1
109+
horz_robots = 1
110+
105111
if aoc.args.progress or aoc.args.verbose:
106112
render_robots(robots, grid, seconds=s+1)
107113

108-
print(f"p2: {best_second} {most_horz_robots}")
114+
print(f"p2: {best_second}")
109115

110116

111117
if __name__ == "__main__":

0 commit comments

Comments
 (0)