Part 1 done!

Part 2 done!
This commit is contained in:
Daniel Flanagan 2022-12-02 10:57:33 -06:00
parent 39c904e8aa
commit c907c24321
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -1,12 +1,64 @@
use std::cmp::{Ord, Ordering, PartialEq};
use std::str::FromStr;
mod common; mod common;
// A -> Rock #[derive(Debug, PartialEq, Eq)]
// B -> Paper enum Choice {
// C -> Scissors Rock = 1,
Paper = 2,
Scissors = 3,
}
// X -> Rock -> 1 impl PartialOrd for Choice {
// Y -> Paper -> 2 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
// Z -> Scissors -> 3 Some(self.cmp(other))
}
}
impl Ord for Choice {
fn cmp(&self, other: &Self) -> Ordering {
match self {
Choice::Rock => match other {
Choice::Rock => Ordering::Equal,
Choice::Paper => Ordering::Less,
Choice::Scissors => Ordering::Greater,
},
Choice::Paper => match other {
Choice::Rock => Ordering::Greater,
Choice::Paper => Ordering::Equal,
Choice::Scissors => Ordering::Less,
},
Choice::Scissors => match other {
Choice::Rock => Ordering::Less,
Choice::Paper => Ordering::Greater,
Choice::Scissors => Ordering::Equal,
},
}
}
}
#[derive(Debug)]
struct ParseChoiceError;
impl std::fmt::Display for ParseChoiceError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl FromStr for Choice {
type Err = ParseChoiceError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.chars().nth(0) {
Some('X') | Some('A') => Ok(Choice::Rock),
Some('Y') | Some('B') => Ok(Choice::Paper),
Some('Z') | Some('C') => Ok(Choice::Scissors),
_ => Err(ParseChoiceError),
}
}
}
fn main() { fn main() {
let input = common::day_input(2); let input = common::day_input(2);
@ -14,84 +66,68 @@ fn main() {
println!("Part 2: {}", part2(&input)); println!("Part 2: {}", part2(&input));
} }
fn rps(a: char, b: char) -> i32 { fn score_part1(opponent: &Choice, mine: &Choice) -> i32 {
match b { (*mine as i32)
'X' => { + match mine.cmp(opponent) {
1 + match a { Ordering::Less => 0,
'A' => 3, Ordering::Equal => 3,
'B' => 0, Ordering::Greater => 6,
'C' => 6,
_ => 1000000,
}
} }
'Y' => {
2 + match a {
'A' => 6,
'B' => 3,
'C' => 0,
_ => 1000000,
}
}
'Z' => {
3 + match a {
'A' => 0,
'B' => 6,
'C' => 3,
_ => 1000000,
}
}
_ => 1000000,
}
} }
fn part1(input: &str) -> i32 { fn part1(input: &str) -> i32 {
input input.lines().fold(0, |points, line| {
.lines() points
.map(|s| { + score_part1(
let mut c = s.chars(); &line[0..1].parse::<Choice>().unwrap(),
rps(c.nth(0).unwrap(), c.nth(1).unwrap()) &line[2..3].parse::<Choice>().unwrap(),
}) )
.sum() })
} }
fn rps2(a: char, b: char) -> i32 { #[derive(Debug)]
match b { struct ParseOrderingError;
'X' => {
0 + match a { fn desired_outcome(s: &str) -> Result<Ordering, ParseOrderingError> {
'A' => 3, match s.chars().nth(0) {
'B' => 1, Some('X') => Ok(Ordering::Less),
'C' => 2, Some('Y') => Ok(Ordering::Equal),
_ => 1000000, Some('Z') => Ok(Ordering::Greater),
} _ => Err(ParseOrderingError),
}
'Y' => {
3 + match a {
'A' => 1,
'B' => 2,
'C' => 3,
_ => 1000000,
}
}
'Z' => {
6 + match a {
'A' => 2,
'B' => 3,
'C' => 1,
_ => 1000000,
}
}
_ => 1000000,
} }
} }
fn outcome_choice(opponent: &Choice, desired_outcome: Ordering) -> Choice {
match opponent {
Choice::Rock => match desired_outcome {
Ordering::Equal => Choice::Rock,
Ordering::Greater => Choice::Paper,
Ordering::Less => Choice::Scissors,
},
Choice::Paper => match desired_outcome {
Ordering::Less => Choice::Rock,
Ordering::Equal => Choice::Paper,
Ordering::Greater => Choice::Scissors,
},
Choice::Scissors => match desired_outcome {
Ordering::Greater => Choice::Rock,
Ordering::Less => Choice::Paper,
Ordering::Equal => Choice::Scissors,
},
}
}
fn score_part2(opponent: &Choice, outcome: &str) -> i32 {
score_part1(
&opponent,
&outcome_choice(&opponent, desired_outcome(outcome).unwrap()),
)
}
fn part2(input: &str) -> i32 { fn part2(input: &str) -> i32 {
input input.lines().fold(0, |points, line| {
.lines() points + score_part2(&line[0..1].parse::<Choice>().unwrap(), &line[2..3])
.map(|s| { })
let mut c = s.chars();
rps2(c.nth(0).unwrap(), c.nth(1).unwrap())
})
.sum()
} }
#[cfg(test)] #[cfg(test)]