-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.cpp
159 lines (143 loc) · 3.46 KB
/
day7.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include "Helpers/HelperFunctions.h"
struct camel {
string hand;
int bid;
};
int getType(const camel& a) {
map<char, int> chars;
for (int i = 0; i < a.hand.size(); i++) {
chars[a.hand[i]]++;
}
vector<int> values;
if (chars['J'] == 0) {
for ( const auto &[key, value]: chars ) {
values.push_back(value);
}
if (contains(values, 5)) { // five of a kind
return 7;
}
if (contains(values, 4)) { // four of a kind
return 6;
}
if (contains(values, 3) && contains(values, 2)) { // full house
return 5;
}
if (contains(values, 3)) { // three of a kind
return 4;
}
if (contains(values, 2)) {
if (count(values.begin(), values.end(), 2) == 2) { // two pair
return 3;
} else {
return 2; // one pair
}
}
return 1; // high card
} else {
int J = 0;
for ( const auto &[key, value]: chars ) {
if (key == 'J') {
J = value;
} else {
values.push_back(value);
}
}
if (J == 5) { // five of a kind // JJJJJ
return 7;
}
if (J == 4) { // five of a kind // JJJJX
return 7;
}
if (J == 3) {
if (contains(values, 2)) {
return 7; // five of a kind // JJJXX
} else {
return 6; // four of a kind // JJJXY
}
}
if (J == 2) {
if (contains(values, 3)) {
return 7; // five of a kind // JJXXX
}
if (contains(values, 2)) {
return 6; // four of a kind // JJXXY
} else {
return 4; // three of a kind // JJXYZ
}
}
if (J == 1) {
if (contains(values, 4)) {
return 7; // five of a kind // JXXXX
}
if (contains(values, 3)) {
return 6; // four of a kind // JXXXY
}
if (contains(values, 2)) {
if (count(values.begin(), values.end(), 2) == 2) { // two pair
return 5; // full house // JXXYY
} else {
return 4; // three of a kind // JXXYZ
}
}
return 2; // one pair // JXYZA
}
return 1;
}
}
static int getCardValue(char n) {
switch(n) {
case 'A': return 13;
case 'K': return 12;
case 'Q': return 11;
case 'T': return 10;
case '9': return 9;
case '8': return 8;
case '7': return 7;
case '6': return 6;
case '5': return 5;
case '4': return 4;
case '3': return 3;
case '2': return 2;
case 'J': return 1;
default: return 0;
}
}
bool operator<(const camel& a, const camel& b) {
if (getType(a) != getType(b)) {
return (getType(a) < getType(b));
}
for (int i = 0; i < a.hand.size(); i++) {
if (getCardValue(a.hand[i]) != getCardValue(b.hand[i])) {
return getCardValue(a.hand[i]) < getCardValue(b.hand[i]);
}
}
return false;
}
int main() {
cout << "Day 7" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2023\inputs\day7.txt)");
string str;
vector<camel> data;
while (getline(file, str)) {
cout << str << endl;
vector<string> out;
tokenize(str, ' ', out);
data.push_back({out[0], stoi(out[1])});
}
cout << " --- " << endl;
sort(data.begin(), data.end());
long long sum = 0;
for (int i = 0; i < data.size(); ++i) {
cout << i+1 << " - " << data[i].hand << " - " << getType(data[i]) << endl;
sum += (i+1)*data[i].bid;
}
cout << sum << endl;
return 0;
}