Cleanup iteration

This commit is contained in:
max.nuding 2022-12-02 07:28:36 +01:00
parent 38f1b5a037
commit be1cd454fe
Failed to extract signature

View File

@ -1,5 +1,6 @@
use crate::read; use crate::read;
#[derive(Clone)]
struct Hand { struct Hand {
pub letter: char pub letter: char
} }
@ -72,34 +73,27 @@ impl Round {
pub fn run() { pub fn run() {
let input = read("02"); let input = read("02");
let rounds = input.lines().map(|l| { let rounds = input
let hands = l.split(' ').collect::<Vec<_>>(); .lines()
Round { .map(|l| {
enemy: Hand { let hands = l
letter: hands.first().unwrap().chars().next().unwrap(), .split(' ')
}, .map(|s|Hand { letter: s.chars().next().unwrap() })
me: Hand { .collect::<Vec<_>>();
letter: hands[1].chars().next().unwrap(), Round {
}, enemy: hands[0].clone(),
} me: hands[1].clone(),
} }
).collect::<Vec<_>>(); }).map(|r|(r.score(), r.score_b()))
.fold((0, 0), |agg, cur|(agg.0 + cur.0, agg.1 + cur.1));
#[cfg(feature = "part1")] #[cfg(feature = "part1")]
{ {
let a: i32 = rounds eprintln!("Day 2, Part 01: {}", rounds.0);
.iter()
.map(|r|r.score())
.sum();
eprintln!("Day 2, Part 01: {}", a);
} }
#[cfg(feature = "part2")] #[cfg(feature = "part2")]
{ {
let b: i32 = rounds eprintln!("Day 2, Part 02: {}", rounds.1);
.iter()
.map(|r|r.score_b())
.sum();
eprintln!("Day 2, Part 02: {}", b);
} }
} }