-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_07b.cpp
142 lines (126 loc) · 3.72 KB
/
day_07b.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
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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include <unordered_map>
struct Hand {
std::string cards;
std::array<int, 5> label_values;
int type;
};
constexpr std::array<char, 13> labels {{'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A'}};
int get_type(const std::string& cards) {
std::unordered_map<char, int> m;
for (const auto card : cards) {
if (m.find(card) == m.end()) m[card] = 0;
m[card]++;
}
for (const auto & [card , n] : m) {
std::cout << "(" << card << ',' << n << ")" << ' ';
}
std::cout << '\n';
if (m.size() == 1) {
return 6;
}
if (m.size() == 2 && (m['J'] != 0)) {
return 6;
}
if (m.size() == 2 && (m[cards[0]] == 4 || m[cards[0]] == 1)) {
return 5;
}
if (m.size() == 3) {
for (const auto& [card, n] : m) {
if (card =='J') continue;
if (n + m['J'] == 4) return 5;
}
}
if (m.size() == 2 && (m[cards[0]] == 3 || m[cards[0]] == 2)) {
return 4;
}
if (m.size() == 3) {
for (const auto& [card, n] : m) {
if (card =='J') continue;
if (n + m['J'] == 3) return 4;
}
}
int count_pairs = 0;
int count_three_of_a_kind = 0;
int single = 0;
for (const auto& [card, n] : m) {
if (card == 'J') continue;
if (n == 1) single++;
if (n == 2) count_pairs++;
if (n == 3) count_three_of_a_kind++;
}
if (count_three_of_a_kind == 1) return 3;
for (const auto [card, n] : m) {
if (card =='J') continue;
if (n + m['J'] == 3) return 3;
}
if (count_pairs > 0 && m['J'] > 0) return 3;
if (count_pairs == 2) return 2;
if (count_pairs + std::min(single, m['J']) == 2) return 2;
if (count_pairs == 1) return 1;
for (const auto [card, n] : m) {
if (card =='J') continue;
if (n + m['J'] == 2) return 1;
}
return 0;
}
int get_value(const char label) {
return std::distance(std::begin(labels), std::find(std::begin(labels), std::end(labels), label));
}
std::pair<Hand, int> get_hand_and_bid(const std::string& line) {
std::cout << line << '\n';
const auto idx = line.find(' ');
Hand hand;
hand.cards = line.substr(0, idx);
std::cout << hand.cards << '|' << '\n';
hand.type = get_type(hand.cards);
std::cout << hand.type << '\n';
for (int i = 0; i < hand.cards.size(); i++) {
hand.label_values[i] = get_value(hand.cards[i]);
std::cout << hand.label_values[i] << ' ';
}
std::cout << '\n';
return std::make_pair(hand, std::stoi(line.substr(idx, line.size() - idx)));
}
bool compare_hands(const Hand& hand1, const Hand& hand2) {
if (hand1.type > hand2.type) {
return true;
} else if (hand1.type < hand2.type) {
return false;
}
for (int i = 0; i < 5; i++) {
if (hand1.label_values[i] > hand2.label_values[i]) return true;
if (hand1.label_values[i] < hand2.label_values[i]) return false;
}
return true;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_07_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::pair<Hand, int>> hands_and_bids;
while(std::getline(file, line)) {
hands_and_bids.emplace_back(get_hand_and_bid(line));
}
std::sort(std::begin(hands_and_bids),
std::end(hands_and_bids),
[](const auto& hand_and_bid_1, const auto& hand_and_bid_2) {
return !compare_hands(hand_and_bid_1.first, hand_and_bid_2.first);
});
std::size_t total = 0;
for (int i = 0; i < hands_and_bids.size(); i++) {
std::cout << hands_and_bids[i].first.cards << ' ' << hands_and_bids[i].first.type << ' ' << hands_and_bids[i].second << '\n';
total += (i+1) * hands_and_bids[i].second;
}
std::cout << total << '\n';
return 0;
}