advent_of_code_2022/aoc_02/src/main.rs

39 lines
1.1 KiB
Rust

use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() {
let file_path = String::from("input");
let mut result = 0;
println!("In file {}", file_path);
if let Ok(lines) = read_lines(file_path) {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
match ip.as_str() {
"A X" => result += 3 + 0,
"A Y" => result += 1 + 3,
"A Z" => result += 2 + 6,
"B X" => result += 1 + 0,
"B Y" => result += 2 + 3,
"B Z" => result += 3 + 6,
"C X" => result += 2 + 0,
"C Y" => result += 3 + 3,
"C Z" => result += 1 + 6,
_ => result += 0,
}
}
}
}
println!("{}", result);
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}