1
0

Added Day 2 part 2

This commit is contained in:
Ishan Jain 2022-12-02 11:22:09 +05:30
parent 8e83830b1d
commit 468b1c142a
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -3,7 +3,7 @@ const INPUTS: [&str; 2] = [
include_str!("../inputs/input.txt"), include_str!("../inputs/input.txt"),
]; ];
fn parse(input: &'static str) -> Vec<(Move, Move)> { fn parse(input: &'static str) -> Vec<(Move, Outcome)> {
input input
.trim() .trim()
.lines() .lines()
@ -18,14 +18,10 @@ fn parse(input: &'static str) -> Vec<(Move, Move)> {
_ => unreachable!(), _ => unreachable!(),
}; };
let y = match b.trim() { let y = match b.trim() {
"X" => Move::Rock, "X" => Outcome::X,
"Y" => Move::Paper, "Y" => Outcome::Y,
"Z" => Move::Scissors, "Z" => Outcome::Z,
v => { _ => unreachable!(),
println!("{:?}", v);
unreachable!();
}
}; };
(x, y) (x, y)
}) })
@ -39,17 +35,22 @@ enum Move {
Scissors = 3, Scissors = 3,
} }
fn calc_score(m1: Move, m2: Move) -> i32 { #[derive(Clone, Copy)]
match (m1, m2) { enum Outcome {
(Move::Rock, Move::Rock) => 3, X = 0,
(Move::Rock, Move::Paper) => 0, Y = 3,
(Move::Rock, Move::Scissors) => 6, Z = 6,
(Move::Paper, Move::Rock) => 6, }
(Move::Paper, Move::Paper) => 3,
(Move::Paper, Move::Scissors) => 0, fn calc_move(d1: Outcome, m1: Move) -> Move {
(Move::Scissors, Move::Rock) => 0, match (d1, m1) {
(Move::Scissors, Move::Paper) => 6, (Outcome::X, Move::Rock) => Move::Scissors,
(Move::Scissors, Move::Scissors) => 3, (Outcome::X, Move::Paper) => Move::Rock,
(Outcome::X, Move::Scissors) => Move::Paper,
(Outcome::Y, v) => v,
(Outcome::Z, Move::Rock) => Move::Paper,
(Outcome::Z, Move::Paper) => Move::Scissors,
(Outcome::Z, Move::Scissors) => Move::Rock,
} }
} }
@ -60,7 +61,7 @@ fn main() {
for (a, b) in output { for (a, b) in output {
score += b as i32; score += b as i32;
score += calc_score(b, a); score += calc_move(b, a) as i32;
} }
println!("{:?}", score); println!("{:?}", score);