summaryrefslogtreecommitdiff
path: root/02/src/part-2.rs
blob: 2c6e14627ec2d0b3b7561be745d7c1624d8d8570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
    
}