This repository has been archived on 2023-12-13. You can view files and clone it, but cannot push or open issues or pull requests.
advent-of-code-2022/day2/src/main.rs

147 lines
3.4 KiB
Rust
Raw Normal View History

2022-12-02 11:35:33 -05:00
#[derive(Debug, Copy, Clone)]
enum Calculation {
ByMove,
ByOutcome,
}
use Calculation::*;
// ----------------------------------------------------------------------------
#[derive(Debug, Copy, Clone)]
enum Outcome {
Win,
Draw,
Lose,
}
use Outcome::*;
impl From<char> for Outcome {
fn from(c: char) -> Self {
match c {
'X' => Lose,
'Y' => Draw,
'Z' => Win,
_ => panic!("Invalid char: {}", c),
}
}
}
impl Outcome {
fn get_score(self) -> u32 {
match self {
Lose => 0,
Draw => 3,
Win => 6,
}
}
}
// ----------------------------------------------------------------------------
2022-12-02 10:55:22 -05:00
#[derive(Debug, Copy, Clone)]
enum Shape {
Rock,
Paper,
Scissors,
}
use Shape::*;
impl From<char> for Shape {
fn from(c: char) -> Self {
match c {
'A' | 'X' => Rock,
'B' | 'Y' => Paper,
'C' | 'Z' => Scissors,
_ => panic!("Invalid char: {}", c),
}
}
}
impl Shape {
2022-12-02 11:35:33 -05:00
fn get_score(self) -> u32 {
2022-12-02 10:55:22 -05:00
match self {
Rock => 1,
Paper => 2,
Scissors => 3,
}
}
2022-12-02 11:35:33 -05:00
fn get_outcome(self, outcome: char) -> Shape {
let outcome = Outcome::from(outcome);
match self {
Rock => match outcome {
Win => Paper,
Draw => Rock,
Lose => Scissors,
},
Paper => match outcome {
Win => Scissors,
Draw => Paper,
Lose => Rock,
},
Scissors => match outcome {
Win => Rock,
Draw => Scissors,
Lose => Paper,
},
}
}
2022-12-02 10:55:22 -05:00
}
2022-12-02 11:35:33 -05:00
// ----------------------------------------------------------------------------
2022-12-02 10:55:22 -05:00
fn get_outcome_score(them: Shape, you: Shape) -> u32 {
match (them, you) {
2022-12-02 11:35:33 -05:00
(Rock, Scissors) | (Paper, Rock) | (Scissors, Paper) => Lose,
2022-12-02 10:55:22 -05:00
2022-12-02 11:35:33 -05:00
(Rock, Rock) | (Paper, Paper) | (Scissors, Scissors) => Draw,
2022-12-02 10:55:22 -05:00
2022-12-02 11:35:33 -05:00
(Rock, Paper) | (Paper, Scissors) | (Scissors, Rock) => Win,
2022-12-02 10:55:22 -05:00
}
2022-12-02 11:35:33 -05:00
.get_score()
2022-12-02 10:55:22 -05:00
}
2022-12-02 11:35:33 -05:00
fn get_round_score(them: char, you: char, score_type: Calculation) -> u32 {
2022-12-02 10:55:22 -05:00
let them = Shape::from(them);
2022-12-02 11:35:33 -05:00
let you = match score_type {
ByMove => Shape::from(you),
ByOutcome => them.get_outcome(you),
};
2022-12-02 10:55:22 -05:00
let shape_score = you.get_score();
let outcome_score = get_outcome_score(them, you);
shape_score + outcome_score
}
2022-12-02 11:35:33 -05:00
fn get_scores(lines: Vec<&str>, score_type: Calculation) -> Vec<u32> {
2022-12-02 10:55:22 -05:00
lines
.into_iter()
.map(|line| {
// This is trying to be too clever
let [them, _, you]: [char; 3] = line.chars().collect::<Vec<char>>().try_into().unwrap();
2022-12-02 11:35:33 -05:00
get_round_score(them, you, score_type)
2022-12-02 10:55:22 -05:00
})
.collect()
}
2022-12-02 11:35:33 -05:00
fn get_total(lines: &Vec<&str>, score_type: Calculation) -> u32 {
get_scores(lines.clone(), score_type)
.into_iter()
.reduce(|accum, item| accum + item)
.unwrap()
}
2022-12-03 16:07:06 -05:00
fn main() {
let file_str = include_str!("input.txt");
2022-12-02 10:55:22 -05:00
let lines: Vec<&str> = file_str.lines().collect();
2022-12-02 11:35:33 -05:00
let part1_total = get_total(&lines, ByMove);
let part2_total = get_total(&lines, ByOutcome);
println!("Part 1: Final score: {}", part1_total);
println!("Part 2: Final score: {}", part2_total);
2022-12-02 10:55:22 -05:00
}