From a96624e0a0ba309524287b21f74f54700013afda Mon Sep 17 00:00:00 2001 From: "max.nuding" Date: Fri, 2 Dec 2022 07:48:10 +0100 Subject: [PATCH] Improve hand creation --- src/day02/mod.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/day02/mod.rs b/src/day02/mod.rs index 48d9b41..861a37c 100644 --- a/src/day02/mod.rs +++ b/src/day02/mod.rs @@ -5,6 +5,12 @@ struct Hand { pub letter: char } +impl From<&str> for Hand { + fn from(s: &str) -> Self { + Self { letter: s.0.chars().next().unwrap() } + } +} + impl Hand { pub fn score(&self) -> i32 { match self.letter { @@ -77,12 +83,14 @@ pub fn run() { .lines() .map(|l| { let hands = l - .split(' ') - .map(|s|Hand { letter: s.chars().next().unwrap() }) - .collect::>(); + .split_once(' ') + .map(|c| + (c.0.into(), + c.1.into()) + ).unwrap(); Round { - enemy: hands[0].clone(), - me: hands[1].clone(), + enemy: hands.0, + me: hands.1, } }).map(|r|(r.score(), r.score_b())) .fold((0, 0), |agg, cur|(agg.0 + cur.0, agg.1 + cur.1));