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 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',
}
}