Skip to content

Commit ac29c05

Browse files
committed
refactor: Optimise chain of map-filter-reduce steps
1 parent 1f89847 commit ac29c05

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/day06.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ pub fn solve_part2(input: &str) -> i32 {
99
let map = parse_map(input);
1010
let path_to_walk = scrape_map_for_guard(&map).1;
1111

12-
let mut map_variants: Vec<Vec<Vec<char>>> = vec![];
13-
for (x, y) in path_to_walk {
14-
let c = map[y as usize][x as usize];
15-
if c != '^' || c != 'v' || c != '<' || c != '>' {
16-
let mut map_variant = map.clone();
17-
map_variant[y as usize][x as usize] = '#';
18-
map_variants.push(map_variant);
19-
}
20-
}
21-
22-
map_variants
12+
path_to_walk
2313
.iter()
24-
.filter(|map_variant| scrape_map_for_guard(&map_variant.to_vec()).2)
14+
.filter_map(|(x, y)| {
15+
let c = map[*y as usize][*x as usize];
16+
if c != '^' || c != 'v' || c != '<' || c != '>' {
17+
let mut map_variant = map.clone();
18+
map_variant[*y as usize][*x as usize] = '#';
19+
return Some(map_variant);
20+
}
21+
None
22+
})
23+
.filter(|map_variant| scrape_map_for_guard(map_variant).2)
2524
.count() as i32
2625
}
2726

0 commit comments

Comments
 (0)