diff --git a/two.rs b/two.rs index 428345a..4c7482a 100644 --- a/two.rs +++ b/two.rs @@ -15,36 +15,50 @@ fn main() { } let chars = s.chars().collect::>(); let opponent = chars[0]; - let choice = chars[2]; + let result = chars[2]; - total += match choice { - 'X' => SCORE_ROCK, - 'Y' => SCORE_PAPER, - 'Z' => SCORE_SCISSORS, + let choice = match result { + 'X' => will_lose(opponent), + 'Y' => will_draw(opponent), + 'Z' => will_win(opponent), + _ => continue, + }; + + total += match result { + 'Y' => SCORE_DRAW, + 'Z' => SCORE_WIN, _ => 0, }; - if draw(choice, opponent) { - total += SCORE_DRAW; - continue; - } - - if won(choice, opponent) { - total += SCORE_WIN; - } + total += match choice { + 'A' => SCORE_ROCK, + 'B' => SCORE_PAPER, + 'C' => SCORE_SCISSORS, + _ => 0, + }; } println!("{}", total); } -fn won(choice: char, opponent: char) -> bool { - return choice == 'X' && opponent == 'C' - || choice == 'Y' && opponent == 'A' - || choice == 'Z' && opponent == 'B'; +fn will_win(opponent: char) -> char { + match opponent { + 'A' => 'B', + 'B' => 'C', + 'C' => 'A', + _ => 'X', + } } -fn draw(choice: char, opponent: char) -> bool { - return choice == 'X' && opponent == 'A' - || choice == 'Y' && opponent == 'B' - || choice == 'Z' && opponent == 'C'; +fn will_draw(opponent: char) -> char { + opponent +} + +fn will_lose(opponent: char) -> char { + match opponent { + 'A' => 'C', + 'B' => 'A', + 'C' => 'B', + _ => 'X', + } }