refactors

This commit is contained in:
JasterV 2021-12-10 00:09:03 +01:00
parent d6508e1ae8
commit 63a56cd97e

View file

@ -9,20 +9,23 @@ use std::{
static INPUT_PATH: &str = "input.txt";
fn main() -> Result<()> {
let overlapping_points_count = read_lines(INPUT_PATH)?
.filter_map(|line| line.ok().map(Line::from))
let overlapping_points_count = read_input(INPUT_PATH)?
.flat_map(|line| line.get_points())
.fold(HashMap::new(), |mut counters, point| {
counters.insert(point, counters.get(&point).map_or(1, |count| count + 1));
counters
})
.values()
.filter(|&&count| count >= 2)
.into_values()
.filter(|&count| count >= 2)
.count();
println!("First puzzle: {}", overlapping_points_count);
Ok(())
}
fn read_input(filename: &str) -> Result<impl Iterator<Item = Line>> {
Ok(read_lines(filename)?.filter_map(|line| line.ok().map(Line::from)))
}
// The output is wrapped in a Result to allow matching on errors
// Returns an Iterator to the Reader of the lines of the file.
fn read_lines<P>(filename: P) -> Result<Lines<BufReader<File>>>