@@ -38,7 +38,7 @@ fn adjacent(x: usize, y: usize) -> Vec<(usize, usize)> {
3838 vec ! [ ( x, y - 1 ) , ( x - 1 , y) , ( x + 1 , y) , ( x, y + 1 ) ]
3939}
4040
41- fn parse_target ( targets : & [ Unit ] , race : char ) -> HashMap < ( usize , usize ) , Vec < usize > > {
41+ fn get_opponents ( targets : & [ Unit ] , race : char ) -> HashMap < ( usize , usize ) , Vec < usize > > {
4242 let mut target_mapping: HashMap < ( usize , usize ) , Vec < usize > > = HashMap :: new ( ) ;
4343
4444 for ( i, t) in targets. iter ( ) . enumerate ( ) {
@@ -192,25 +192,24 @@ impl Puzzle {
192192 continue ;
193193 }
194194
195- let target_mapping = parse_target ( & units, units[ u] . race ) ;
195+ let opponents = get_opponents ( & units, units[ u] . race ) ;
196196
197- if !target_mapping . contains_key ( & ( units[ u] . x , units[ u] . y ) ) {
198- // not in target range
197+ if !opponents . contains_key ( & ( units[ u] . x , units[ u] . y ) ) {
198+ // not in range
199199
200- let target_adj: HashSet < ( usize , usize ) > =
201- target_mapping. keys ( ) . copied ( ) . collect ( ) ;
200+ let target_adj: HashSet < ( usize , usize ) > = opponents. keys ( ) . copied ( ) . collect ( ) ;
202201
203202 if let Some ( xy) = next_pos ( u, & units, & target_adj, wall) {
204203 units[ u] . x = xy. 0 ;
205204 units[ u] . y = xy. 1 ;
206205 }
207206 }
208207
209- if target_mapping . contains_key ( & ( units[ u] . x , units[ u] . y ) ) {
208+ if opponents . contains_key ( & ( units[ u] . x , units[ u] . y ) ) {
210209 // attack
211210 let damage = units[ u] . attack_power ;
212211
213- let target_indices = target_mapping . get ( & ( units[ u] . x , units[ u] . y ) ) . unwrap ( ) ;
212+ let target_indices = opponents . get ( & ( units[ u] . x , units[ u] . y ) ) . unwrap ( ) ;
214213
215214 let & target_index = target_indices
216215 . iter ( )
@@ -261,9 +260,6 @@ fn main() {
261260 let args = aoc:: parse_args ( ) ;
262261 let mut puzzle = Puzzle :: new ( ) ;
263262 puzzle. configure ( args. path . as_str ( ) ) ;
264-
265- // print!("\x1b[H\x1b[2J\x1b]1337;ClearScrollback\x07");
266-
267263 println ! ( "{}" , puzzle. part1( ) ) ;
268264 println ! ( "{}" , puzzle. part2( ) ) ;
269265}
0 commit comments