Day two part 1
This commit is contained in:
parent
93936f936c
commit
6a8371e733
50
two.rs
Normal file
50
two.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
const SCORE_WIN: u32 = 6;
|
||||
const SCORE_DRAW: u32 = 3;
|
||||
|
||||
const SCORE_ROCK: u32 = 1;
|
||||
const SCORE_PAPER: u32 = 2;
|
||||
const SCORE_SCISSORS: u32 = 3;
|
||||
|
||||
fn main() {
|
||||
let buf = std::fs::read_to_string("two.txt").unwrap();
|
||||
let mut total = 0u32;
|
||||
|
||||
for s in buf.split("\n") {
|
||||
if s.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let chars = s.chars().collect::<Vec<_>>();
|
||||
let opponent = chars[0];
|
||||
let choice = chars[2];
|
||||
|
||||
total += match choice {
|
||||
'X' => SCORE_ROCK,
|
||||
'Y' => SCORE_PAPER,
|
||||
'Z' => SCORE_SCISSORS,
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
if draw(choice, opponent) {
|
||||
total += SCORE_DRAW;
|
||||
continue;
|
||||
}
|
||||
|
||||
if won(choice, opponent) {
|
||||
total += SCORE_WIN;
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", total);
|
||||
}
|
||||
|
||||
fn won(choice: char, opponent: char) -> bool {
|
||||
return choice == 'X' && opponent == 'C'
|
||||
|| choice == 'Y' && opponent == 'A'
|
||||
|| choice == 'Z' && opponent == 'B';
|
||||
}
|
||||
|
||||
fn draw(choice: char, opponent: char) -> bool {
|
||||
return choice == 'X' && opponent == 'A'
|
||||
|| choice == 'Y' && opponent == 'B'
|
||||
|| choice == 'Z' && opponent == 'C';
|
||||
}
|
Loading…
Reference in a new issue