You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
1.9 KiB

use std::error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
#[derive(PartialEq)]
enum Values {
Rock,
Paper,
Scissors,
}
impl Values {
fn get_from_str(v: &str) -> Values {
match v {
"A" => Values::Rock,
"B" => Values::Paper,
"C" => Values::Scissors,
_ => panic!("Unexpected value for Values")
}
}
fn get_u32(&self) -> u32 {
match self {
Values::Rock => 1,
Values::Paper => 2,
Values::Scissors => 3,
}
}
fn get_other(&self, play_result: &PlayResult) -> Values {
match self {
Values::Rock => {
match play_result {
PlayResult::Lose => Values::Scissors,
PlayResult::Draw => Values::Rock,
PlayResult::Win => Values::Paper
}
},
Values::Paper => {
match play_result {
PlayResult::Lose => Values::Rock,
PlayResult::Draw => Values::Paper,
PlayResult::Win => Values::Scissors
}
},
Values::Scissors => {
match play_result {
PlayResult::Lose => Values::Paper,
PlayResult::Draw => Values::Scissors,
PlayResult::Win => Values::Rock,
}
},
}
}
}
enum PlayResult {
Lose,
Draw,
Win,
}
impl PlayResult {
fn get_from_str(v: &str) -> PlayResult {
match v {
"X" => PlayResult::Lose,
"Y" => PlayResult::Draw,
"Z" => PlayResult::Win,
_ => panic!("Unexpected value for PlayResult")
}
}
fn get_u32(&self) -> u32 {
match self {
PlayResult::Lose => 0,
PlayResult::Draw => 3,
PlayResult::Win => 6
}
}
}
fn main() -> Result<(), Box<dyn error::Error>> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut sum: u32 = 0;
for line in reader.lines() {
let line = line?;
let split_result = line.split_whitespace().collect::<Vec<&str>>();
let other = Values::get_from_str(split_result[0]);
let play_result = PlayResult::get_from_str(split_result[1]);
let my = other.get_other(&play_result);
sum += my.get_u32();
sum += play_result.get_u32();
}
println!("{}", sum);
Ok(())
}