90 lines
2.3 KiB
Text
90 lines
2.3 KiB
Text
use std::fs;
|
|
|
|
enum Hand {
|
|
Rock = 1,
|
|
Paper = 2,
|
|
Scissors = 3,
|
|
}
|
|
|
|
enum Outcome { Win, Loss, Draw }
|
|
|
|
struct Round {
|
|
opponent_hand: Hand,
|
|
player_hand: Hand,
|
|
}
|
|
|
|
impl Round {
|
|
fn outcome(&self) -> Outcome {
|
|
match self.player_hand as i8 - self.opponent_hand as i8 {
|
|
0 => Outcome::Draw,
|
|
1 | -2 => Outcome::Win,
|
|
_ => Outcome::Loss,
|
|
}
|
|
}
|
|
fn points(&self) -> u32 {
|
|
self.player_hand as u32 + match self.outcome() {
|
|
Outcome::Loss => 0,
|
|
Outcome::Draw => 3,
|
|
Outcome::Win => 6,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn read_file(path: &str) -> String {
|
|
fs::read_to_string(path)
|
|
.expect("File not Found")
|
|
}
|
|
|
|
fn parse_round(line: &str, strat: u8) -> Round {
|
|
let line = line.as_bytes();
|
|
let opponent_hand = match line[0] {
|
|
b'A' => Hand::Rock,
|
|
b'B' => Hand::Paper,
|
|
b'C' => Hand::Scissors,
|
|
_ => panic!("Unexpected Token"),
|
|
};
|
|
let player_hand = match strat {
|
|
1 => {
|
|
match line[2] {
|
|
b'X' => Hand::Rock,
|
|
b'Y' => Hand::Paper,
|
|
b'Z' => Hand::Scissors,
|
|
_ => panic!("Unexpected Token"),
|
|
}
|
|
},
|
|
2 => {
|
|
match line[2] {
|
|
b'X' => match opponent_hand {
|
|
Hand::Rock => Hand::Scissors,
|
|
Hand::Paper => Hand::Rock,
|
|
Hand::Scissors => Hand::Paper,
|
|
}, // Lose
|
|
b'Y' => opponent_hand, // Draw
|
|
b'Z' => match opponent_hand{
|
|
Hand::Rock => Hand::Paper,
|
|
Hand::Paper => Hand::Scissors,
|
|
Hand::Scissors => Hand::Rock,
|
|
}, // Win
|
|
_ => panic!("Unexpected Token"),
|
|
}
|
|
}
|
|
};
|
|
Round { opponent_hand, player_hand }
|
|
}
|
|
|
|
fn main() {
|
|
let contents = read_file("input");
|
|
|
|
let mut tally1 = 0;
|
|
let mut tally2 = 0;
|
|
for line in contents.lines() {
|
|
if line.len() == 3 {
|
|
let round1 = parse_round(line, 1);
|
|
let round2 = parse_round(line, 2);
|
|
tally1 += round1.points();
|
|
tally2 += round2.points();
|
|
}
|
|
}
|
|
println!("Total Points (Strat 1): {tally1}");
|
|
println!("Total Points (Strat 2): {tally2}");
|
|
}
|