.filter(|(_, v)| v.1 && v.0 == '.')
.map(|(k, _)| *k)
.collect();
- beams_pos.sort_by(|a, b| a.cmp(&b));
+ beams_pos.sort();
// keep track of the number of timelines for each visited location
let mut beams_timelines: HashMap<(usize, usize), u64> = self.map.iter()
// compute the number of timelines per line
beams_pos.iter()
.for_each(|pos| {
- let timeline = *beams_timelines.get(&pos).unwrap();
+ let timeline = *beams_timelines.get(pos).unwrap();
let next_pos = (pos.0 + 1, pos.1);
if self.map.contains_key(&next_pos) {
let next_char = self.map.get_mut(&next_pos).unwrap().0; // it exists
- match next_char {
- '.' => {
- let next_timeline = beams_timelines.get_mut(&next_pos).unwrap();
- *next_timeline += timeline;
- },
- '^' => {
- [next_pos.1 - 1, next_pos.1 + 1].into_iter()
- .for_each(|j| {
- let p = (next_pos.0, j);
- let next_timeline = beams_timelines.get_mut(&p).unwrap();
- *next_timeline += timeline;
- });
- },
+ let next_pos = match next_char {
+ '.' => vec![(pos.0 + 1, pos.1)],
+ '^' => vec![(pos.0 + 1, pos.1 - 1), (pos.0 + 1, pos.1 + 1)],
_ => unreachable!(),
- }
+ };
+ next_pos.iter()
+ .for_each(|p| {
+ let next_timeline = beams_timelines.get_mut(p).unwrap();
+ *next_timeline += timeline;
+ });
}
});