Day two part 1

This commit is contained in:
Yash Karandikar 2022-12-02 09:12:07 -06:00
parent 93936f936c
commit 6a8371e733
2 changed files with 2550 additions and 0 deletions

50
two.rs Normal file
View 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';
}

2500
two.txt Normal file

File diff suppressed because it is too large Load diff