From cd0c64b1528e02db8bad10ebf24037769959b0c3 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Tue, 7 Dec 2021 22:38:36 -0700 Subject: [PATCH] small tweak (convers sum() to fold()) --- src/day05.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/day05.rs b/src/day05.rs index 9a501c3..4aa1136 100644 --- a/src/day05.rs +++ b/src/day05.rs @@ -77,19 +77,16 @@ fn count_overlapped_cells(segments: &[LineSegment], includ_diag: bool) -> usize segments .iter() .filter(|s| s.is_horizontal_or_vertical() || includ_diag) - .map(|s| { - s.rasterize() - .map(|(x, y)| -> usize { - cells[x as usize][y as usize] += 1; - if cells[x as usize][y as usize] == 2 { - 1 - } else { - 0 - } - }) - .sum::() + .fold(0, |acc, s| { + acc + s.rasterize().fold(0, |acc, (x, y)| -> usize { + cells[x as usize][y as usize] += 1; + if cells[x as usize][y as usize] == 2 { + acc + 1 + } else { + acc + } + }) }) - .sum::() } pub(crate) fn run() -> Result<(usize, usize)> {