3
3
from GhostyUtils .grid import Grid
4
4
from collections import Counter
5
5
import math
6
+ import pathlib
7
+ from PIL import Image , ImageDraw
6
8
7
9
8
10
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]:
52
54
return count
53
55
54
56
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 (',' )))} " )
57
66
58
67
59
68
def main ():
@@ -66,19 +75,38 @@ def main():
66
75
dimensions = Vec2 .from_str (aoc .args .dimensions , ',' )
67
76
grid = Grid (['.' * dimensions .x ] * dimensions .y )
68
77
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
+
72
83
for s in range (aoc .args .seconds ):
73
84
for robot in robots :
74
85
robot .step (grid )
75
86
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 ()
79
91
80
92
print (f"p1: { math .prod (count_quadrants (robots , grid ))} " )
81
93
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
+
82
110
83
111
if __name__ == "__main__" :
84
112
main ()
0 commit comments