-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
95 lines (87 loc) · 2.6 KB
/
main.rs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::fs;
use std::str;
/*
A for Rock, B for Paper, and C for Scissors.
X for Rock, Y for Paper, and Z for Scissors.
the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors)
*/
fn get_outcome_score(your_move:&str, opponnent_move:&str)-> u32 {
match your_move {
"X" => {
match opponnent_move {
"C" => return 6,
"A" => return 3,
_ => return 0
}
}
"Y" => {
match opponnent_move {
"A" => return 6,
"B" => return 3,
_ => return 0
}
}
"Z" => {
match opponnent_move {
"B" => return 6,
"C" => return 3,
_ => return 0
}
}
_ => return 0
};
}
fn get_shape_score(your_move:&str)->u32 {
match your_move {
"X" => return 1,
"Y" => return 2,
"Z" => return 3,
_ => return 0
}
}
fn get_score(your_move:&str, opponent_move:&str) -> u32{
return get_outcome_score(your_move, opponent_move) + get_shape_score(your_move);
}
// X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win
fn get_your_move(your_outcome:&str, opponent_move:&str) -> &'static str{
match your_outcome {
"Z" => { // win
match opponent_move {
"A" => return "Y",
"B" => return "Z",
_ => return "X"
}
}
"Y" => { // draw
match opponent_move {
"A" => return "X",
"B" => return "Y",
_ => return "Z"
}
}
"X" => { // lose
match opponent_move {
"A" => return "Z",
"B" => return "X",
_ => return "Y"
}
}
_ => return "K"
};
}
fn main() {
// read file into a string
let input: String = fs::read_to_string("input.txt")
.expect("Something went wrong reading the file");
let mut total_score: u32 = 0;
input.lines().for_each(|line| {
let moves: Vec<&str> = line.split(" ").collect();
let opponent_move = moves.get(0).unwrap();
let your_outcome = moves.get(1).unwrap();
let your_move = get_your_move(your_outcome, opponent_move);
println!("The move is {:?} against {:?} for the outcome {:?}", your_move, opponent_move, your_outcome);
//println!("move is {:?} {:?} for score: {:?}", your_move, opponent_move, score);
total_score += get_score(your_move, opponent_move);
});
println!("The total score sum is {:?}", total_score);
}