Day 2 part 2

This commit is contained in:
Yash Karandikar 2022-12-03 11:51:33 -06:00
parent 6a8371e733
commit f2a75a143a

56
two.rs
View file

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