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.
70 lines
1.3 KiB
70 lines
1.3 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,
|
|
"X" => Values::Rock,
|
|
"Y" => Values::Paper,
|
|
"Z" => Values::Scissors,
|
|
_ => panic!("Unexpected value for Values")
|
|
}
|
|
}
|
|
|
|
fn get_u32(&self) -> u32 {
|
|
match self {
|
|
Values::Rock => 1,
|
|
Values::Paper => 2,
|
|
Values::Scissors => 3
|
|
}
|
|
}
|
|
|
|
fn beats(self, other: Values) -> bool {
|
|
if self == Values::Rock {
|
|
other == Values::Scissors
|
|
} else if self == Values::Paper {
|
|
other == Values::Rock
|
|
} else if self == Values::Scissors {
|
|
other == Values::Paper
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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 mut split_result = line.split_whitespace().map(|o| Values::get_from_str(o));
|
|
let other = split_result.next().unwrap();
|
|
let my = split_result.next().unwrap();
|
|
sum += my.get_u32();
|
|
if other == my {
|
|
sum += 3;
|
|
} else if my.beats(other) {
|
|
sum += 6;
|
|
}
|
|
}
|
|
|
|
println!("{}", sum);
|
|
Ok(())
|
|
}
|