diff options
Diffstat (limited to '02/src/part-2.rs')
-rw-r--r-- | 02/src/part-2.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/02/src/part-2.rs b/02/src/part-2.rs new file mode 100644 index 0000000..2c6e146 --- /dev/null +++ b/02/src/part-2.rs @@ -0,0 +1,74 @@ +// TODO: This should of course all be table lookups instead of nested switches. + +use std::io::{BufRead}; + +fn main() { + let stdin = std::io::stdin(); + let handle = stdin.lock(); + + let rock: usize = 1; + let paper: usize = 2; + let scissors: usize = 3; + let lose: usize = 0; + let draw: usize = 3; + let win: usize = 6; + + let mut score: usize = 0; + + for l in handle.lines() { + let line = l.unwrap(); + let mut chars = line.chars(); + let opponent_move = chars.next().unwrap(); + assert_eq!(chars.next().unwrap(), ' '); + let my_move = chars.next().unwrap(); + + score += match opponent_move { + 'A' => { // Opponent plays rock. + match my_move { + 'X' => { // We need to lose, play scissors. + lose + scissors + }, + 'Y' => { // We need to draw, play rock. + draw + rock + }, + 'Z' => { // We need to win, play paper + win + paper + }, + _ => panic!("Unreachable") + } + }, + 'B' => { // Opponent plays paper. + match my_move { + 'X' => { // We need to lose, play rock. + lose + rock + }, + 'Y' => { // We need to draw, play paper. + draw + paper + }, + 'Z' => { // We need to win, play scissors + win + scissors + }, + _ => panic!("Unreachable") + } + }, + 'C' => { // Opponent plays scissors. + match my_move { + 'X' => { // We need to lose, play paper. + lose + paper + }, + 'Y' => { // We need to draw, play scissors. + draw + scissors + }, + 'Z' => { // We need to win, play rock + win + rock + }, + _ => panic!("Unreachable") + } + }, + _ => panic!("Unreachable") + }; + } + + println!("Total score: {}", score); + +} |