day 2
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d2"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "d2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
use std::fs::read_to_string;
|
||||||
|
use std::io::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum RPS {
|
||||||
|
Rock,
|
||||||
|
Paper,
|
||||||
|
Scissor,
|
||||||
|
}
|
||||||
|
use RPS::*;
|
||||||
|
|
||||||
|
impl RPS {
|
||||||
|
fn from_char(c: char) -> Self {
|
||||||
|
match c {
|
||||||
|
'A' | 'X' => Rock,
|
||||||
|
'B' | 'Y' => Paper,
|
||||||
|
'C' | 'Z' => Scissor,
|
||||||
|
_ => panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn score(self) -> u32 {
|
||||||
|
match self {
|
||||||
|
Rock => 1,
|
||||||
|
Paper => 2,
|
||||||
|
Scissor => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn outcome(opponent: Self, you: Self) -> Outcome {
|
||||||
|
match (you, opponent) {
|
||||||
|
(Rock, Rock) | (Paper, Paper) | (Scissor, Scissor) => Draw,
|
||||||
|
(Rock, Scissor) | (Paper, Rock) | (Scissor, Paper) => Win,
|
||||||
|
(Rock, Paper) | (Paper, Scissor) | (Scissor, Rock) => Loss,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum Outcome {
|
||||||
|
Win,
|
||||||
|
Loss,
|
||||||
|
Draw,
|
||||||
|
}
|
||||||
|
use Outcome::*;
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
pub fn from_char(c: char) -> Self {
|
||||||
|
match c {
|
||||||
|
'X' => Loss,
|
||||||
|
'Y' => Draw,
|
||||||
|
'Z' => Win,
|
||||||
|
_ => panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn score(self) -> u32 {
|
||||||
|
match self {
|
||||||
|
Win => 6,
|
||||||
|
Draw => 3,
|
||||||
|
Loss => 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn for_opponent(self, opponent: RPS) -> RPS {
|
||||||
|
match (self, opponent) {
|
||||||
|
(Win, Rock) | (Draw, Paper) | (Loss, Scissor) => Paper,
|
||||||
|
(Win, Paper) | (Draw, Scissor) | (Loss, Rock) => Scissor,
|
||||||
|
(Win, Scissor) | (Draw, Rock) | (Loss, Paper) => Rock,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Error> {
|
||||||
|
let mut total_score_p1 = 0;
|
||||||
|
let mut total_score_p2 = 0;
|
||||||
|
for line in read_to_string("input.txt")?.split("\n") {
|
||||||
|
if line.is_empty() { continue }
|
||||||
|
let (opponent, you) = (line.chars().nth(0).unwrap(), line.chars().nth(2).unwrap());
|
||||||
|
let (opponent, your_move, outcome) = (RPS::from_char(opponent), RPS::from_char(you), Outcome::from_char(you));
|
||||||
|
let score_p1 = your_move.score() + RPS::outcome(opponent, your_move).score();
|
||||||
|
total_score_p1 += score_p1;
|
||||||
|
let score_p2 = outcome.score() + outcome.for_opponent(opponent).score();
|
||||||
|
total_score_p2 += score_p2;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("part 1: {}", total_score_p1);
|
||||||
|
println!("part 2: {}", total_score_p2);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user