From be1cd454fe72b716fc0df41062f9262b7d7e211b Mon Sep 17 00:00:00 2001 From: "max.nuding" Date: Fri, 2 Dec 2022 07:28:36 +0100 Subject: [PATCH] Cleanup iteration --- src/day02/mod.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/day02/mod.rs b/src/day02/mod.rs index cb39c04..48d9b41 100644 --- a/src/day02/mod.rs +++ b/src/day02/mod.rs @@ -1,5 +1,6 @@ use crate::read; +#[derive(Clone)] struct Hand { pub letter: char } @@ -72,34 +73,27 @@ impl Round { pub fn run() { let input = read("02"); - let rounds = input.lines().map(|l| { - let hands = l.split(' ').collect::>(); - Round { - enemy: Hand { - letter: hands.first().unwrap().chars().next().unwrap(), - }, - me: Hand { - letter: hands[1].chars().next().unwrap(), - }, - } - } - ).collect::>(); + let rounds = input + .lines() + .map(|l| { + let hands = l + .split(' ') + .map(|s|Hand { letter: s.chars().next().unwrap() }) + .collect::>(); + Round { + enemy: hands[0].clone(), + me: hands[1].clone(), + } + }).map(|r|(r.score(), r.score_b())) + .fold((0, 0), |agg, cur|(agg.0 + cur.0, agg.1 + cur.1)); #[cfg(feature = "part1")] { - let a: i32 = rounds - .iter() - .map(|r|r.score()) - .sum(); - eprintln!("Day 2, Part 01: {}", a); + eprintln!("Day 2, Part 01: {}", rounds.0); } #[cfg(feature = "part2")] { - let b: i32 = rounds - .iter() - .map(|r|r.score_b()) - .sum(); - eprintln!("Day 2, Part 02: {}", b); + eprintln!("Day 2, Part 02: {}", rounds.1); } }