1
- use std:: collections:: HashMap ;
1
+ use std:: collections:: { HashMap , HashSet } ;
2
+ use std:: fmt:: Display ;
2
3
use std:: ops:: { Deref , Rem } ;
3
4
use std:: str:: FromStr ;
4
5
use std:: sync:: LazyLock ;
@@ -52,23 +53,57 @@ fn part2(input: String) -> Result<()> {
52
53
let mut robots = Robots :: from_str ( & input) . unwrap ( ) ;
53
54
info ! ( "robots: {robots:?}" ) ;
54
55
55
- let max_iterations = 1_000_000 ;
56
- let mut iterations = 0 ;
57
- loop {
58
- if iterations == max_iterations {
59
- panic ! ( "max iterations ({max_iterations}) reached without a solution" ) ;
60
- }
61
-
62
- iterations += 1 ;
56
+ let max_iterations = 10_000 ;
57
+ let mut list: Vec < _ > = ( 0 ..max_iterations)
58
+ . map ( |i| {
59
+ if i != 0 {
60
+ robots. move_once ( ) ;
61
+ }
62
+ // I got this idea from reddit.. still very satisfying!
63
+ //
64
+ // ###############################
65
+ // #.............................#
66
+ // #.............................#
67
+ // #.............................#
68
+ // #.............................#
69
+ // #..............#..............#
70
+ // #.............###.............#
71
+ // #............#####............#
72
+ // #...........#######...........#
73
+ // #..........#########..........#
74
+ // #............#####............#
75
+ // #...........#######...........#
76
+ // #..........#########..........#
77
+ // #.........###########.........#
78
+ // #........#############........#
79
+ // #..........#########..........#
80
+ // #.........###########.........#
81
+ // #........#############........#
82
+ // #.......###############.......#
83
+ // #......#################......#
84
+ // #........#############........#
85
+ // #.......###############.......#
86
+ // #......#################......#
87
+ // #.....###################.....#
88
+ // #....#####################....#
89
+ // #.............###.............#
90
+ // #.............###.............#
91
+ // #.............###.............#
92
+ // #.............................#
93
+ // #.............................#
94
+ // #.............................#
95
+ // #.............................#
96
+ // ###############################
97
+ let neighboor_prob_score = robots. compute_neighboor_prob_score ( ) ;
98
+ ( neighboor_prob_score, robots. clone ( ) , i)
99
+ } )
100
+ . collect ( ) ;
101
+ list. sort_by ( |a, b| b. 0 . cmp ( & a. 0 ) ) ;
63
102
64
- robots. move_once ( ) ;
65
- let quadrants = robots. compute_quadrants ( ) ;
66
- if quadrants. is_christmas_tree ( robots. width , robots. height ) {
67
- break ;
68
- }
69
- }
103
+ let candidate = list. first ( ) . unwrap ( ) ;
104
+ info ! ( "iteration {}:\n {}" , candidate. 0 , candidate. 1 ) ;
70
105
71
- println ! ( "{iterations}" ) ;
106
+ println ! ( "{}" , candidate . 2 ) ;
72
107
Ok ( ( ) )
73
108
}
74
109
@@ -137,6 +172,23 @@ impl Deref for Robots {
137
172
}
138
173
}
139
174
175
+ impl Display for Robots {
176
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
177
+ let positions: HashSet < ( u32 , u32 ) > = HashSet :: from_iter ( self . robots . iter ( ) . map ( |r| r. position ) ) ;
178
+ for y in 0 ..self . height {
179
+ for x in 0 ..self . width {
180
+ if positions. contains ( & ( x, y) ) {
181
+ write ! ( f, "#" ) ?;
182
+ } else {
183
+ write ! ( f, "." ) ?;
184
+ }
185
+ }
186
+ writeln ! ( f) ?;
187
+ }
188
+ Ok ( ( ) )
189
+ }
190
+ }
191
+
140
192
#[ derive( Debug , Default ) ]
141
193
struct Quadrants (
142
194
HashMap < ( u32 , u32 ) , usize > ,
@@ -154,18 +206,6 @@ impl Quadrants {
154
206
self . 3 . values ( ) . sum ( ) ,
155
207
)
156
208
}
157
-
158
- fn compute_uniques ( & self ) -> ( usize , usize , usize , usize ) {
159
- ( self . 0 . len ( ) , self . 1 . len ( ) , self . 2 . len ( ) , self . 3 . len ( ) )
160
- }
161
-
162
- fn is_christmas_tree ( & self , width : u32 , height : u32 ) -> bool {
163
- let center = ( width / 2 , height / 2 ) ;
164
- let size = center. 0 * center. 1 ;
165
- let ( q1, q2, q3, q4) = self . compute_uniques ( ) ;
166
- // TODO
167
- false
168
- }
169
209
}
170
210
171
211
impl Robots {
@@ -193,4 +233,23 @@ impl Robots {
193
233
acc
194
234
} )
195
235
}
236
+
237
+ fn compute_neighboor_prob_score ( & self ) -> u32 {
238
+ let positions: HashSet < ( u32 , u32 ) > = HashSet :: from_iter ( self . robots . iter ( ) . map ( |r| r. position ) ) ;
239
+
240
+ let directions = [ ( -1 , -1 ) , ( 0 , -1 ) , ( 1 , -1 ) , ( -1 , 0 ) , ( 1 , 0 ) , ( -1 , 1 ) , ( 0 , 1 ) , ( 1 , 1 ) ] ;
241
+ self . robots
242
+ . iter ( )
243
+ . flat_map ( |r| {
244
+ directions. iter ( ) . map ( |d| {
245
+ let pos = ( r. position . 0 as i32 + d. 0 , r. position . 1 as i32 + d. 1 ) ;
246
+ if pos. 0 >= 0 && pos. 1 >= 0 && positions. contains ( & ( pos. 0 as u32 , pos. 1 as u32 ) ) {
247
+ 1
248
+ } else {
249
+ 0
250
+ }
251
+ } )
252
+ } )
253
+ . sum ( )
254
+ }
196
255
}
0 commit comments