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
|
use advent_lib::prelude::*;
//use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::convert::TryFrom;
fn main() -> Result<()> {
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("Usage: advent input");
}
let _ = args.next();
let filename = args.next().unwrap();
let input = advent_lib::read_lines_file(&filename)?;
let input = advent_lib::group_lines_by_blanks(input);
let mut decks: Vec<Vec<i64>> = Vec::new();
for player_input in &input {
let mut deck = Vec::new();
let mut card_iterator = player_input.iter();
let _ = card_iterator.next();
for line in card_iterator {
let card_id = line.parse::<i64>().unwrap();
deck.push(card_id);
}
decks.push(deck);
}
let (_winner, score) = run_game(&decks);
println!("{}", score);
let (_winner2, score2) = run_game2(&decks);
println!("{}", score2);
Ok(())
}
fn run_game(input_decks: &Vec<Vec<i64>>) -> (usize, i64) {
let mut decks: Vec<Vec<i64>> = Vec::new();
for input_deck in input_decks {
let mut deck = Vec::new();
for card in input_deck {
deck.push(*card);
}
decks.push(deck);
}
loop {
let mut is_game_over = false;
for deck in &decks {
if deck.len() == 0 {
is_game_over = true;
break;
}
}
if is_game_over {
break;
}
let round_winner;
if decks[0][0] > decks[1][0] {
round_winner = 0;
} else {
round_winner = 1;
}
let round_loser = 1 - round_winner;
let mut transferred_cards = Vec::new();
transferred_cards.push(decks[round_winner][0]);
transferred_cards.push(decks[round_loser][0]);
for i in 0 .. decks.len() {
let (_, rest_of_deck) = decks[i].split_at(1);
let new_deck;
if i == round_winner {
new_deck = [rest_of_deck, transferred_cards.as_slice()].concat();
} else {
new_deck = rest_of_deck.to_vec();
}
decks[i] = new_deck;
}
}
let mut winner = 0;
let mut score = 0;
for i in 0 .. decks.len() {
let deck = &decks[i];
if deck.len() > 0 {
winner = i;
for j in 0 .. deck.len() {
let j_from_bottom = i64::try_from(deck.len() - j).unwrap();
score += deck[j] * j_from_bottom;
}
}
}
(winner, score)
}
fn run_game2(input_decks: &Vec<Vec<i64>>) -> (usize, i64) {
let mut decks = copy_decks(input_decks);
let mut previous_configurations: BTreeSet<Vec<Vec<i64>>> = BTreeSet::new();
loop {
//println!("{:?}", decks);
let mut is_game_over = false;
for deck in &decks {
if deck.len() == 0 {
is_game_over = true;
break;
}
}
if is_game_over {
break;
}
if previous_configurations.contains(&decks) {
//println!("loop detected");
return (0, score_deck(&decks[0]));
}
previous_configurations.insert(copy_decks(&decks));
let round_winner;
if decks[0].len() >= usize::try_from(decks[0][0]).unwrap() + 1
&& decks[1].len() >= usize::try_from(decks[1][0]).unwrap() + 1
{
let mut recursive_decks: Vec<Vec<i64>> = Vec::new();
for i in 0 .. decks.len() {
let mut recursive_deck = Vec::new();
let n_to_take = usize::try_from(decks[i][0]).unwrap();
for j in 1 .. n_to_take + 1 {
recursive_deck.push(decks[i][j]);
}
recursive_decks.push(recursive_deck);
}
let (recursive_winner, _) = run_game2(&recursive_decks);
round_winner = recursive_winner;
//println!("recursing {:?}", decks);
} else if decks[0][0] > decks[1][0] {
round_winner = 0;
} else {
round_winner = 1;
}
let round_loser = 1 - round_winner;
let mut transferred_cards = Vec::new();
transferred_cards.push(decks[round_winner][0]);
transferred_cards.push(decks[round_loser][0]);
for i in 0 .. decks.len() {
let (_, rest_of_deck) = decks[i].split_at(1);
let new_deck;
if i == round_winner {
new_deck = [rest_of_deck, transferred_cards.as_slice()].concat();
} else {
new_deck = rest_of_deck.to_vec();
}
decks[i] = new_deck;
}
}
let mut winner = 0;
let mut score = 0;
for i in 0 .. decks.len() {
if decks[i].len() > 0 {
winner = i;
score = score_deck(&decks[i]);
}
}
(winner, score)
}
fn copy_decks(input_decks: &Vec<Vec<i64>>) -> Vec<Vec<i64>> {
let mut decks: Vec<Vec<i64>> = Vec::new();
for input_deck in input_decks {
let mut deck = Vec::new();
for card in input_deck {
deck.push(*card);
}
decks.push(deck);
}
decks
}
fn score_deck(deck: &Vec<i64>) -> i64 {
let mut score = 0;
for i in 0 .. deck.len() {
let i_from_bottom = i64::try_from(deck.len() - i).unwrap();
score += deck[i] * i_from_bottom;
}
score
}
|