-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
106 lines (96 loc) · 2.81 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::str::FromStr;
#[derive(Clone, Copy)]
#[repr(u32)]
enum Shape {
Rock = 1,
Paper = 2,
Scissor = 3,
}
#[derive(Clone, Copy)]
#[repr(u32)]
enum Outcome {
Loose = 0,
Draw = 3,
Win = 6,
}
impl Shape {
pub const fn shape_to_outcome(self, outcome: Outcome) -> Self {
match outcome {
Outcome::Win => match self {
Self::Rock => Self::Paper,
Self::Paper => Self::Scissor,
Self::Scissor => Self::Rock,
},
Outcome::Loose => match self {
Self::Rock => Self::Scissor,
Self::Paper => Self::Rock,
Self::Scissor => Self::Paper,
},
Outcome::Draw => self,
}
}
pub const fn outcome_against(self, other: Self) -> Outcome {
match (self, other) {
(Self::Rock, Self::Scissor)
| (Self::Scissor, Self::Paper)
| (Self::Paper, Self::Rock) => Outcome::Win,
(Self::Rock, Self::Paper)
| (Self::Scissor, Self::Rock)
| (Self::Paper, Self::Scissor) => Outcome::Loose,
_ => Outcome::Draw,
}
}
pub const fn score_against(self, other: Self) -> u32 {
let outcome = self.outcome_against(other);
outcome as u32 + self as u32
}
}
impl FromStr for Shape {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let rcs = match s {
"A" | "X" => Self::Rock,
"B" | "Y" => Self::Paper,
"C" | "Z" => Self::Scissor,
_ => return Err(format!("{s} is not a valis RCS action")),
};
Ok(rcs)
}
}
impl FromStr for Outcome {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let outcome = match s {
"X" => Self::Loose,
"Y" => Self::Draw,
"Z" => Self::Win,
_ => return Err(format!("{s} is not a valis RCS outcome")),
};
Ok(outcome)
}
}
fn main() {
let file: Vec<(&str, &str)> = include_str!("../input.txt")
.lines()
.map(|line| line.split_once(' ').unwrap())
.collect();
let part1_score: u32 = file
.iter()
.map(|(a, b)| {
let enemy_shape = Shape::from_str(a).unwrap();
let my_shape = Shape::from_str(b).unwrap();
my_shape.score_against(enemy_shape)
})
.sum();
println!("Part 1 score = {part1_score}");
let part2_score: u32 = file
.iter()
.map(|(a, b)| {
let enemy_shape = Shape::from_str(a).unwrap();
let outcome = Outcome::from_str(b).unwrap();
let my_shape = enemy_shape.shape_to_outcome(outcome);
my_shape.score_against(enemy_shape)
})
.sum();
println!("Part 2 score = {part2_score}");
}