Add part1 of day02
This commit is contained in:
parent
e66d90983e
commit
60f4e24250
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day02"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day02"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,69 @@
|
||||||
|
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 split_result: Vec<&str> = line.split_whitespace().collect();
|
||||||
|
let other = Values::get_from_str(&split_result[0]);
|
||||||
|
let my = Values::get_from_str(&split_result[1]);
|
||||||
|
sum += my.get_u32();
|
||||||
|
if other == my {
|
||||||
|
sum += 3;
|
||||||
|
} else if my.beats(other) {
|
||||||
|
sum += 6;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", sum);
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue