-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2_2.rs
89 lines (76 loc) · 2.35 KB
/
day2_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
use std::collections::HashMap;
#[derive(PartialEq)]
enum RoundResult {
Win,
Lose,
Draw,
}
#[derive(Debug, PartialEq)]
enum Choice {
Rock,
Paper,
Scissors,
}
impl Choice {
fn get_desired_choice(&self, desired_result: &RoundResult) -> Choice {
match *self {
Choice::Rock => match *desired_result {
RoundResult::Win => Choice::Paper,
RoundResult::Lose => Choice::Scissors,
RoundResult::Draw => Choice::Rock,
},
Choice::Paper => match *desired_result {
RoundResult::Win => Choice::Scissors,
RoundResult::Lose => Choice::Rock,
RoundResult::Draw => Choice::Paper,
},
Choice::Scissors => match *desired_result {
RoundResult::Win => Choice::Rock,
RoundResult::Lose => Choice::Paper,
RoundResult::Draw => Choice::Scissors,
},
}
}
fn get_choice_score(&self) -> isize {
match *self {
Self::Rock => 1,
Self::Paper => 2,
Self::Scissors => 3,
}
}
fn get_result_score(&self, desired_result: &RoundResult) -> isize {
match *desired_result {
RoundResult::Win => 6,
RoundResult::Lose => 0,
RoundResult::Draw => 3,
}
}
}
fn main() {
let strategy_lines = include_str!("../inputs/day2.txt");
let opponent_lookup: HashMap<&str, Choice> = HashMap::from([
("A", Choice::Rock),
("B", Choice::Paper),
("C", Choice::Scissors),
]);
let result_lookup: HashMap<&str, RoundResult> = HashMap::from([
("X", RoundResult::Lose),
("Y", RoundResult::Draw),
("Z", RoundResult::Win),
]);
let rounds: Vec<Vec<&str>> = strategy_lines
.lines()
.map(|l| return l.split(" ").collect())
.collect();
let score: isize = rounds
.iter()
.map(|round| {
let opp_choice = opponent_lookup.get(round[0]).unwrap();
let desired_result = result_lookup.get(round[1]).unwrap();
let player_choice = opp_choice.get_desired_choice(desired_result);
return player_choice.get_choice_score()
+ player_choice.get_result_score(desired_result);
})
.sum();
println!("{:?}", score)
}