-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-2.rs
97 lines (85 loc) · 2.43 KB
/
day-2.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
use std::str::FromStr;
const INPUT: &str = include_str!("day-2.txt");
aoc_2022::aoc! {
struct Day2 {
rounds: Vec<Round>,
}
self(input) {
let mut rounds = Vec::new();
for line in input.lines() {
rounds.push(line.parse()?);
}
Ok(Self { rounds })
}
part1 usize {
Ok(self.rounds.iter().map(|r| r.response.vs(r.opponent)).sum())
}
part2 usize {
Ok(self.rounds.iter().map(|r| r.response.vs2(r.opponent)).sum())
}
input = INPUT;
test day2(INPUT, 13268, 15508);
}
#[derive(Clone, Copy)]
enum RPS {
Rock,
Paper,
Scissors,
}
impl RPS {
fn vs(self, other: Self) -> usize {
const WIN: usize = 6;
const DRAW: usize = 3;
const LOSE: usize = 0;
match (self, other) {
(RPS::Rock, RPS::Rock) => 1 + DRAW,
(RPS::Rock, RPS::Paper) => 1 + LOSE,
(RPS::Rock, RPS::Scissors) => 1 + WIN,
(RPS::Paper, RPS::Rock) => 2 + WIN,
(RPS::Paper, RPS::Paper) => 2 + DRAW,
(RPS::Paper, RPS::Scissors) => 2 + LOSE,
(RPS::Scissors, RPS::Rock) => 3 + LOSE,
(RPS::Scissors, RPS::Paper) => 3 + WIN,
(RPS::Scissors, RPS::Scissors) => 3 + DRAW,
}
}
fn vs2(self, other: Self) -> usize {
match self {
RPS::Rock => match other {
RPS::Rock => RPS::Scissors,
RPS::Paper => RPS::Rock,
RPS::Scissors => RPS::Paper,
},
RPS::Paper => other,
RPS::Scissors => match other {
RPS::Rock => RPS::Paper,
RPS::Paper => RPS::Scissors,
RPS::Scissors => RPS::Rock,
},
}
.vs(other)
}
}
struct Round {
opponent: RPS,
response: RPS,
}
impl FromStr for Round {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (opponent, response) = s.split_once(' ').ok_or("invalid format")?;
let opponent = match opponent {
"A" => RPS::Rock,
"B" => RPS::Paper,
"C" => RPS::Scissors,
_ => return Err("invalid opponent move"),
};
let response = match response {
"X" => RPS::Rock,
"Y" => RPS::Paper,
"Z" => RPS::Scissors,
_ => return Err("invalid response move"),
};
Ok(Self { opponent, response })
}
}