2021 day5/rust: dedup using Vec matrix instead of HashMap
This commit is contained in:
parent
4ee0046b97
commit
83b9b79a28
1 changed files with 21 additions and 9 deletions
|
@ -1,6 +1,5 @@
|
|||
#![warn(clippy::pedantic)]
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
convert::Infallible,
|
||||
io::{stdin, BufRead},
|
||||
str::FromStr,
|
||||
|
@ -58,14 +57,27 @@ impl FromStr for Line {
|
|||
}
|
||||
|
||||
fn count_overlaps<'a>(it: impl IntoIterator<Item = &'a Line>) -> usize {
|
||||
let counts: HashMap<(usize, usize), usize> =
|
||||
it.into_iter()
|
||||
let points: Vec<(usize, usize)> = it
|
||||
.into_iter()
|
||||
.flat_map(Line::expand)
|
||||
.fold(HashMap::new(), |mut map, point| {
|
||||
*map.entry(point.into()).or_default() += 1;
|
||||
map
|
||||
.map(Into::into)
|
||||
.collect();
|
||||
|
||||
let (max_x, max_y) = points.iter().fold((0, 0), |(acc_x, acc_y), &(x, y)| {
|
||||
(usize::max(acc_x, x), usize::max(acc_y, y))
|
||||
});
|
||||
counts.into_iter().filter(|&(_k, v)| v >= 2).count()
|
||||
|
||||
let mut field = vec![vec![0_usize; max_y + 1]; max_x + 1];
|
||||
|
||||
let mut hits = 0;
|
||||
for (x, y) in points {
|
||||
let point = &mut field[x][y];
|
||||
if *point == 1 {
|
||||
hits += 1;
|
||||
}
|
||||
*point += 1;
|
||||
}
|
||||
hits
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
Loading…
Reference in a new issue