-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 7.cpp
129 lines (118 loc) · 3.01 KB
/
Day 7.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
#include <iostream>
#include <istream>
#include <fstream>
#include <ostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
#define cin infile
#define cout outfile
using namespace std;
bool partTwo = true;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
string input;
auto getHandType = [](string hand)
{
map<char, int> mp;
int jokers = 0;
for (int i = 0; i < 5; i++)
{
char card = hand[i];
if(partTwo && card == 'J') {
jokers++;
continue;
}
mp[card]++;
}
vector<int> values;
for (auto y : mp)
values.push_back(y.second);
sort(values.begin(), values.end());
reverse(values.begin(), values.end());
if(jokers == 5)
return 7;
values[0] += jokers;
if (values[0] == 5)
return 7;
if (values[0] == 4)
return 6;
if (values[0] == 3 && values[1] == 2)
return 5;
if (values[0] == 3 && values[1] == 1)
return 4;
if (values[0] == 2 && values[1] == 2)
return 3;
if (values[0] == 2 && values[1] == 1)
return 2;
return 1;
};
auto toNumCast = [](char c)
{
if (c >= '1' && c <= '9')
return c - '0';
if (c == 'T')
return 10;
if (c == 'J')
return partTwo ? 1 : 11;
if (c == 'Q')
return 12;
if (c == 'K')
return 13;
if (c == 'A')
return 14;
return 0;
};
auto isSecondHandBetter = [toNumCast](string hand1, string hand2)
{
for (int i = 0; i < 5; i++)
{
if (toNumCast(hand1[i]) > toNumCast(hand2[i]))
return true;
if (toNumCast(hand1[i]) < toNumCast(hand2[i]))
return false;
}
return false;
};
vector<pair<string, int>> hands;
while (getline(cin, input))
{
string hand = "";
int val = 0;
bool spacePassed = false;
for (int i = 0; i < input.size(); i++)
{
if (input[i] == ' ')
{
spacePassed = true;
continue;
}
if (spacePassed)
{
val *= 10;
val += input[i] - '0';
}
else
{
hand.push_back(input[i]);
}
}
hands.push_back(make_pair(hand, val));
}
sort(hands.begin(), hands.end(), [isSecondHandBetter, getHandType](auto a, auto b)
{
if(getHandType(a.first) > getHandType(b.first)) return false;
if (getHandType(a.first) < getHandType(b.first)) return true;
return !isSecondHandBetter(a.first, b.first); });
int sum = 0;
for(int i = 0; i < hands.size(); i++)
{
sum += hands[i].second * (i+1);
}
cout << sum;
return 0;
}