-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
263 lines (212 loc) · 7.47 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
BENCHMARK RESULTS
*/
// allow bench feature when using unstable flag
// use: $ rustup run nightly cargo bench --features unstable
#![cfg_attr(feature = "unstable", feature(test))]
use aoc_import_magic::{import_magic, PuzzleOptions};
use std::{
collections::{HashMap, HashSet, VecDeque},
io,
};
const DAY: u32 = 22;
type InputType = Game;
type OutputType1 = usize;
type OutputType2 = OutputType1;
type TodaysPuzzleOptions = PuzzleOptions<InputType>;
#[derive(Clone, Debug, Default, PartialEq)]
struct Game {
player_one: VecDeque<usize>,
player_two: VecDeque<usize>,
}
impl Game {
fn play_round(&mut self) -> Option<usize> {
let card_one = self.player_one.pop_front().unwrap();
let card_two = self.player_two.pop_front().unwrap();
if card_one > card_two {
self.player_one.push_back(card_one);
self.player_one.push_back(card_two);
} else {
self.player_two.push_back(card_two);
self.player_two.push_back(card_one);
}
if self.player_one.is_empty() {
Some(Self::calc_score(&self.player_two))
} else if self.player_two.is_empty() {
Some(Self::calc_score(&self.player_one))
} else {
None
}
}
fn calc_score(player: &VecDeque<usize>) -> usize {
player.iter().rev().enumerate().map(|(idx, card)| card * (idx + 1)).sum()
}
}
impl From<Vec<String>> for Game {
fn from(from: Vec<String>) -> Self {
let mut iter = from.into_iter();
iter.next().unwrap(); // "Player 1:"
let mut player_one = VecDeque::new();
while let Some(line) = iter.next() {
if line.is_empty() {
break;
}
player_one.push_back(line.parse().unwrap());
}
iter.next().unwrap();
let player_two = iter.map(|line| line.parse().unwrap()).collect();
Game {
player_one,
player_two,
}
}
}
fn parse_input(input: Vec<String>, _config: &HashMap<String, String>, _verbose: bool) -> InputType {
Game::from(input)
}
fn part1(po: &TodaysPuzzleOptions) -> OutputType1 {
let mut game = po.get_data().clone();
loop {
if let Some(result) = game.play_round() {
break result;
}
}
}
fn part2(po: &TodaysPuzzleOptions) -> OutputType2 {
let Game { player_one, player_two} = po.get_data().clone();
play_recursive(player_one, player_two).1
}
enum Player {
One,
Two,
}
fn play_recursive(mut player_one: VecDeque<usize>, mut player_two: VecDeque<usize>) -> (Player, usize) {
//println!("Starting game with decks:");
//println!("Player 1: {:?}", player_one);
//println!("Player 2: {:?}", player_two);
let mut history = HashSet::new();
//let mut round = 1;
loop {
//println!("\n-- Round {} --", round);
//println!("Player 1: {:?}", player_one);
//println!("Player 2: {:?}", player_two);
let game_state = (player_one.clone(), player_two.clone());
if history.contains(&game_state) {
//println!("We ended up in a cycle");
break (Player::One, 0);
} else {
history.insert(game_state);
}
let card_one = player_one.pop_front().unwrap();
let card_two = player_two.pop_front().unwrap();
//println!("Player 1 plays: {}", card_one);
//println!("Player 2 plays: {}", card_two);
let winner = if player_one.len() >= card_one && player_two.len() >= card_two {
//println!("Playing a sub-game to determine winner");
let player_one_subgame = player_one.iter().take(card_one).map(ToOwned::to_owned).collect();
let player_two_subgame = player_two.iter().take(card_two).map(ToOwned::to_owned).collect();
play_recursive(player_one_subgame, player_two_subgame).0
} else {
if card_one > card_two {
Player::One
} else {
Player::Two
}
};
match winner {
Player::One => {
//println!("Player 1 wins round {}", round);
player_one.push_back(card_one);
player_one.push_back(card_two);
}
Player::Two => {
//println!("Player 2 wins round {}", round);
player_two.push_back(card_two);
player_two.push_back(card_one);
}
}
if player_one.is_empty() {
break (Player::Two, Game::calc_score(&player_two));
} else if player_two.is_empty() {
break (Player::One, Game::calc_score(&player_one));
}
//round += 1;
}
}
// =================================================================================================
// End of actual logic
// What follows is the main function glue as well as tests + benchmarking code
// =================================================================================================
fn main() -> Result<(), io::Error> {
println!("AoC 2020 | Day {}", DAY);
// This function is pure magic (see ../../aoc_import_magic/lib.rs) because it
// 1. parses command line arguments
// 2. reads the input file for the correct day
// 3. uses `parse_input` as a parsing function
// 4. returns a nice usable struct which contains everything which we need for the actual puzzle
let puzzle = import_magic(DAY, parse_input)?;
if !puzzle.skip_p1 {
let res1 = part1(&puzzle);
println!("Part 1 result: {}", res1);
};
let res2 = part2(&puzzle);
println!("Part 2 result: {}", res2);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use aoc_import_magic::{import_magic_with_params, PuzzleOptions};
pub(super) fn import_helper(inputname: &str) -> PuzzleOptions<InputType> {
let params = ["appname", "--input", inputname];
import_magic_with_params(DAY, parse_input, ¶ms).unwrap()
}
fn test_case_helper(inputname: &str, sol1: Option<OutputType1>, sol2: Option<OutputType2>) {
let po = import_helper(inputname);
if let Some(sol1) = sol1 {
let res1 = part1(&po);
assert_eq!(sol1, res1, "part1");
}
if let Some(sol2) = sol2 {
let res2 = part2(&po);
assert_eq!(sol2, res2, "part2");
}
}
#[test]
fn example_1() {
test_case_helper("example1", Some(306), Some(291))
}
}
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use super::*;
use aoc_import_magic::test_helper_import_config;
use std::{
fs::File,
io::{BufRead, BufReader},
};
use test::Bencher;
fn helper_read_file(fname: &str) -> Vec<String> {
BufReader::new(File::open(fname).unwrap())
.lines()
.map(|line| line.unwrap())
.collect()
}
#[bench]
fn bench_parsing(bb: &mut Bencher) {
let input = helper_read_file(&format!("../../_inputs/day{:02}/real1.input", DAY));
let config = test_helper_import_config(DAY, "real1");
bb.iter(|| test::black_box(parse_input(input.to_owned(), &config, false)));
}
#[bench]
fn bench_part1(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| test::black_box(part1(&puzzle_options)));
}
#[bench]
fn bench_part2(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| test::black_box(part2(&puzzle_options, None)));
}
}