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;
#[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::<Vec<_>>();
let rounds = input
.lines()
.map(|l| {
let hands = l
.split(' ')
.map(|s|Hand { letter: s.chars().next().unwrap() })
.collect::<Vec<_>>();
Round {
enemy: Hand {
letter: hands.first().unwrap().chars().next().unwrap(),
},
me: Hand {
letter: hands[1].chars().next().unwrap(),
},
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")]
{
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);
}
}