Advent of Code 2022
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.

45 lines
1.5 KiB

use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::path::PathBuf;
use clap::Parser;
#[derive(Parser)]
struct Cli {
filename: PathBuf,
}
fn main() {
let cli = Cli::parse();
let file = File::open(cli.filename).expect("Can't read file");
let reader = BufReader::new(file);
let mut score = 0;
for line in reader.lines() {
let line = line.unwrap();
if line.is_empty() {
continue;
}
let mut play = line.trim().split(' ');
let their_play = play.next().unwrap();
let play_result = play.next().unwrap();
// A = Rock (1), B = Paper (2), C = Scissors (3)
// X = Lose (0), Y = Draw (3), Z = Win (6)
score += match (their_play, play_result) {
("A", "X") => 0 + 3, // they play rock, I lose, I played scissors
("B", "X") => 0 + 1, // they play paper, I lose, I played rock
("C", "X") => 0 + 2, // they play scissors, I lose, I played paper
("A", "Y") => 3 + 1, // they play rock, we draw, I played rock
("B", "Y") => 3 + 2, // they play paper, we draw, I played paper
("C", "Y") => 3 + 3, // they play scissors, we draw, I played scissors
("A", "Z") => 6 + 2, // they play rock, I win, I played paper
("B", "Z") => 6 + 3, // they played paper, I win, I played scissors
("C", "Z") => 6 + 1, // they played sicssors, I win, I played rock
_ => 0,
};
}
println!("Your score; {}", score);
}