Improve hand creation

This commit is contained in:
max.nuding 2022-12-02 07:48:10 +01:00
parent be1cd454fe
commit a96624e0a0
Failed to extract signature

View File

@ -5,6 +5,12 @@ struct Hand {
pub letter: char pub letter: char
} }
impl From<&str> for Hand {
fn from(s: &str) -> Self {
Self { letter: s.0.chars().next().unwrap() }
}
}
impl Hand { impl Hand {
pub fn score(&self) -> i32 { pub fn score(&self) -> i32 {
match self.letter { match self.letter {
@ -77,12 +83,14 @@ pub fn run() {
.lines() .lines()
.map(|l| { .map(|l| {
let hands = l let hands = l
.split(' ') .split_once(' ')
.map(|s|Hand { letter: s.chars().next().unwrap() }) .map(|c|
.collect::<Vec<_>>(); (c.0.into(),
c.1.into())
).unwrap();
Round { Round {
enemy: hands[0].clone(), enemy: hands.0,
me: hands[1].clone(), me: hands.1,
} }
}).map(|r|(r.score(), r.score_b())) }).map(|r|(r.score(), r.score_b()))
.fold((0, 0), |agg, cur|(agg.0 + cur.0, agg.1 + cur.1)); .fold((0, 0), |agg, cur|(agg.0 + cur.0, agg.1 + cur.1));