@@ -38,7 +38,7 @@ fn adjacent(x: usize, y: usize) -> Vec<(usize, usize)> {
38
38
vec ! [ ( x, y - 1 ) , ( x - 1 , y) , ( x + 1 , y) , ( x, y + 1 ) ]
39
39
}
40
40
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 > > {
42
42
let mut target_mapping: HashMap < ( usize , usize ) , Vec < usize > > = HashMap :: new ( ) ;
43
43
44
44
for ( i, t) in targets. iter ( ) . enumerate ( ) {
@@ -192,25 +192,24 @@ impl Puzzle {
192
192
continue ;
193
193
}
194
194
195
- let target_mapping = parse_target ( & units, units[ u] . race ) ;
195
+ let opponents = get_opponents ( & units, units[ u] . race ) ;
196
196
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
199
199
200
- let target_adj: HashSet < ( usize , usize ) > =
201
- target_mapping. keys ( ) . copied ( ) . collect ( ) ;
200
+ let target_adj: HashSet < ( usize , usize ) > = opponents. keys ( ) . copied ( ) . collect ( ) ;
202
201
203
202
if let Some ( xy) = next_pos ( u, & units, & target_adj, wall) {
204
203
units[ u] . x = xy. 0 ;
205
204
units[ u] . y = xy. 1 ;
206
205
}
207
206
}
208
207
209
- if target_mapping . contains_key ( & ( units[ u] . x , units[ u] . y ) ) {
208
+ if opponents . contains_key ( & ( units[ u] . x , units[ u] . y ) ) {
210
209
// attack
211
210
let damage = units[ u] . attack_power ;
212
211
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 ( ) ;
214
213
215
214
let & target_index = target_indices
216
215
. iter ( )
@@ -261,9 +260,6 @@ fn main() {
261
260
let args = aoc:: parse_args ( ) ;
262
261
let mut puzzle = Puzzle :: new ( ) ;
263
262
puzzle. configure ( args. path . as_str ( ) ) ;
264
-
265
- // print!("\x1b[H\x1b[2J\x1b]1337;ClearScrollback\x07");
266
-
267
263
println ! ( "{}" , puzzle. part1( ) ) ;
268
264
println ! ( "{}" , puzzle. part2( ) ) ;
269
265
}
0 commit comments