File tree Expand file tree Collapse file tree 2 files changed +14
-12
lines changed
Expand file tree Collapse file tree 2 files changed +14
-12
lines changed Original file line number Diff line number Diff line change 11use aoc:: { common, grid:: Grid } ;
22
33fn remove_if_possible ( grid : & mut Grid < char > ) -> Option < usize > {
4- let positions = grid. positions ( & '@' ) ;
5- let mut to_be_removed = Vec :: new ( ) ;
6- for pos in positions. iter ( ) {
7- let nbs = grid. adjacent_8 ( pos) ;
8- //dbg!(pos, &nbs);
9- if nbs. iter ( ) . filter ( |x| grid. get ( x) == '@' ) . count ( ) < 4 {
10- to_be_removed. push ( pos) ;
11- }
12- }
4+ let to_be_removed: Vec < ( usize , usize ) > = grid
5+ . positions ( & '@' )
6+ . iter ( )
7+ . map ( |idx| ( idx, grid. adjacent_8 ( idx) ) )
8+ . filter ( |( _idx, neighbors) | neighbors. iter ( ) . filter ( |x| grid. get ( x) == '@' ) . count ( ) < 4 )
9+ . map ( |( idx, _neighbors) | * idx)
10+ . collect ( ) ;
1311
1412 if to_be_removed. is_empty ( ) {
1513 return None ;
1614 }
1715
18- for idx in to_be_removed. iter ( ) {
19- grid. set ( idx, '.' ) ;
20- }
16+ grid. set_all ( & to_be_removed, '.' ) ;
2117
2218 Some ( to_be_removed. len ( ) )
2319}
Original file line number Diff line number Diff line change @@ -121,6 +121,12 @@ impl<T: std::fmt::Debug + Clone + Default + PartialEq + Hash> Grid<T> {
121121 ret
122122 }
123123
124+ pub fn set_all ( & mut self , positions : & [ CellIndex ] , val : T ) {
125+ for idx in positions. iter ( ) {
126+ self . set ( idx, val. clone ( ) ) ;
127+ }
128+ }
129+
124130 #[ must_use]
125131 pub fn count ( & self , x : & T ) -> usize {
126132 self . values
You can’t perform that action at this time.
0 commit comments