-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_22a.cpp
64 lines (61 loc) · 1.95 KB
/
day_22a.cpp
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
#include <algorithm>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
int main() {
std::fstream file{"../input/day_22_input"};
std::string line;
std::vector<std::deque<int>> player_decks;
std::vector<int> players;
while (std::getline(file, line)) {
line.erase(std::remove_if(std::begin(line), std::end(line),
[](const char c) { return !isprint(c); }),
std::end(line));
if (line == "") continue;
if (line.substr(0, 6) == "Player") {
player_decks.emplace_back();
players.push_back(std::stoi(line.substr(7, line.size() - 8)));
} else {
player_decks.back().push_back(std::stoi(line));
}
}
int total_players = players.size();
// Play
while (player_decks.size() > 1) {
size_t winning_player = -1;
int hightest_card = -1;
std::vector<int> top_cards(player_decks.size());
for (int i = 0; i < player_decks.size(); i++) {
if (hightest_card < player_decks[i].front()) {
hightest_card = player_decks[i].front();
winning_player = i;
}
top_cards[i] = player_decks[i].front();
player_decks[i].pop_front();
}
std::sort(std::begin(top_cards), std::end(top_cards), std::greater<int>());
for (const auto& top_card : top_cards) {
player_decks[winning_player].push_back(top_card);
}
for (int i = 0; i < player_decks.size(); i++) {
if (player_decks[i].empty()) {
player_decks.erase(std::begin(player_decks) + i);
players.erase(std::begin(players) + i);
i--;
}
}
}
long long score = 0;
for (const auto& player_deck : player_decks) {
for (int i = 0; i < player_deck.size(); i++) {
score += ((i + 1) * player_deck[player_deck.size() - 1 - i]);
}
}
std::cout << score << '\n';
return score;
}