feat: day 2
time: just under 20 mins
This commit is contained in:
parent
789b957854
commit
ca9aa96fb1
3 changed files with 2633 additions and 0 deletions
2500
.aoc-cache/2.txt
Normal file
2500
.aoc-cache/2.txt
Normal file
File diff suppressed because it is too large
Load diff
130
src/bin/day_2.rs
Normal file
130
src/bin/day_2.rs
Normal file
|
@ -0,0 +1,130 @@
|
|||
|
||||
use std::str::FromStr;
|
||||
|
||||
use aoc_2022::prelude::*;
|
||||
use color_eyre::Report;
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
enum Hand {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
impl FromStr for Hand {
|
||||
type Err = Report;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"A" | "X" => Ok(Self::Rock),
|
||||
"B" | "Y" => Ok(Self::Paper),
|
||||
"C" | "Z" => Ok(Self::Scissors),
|
||||
_ => Err(eyre!("invalid move")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hand {
|
||||
fn round(&self, other: Hand) -> RoundOutcome {
|
||||
if *self == other {
|
||||
RoundOutcome::Draw(self.score())
|
||||
} else {
|
||||
match (*self, other) {
|
||||
(Self::Rock, Self::Scissors)
|
||||
| (Self::Paper, Self::Rock)
|
||||
| (Self::Scissors, Self::Paper) => RoundOutcome::Win(self.score()),
|
||||
_ => RoundOutcome::Loss(self.score()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn round_but_other_has_a_different_meaning_not_just_the_opponents_hand_and_self_is_also_the_opponents_hand_and_we_dont_care_about_our_hand(&self, other: Hand) -> RoundOutcome {
|
||||
match other {
|
||||
Hand::Rock => {
|
||||
// need to loose
|
||||
RoundOutcome::Loss(self.would_win_to().score())
|
||||
},
|
||||
Hand::Paper => {
|
||||
// need to draw
|
||||
RoundOutcome::Draw(self.score())
|
||||
},
|
||||
Hand::Scissors => {
|
||||
// need to win
|
||||
RoundOutcome::Win(self.would_loose_to().score())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn score(&self) -> usize {
|
||||
match self {
|
||||
Hand::Rock => 1,
|
||||
Hand::Paper => 2,
|
||||
Hand::Scissors => 3,
|
||||
}
|
||||
}
|
||||
|
||||
fn would_win_to(&self) -> Self {
|
||||
match self {
|
||||
Hand::Rock => Hand::Scissors,
|
||||
Hand::Paper => Hand::Rock,
|
||||
Hand::Scissors => Hand::Paper,
|
||||
}
|
||||
}
|
||||
|
||||
fn would_loose_to(&self) -> Self {
|
||||
match self {
|
||||
Hand::Scissors => Hand::Rock,
|
||||
Hand::Rock => Hand::Paper,
|
||||
Hand::Paper => Hand::Scissors,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum RoundOutcome {
|
||||
Win(usize),
|
||||
Draw(usize),
|
||||
Loss(usize),
|
||||
}
|
||||
|
||||
impl RoundOutcome {
|
||||
fn score(&self) -> usize {
|
||||
match self {
|
||||
RoundOutcome::Win(score) => *score + 6,
|
||||
RoundOutcome::Draw(score) => *score + 3,
|
||||
RoundOutcome::Loss(score) => *score,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Input = Vec<(Hand, Hand)>;
|
||||
|
||||
fn parse(s: &str) -> Result<Input> {
|
||||
let mut games = vec![];
|
||||
for line in s.lines() {
|
||||
let (them, you) = line.split_once(" ").ok_or_else(|| eyre!("no space"))?;
|
||||
games.push((them.parse()?, you.parse()?));
|
||||
}
|
||||
Ok(games)
|
||||
}
|
||||
|
||||
#[aoc(day = 2, parse = parse, test_cases = ["day_2.txt"])]
|
||||
fn day_2(input: Input) -> Result<()> {
|
||||
// Part 1
|
||||
let mut total_score = 0;
|
||||
|
||||
for (them, me) in &input {
|
||||
total_score += me.round(*them).score();
|
||||
}
|
||||
|
||||
println!("Part one: you would score {total_score} in total");
|
||||
|
||||
// Part 2
|
||||
total_score = 0;
|
||||
for (them, me) in &input {
|
||||
total_score += them.round_but_other_has_a_different_meaning_not_just_the_opponents_hand_and_self_is_also_the_opponents_hand_and_we_dont_care_about_our_hand(*me).score();
|
||||
}
|
||||
|
||||
println!("Part two: you would score {total_score} in total");
|
||||
|
||||
Ok(())
|
||||
}
|
3
test_cases/day_2.txt
Normal file
3
test_cases/day_2.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
A Y
|
||||
B X
|
||||
C Z
|
Loading…
Reference in a new issue