Refactor grouping count into a trait.

This commit is contained in:
2018-12-06 16:19:52 +01:00
parent d53100e591
commit c44ebaa238
4 changed files with 54 additions and 27 deletions

View File

@@ -3,9 +3,11 @@ use std::io;
use std::io::BufRead;
use std::ops::Range;
use common;
use regex;
use common;
use common::GroupingCount;
#[derive(Copy, Clone, Debug)]
struct Claim {
x: usize,
@@ -60,15 +62,10 @@ impl Day03 {
}
}
fn get_claims(&self) -> HashMap<(usize, usize), i32> {
let mut claims = HashMap::new();
for claim in &self.claims {
for coordinate in claim.range() {
*claims.entry(coordinate).or_insert(0) += 1;
}
}
claims
fn get_claims(&self) -> HashMap<(usize, usize), usize> {
self.claims.iter()
.flat_map(|x| x.range())
.grouping_count()
}
}