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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/// --- Part Two ---
///
/// The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column
/// says how the round needs to end: X means you need to lose, Y means you need to end the round in
/// a draw, and Z means you need to win. Good luck!"
///
/// The total score is still calculated in the same way, but now you need to figure out what shape
/// to choose so the round ends as indicated. The example above now goes like this:
///
/// In the first round, your opponent will choose Rock (A), and you need the round to end in a
/// draw (Y), so you also choose Rock. This gives you a score of 1 + 3 = 4.
/// In the second round, your opponent will choose Paper (B), and you choose Rock so you lose
/// (X) with a score of 1 + 0 = 1.
/// In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6
/// = 7.
///
/// Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total
/// score of 12.
///
/// Following the Elf's instructions for the second column, what would your total score be if
/// everything goes exactly according to your strategy guide?
use clap::Parser;
use itertools::Itertools;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;
const FILEPATH: &'static str = "examples/input.txt";
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[clap(short, long, default_value = FILEPATH)]
file: PathBuf,
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
enum RockPaperScissorsKind {
Rock,
Paper,
Scissors,
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
enum GameOutcome {
Win,
Loss,
Draw,
}
#[derive(Copy, Clone, Debug)]
struct GamePrediction {
outcome: GameOutcome,
opponent: RockPaperScissorsKind,
}
fn kind_to_value(kind: RockPaperScissorsKind) -> i32 {
use RockPaperScissorsKind::*;
match kind {
Rock => 1,
Paper => 2,
Scissors => 3,
}
}
fn opp_to_kind(val: char) -> Option<RockPaperScissorsKind> {
use RockPaperScissorsKind::*;
match val {
'A' => Some(Rock),
'B' => Some(Paper),
'C' => Some(Scissors),
_ => None,
}
}
fn exp_out_to_outcome(val: char) -> Option<GameOutcome> {
use GameOutcome::*;
match val {
'X' => Some(Loss),
'Y' => Some(Draw),
'Z' => Some(Win),
_ => None,
}
}
fn outcome_to_value(outcome: GameOutcome) -> i32 {
use GameOutcome::*;
match outcome {
Win => 6,
Draw => 3,
Loss => 0,
}
}
fn pred_to_player_kind(pred: GamePrediction) -> RockPaperScissorsKind {
use GameOutcome::*;
use RockPaperScissorsKind::*;
match (pred.outcome, pred.opponent) {
(Win, Rock) => Paper,
(Win, Paper) => Scissors,
(Win, Scissors) => Rock,
(Loss, Rock) => Scissors,
(Loss, Paper) => Rock,
(Loss, Scissors) => Paper,
(Draw, x) => x,
}
}
fn game_pred_to_value(prediction: GamePrediction) -> i32 {
outcome_to_value(prediction.outcome) + kind_to_value(pred_to_player_kind(prediction))
}
fn main() {
let args = Cli::parse();
let file = File::open(&args.file).unwrap();
let reader = BufReader::new(file);
let res: i32 = reader
.lines()
.map(|l| {
l.unwrap()
.split(' ')
.map(|w| w.parse::<char>().unwrap())
.collect_tuple::<(char, char)>()
})
.map(|v| v.unwrap())
.map(|(o_char, out_char)| GamePrediction {
outcome: exp_out_to_outcome(out_char).unwrap(),
opponent: opp_to_kind(o_char).unwrap(),
})
.map(|pred| game_pred_to_value(pred))
.sum();
println!(
"The total score if everything goes exactly according to your strategy guide is {}",
res
);
}
|