Loadeksdi's 2022 Advent of Code writeups #2

Topic

The topic of the challenge can be found here

My solution

use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::collections::HashMap;


fn main() {
    // Insert each possible rock paper scissors score into a hashmap
    let mut scores = HashMap::new();
    // Part 1
    /*
    scores.insert(String::from("A X"), 3);
    scores.insert(String::from("A Y"), 4);
    scores.insert(String::from("A Z"), 8);
    scores.insert(String::from("B X"), 1);
    scores.insert(String::from("B Y"), 5);
    scores.insert(String::from("B Z"), 9);
    scores.insert(String::from("C X"), 2);
    scores.insert(String::from("C Y"), 6);
    scores.insert(String::from("C Z"), 7);
    */
    scores.insert(String::from("A X"), 3);
    scores.insert(String::from("A Y"), 4);
    scores.insert(String::from("A Z"), 8);
    scores.insert(String::from("B X"), 1);
    scores.insert(String::from("B Y"), 5);
    scores.insert(String::from("B Z"), 9);
    scores.insert(String::from("C X"), 2);
    scores.insert(String::from("C Y"), 6);
    scores.insert(String::from("C Z"), 7);
    let mut total_score: i32 = 0;
    // Iterate on each line of the file
    if let Ok(lines) = read_lines("input.txt") {
        for line in lines {
            if let Ok(game) = line {
               total_score += scores[&game]; 
            }
        }
    }
    println!("Total score: {}", total_score);
}

// Read input file, taken from rust-by-example
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())
}
Previous challenge
Next challenge