mirror of
https://codeberg.org/JasterV/aoc_2021.git
synced 2026-04-26 18:40:05 +00:00
refactors
This commit is contained in:
parent
d6508e1ae8
commit
63a56cd97e
1 changed files with 7 additions and 4 deletions
|
|
@ -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>>>
|
||||
|
|
|
|||
Loading…
Reference in a new issue