-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.cpp
86 lines (81 loc) · 2.11 KB
/
part2.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
#include <bits/stdc++.h>
using namespace std;
#define MAX_LINE_LENGTH 1000
int sum(vector<int> &v)
{
int res = 0;
for (int i = 0; i < v.size(); i++)
res += v[i];
return res;
}
int getScoreOfCard(int &c)
{
if (c > 1)
{
int res = 1;
for (int i = 1; i < c; i++)
res *= 2;
return res;
}
return c;
}
int solvePart1(vector<int> &cards)
{
int res = 0;
for (int i = 0; i < cards.size(); i++)
res += getScoreOfCard(cards[i]);
return res;
}
int solvePart2(vector<int> &cards)
{
vector<int> cardCounts = vector<int>(cards.size(), 1);
vector<int> cardsConsumed = vector<int>(cards.size(), 0);
for (int i = 0; i < cards.size(); i++)
{
for (int winId = i + 1; winId <= min(i + cards[i], (int)cards.size()); winId++)
cardCounts[winId] += cardCounts[i] - cardsConsumed[i];
cardsConsumed[i] += cardCounts[i];
}
return sum(cardCounts);
}
int cardHasHowManyRight(vector<int> &winVals, vector<int> &candVals)
{
int res = 0;
sort(candVals.begin(), candVals.end());
for (int i = 0; i < winVals.size(); i++)
if (binary_search(candVals.begin(), candVals.end(), winVals[i]))
res++;
return res;
}
vector<int> initCardsFromIO()
{
vector<int> cardsByHowManyRight;
char line[MAX_LINE_LENGTH];
int x;
while (cin.getline(line, MAX_LINE_LENGTH))
{
vector<int> winVals, candVals;
stringstream ss;
string s;
bool hasSeenPipe = false;
ss << line;
// Consume card ID (lol)
ss >> s >> s;
while (!ss.eof())
{
ss >> s;
if (!hasSeenPipe && s == "|")
hasSeenPipe = true;
else if (stringstream(s) >> x)
hasSeenPipe ? candVals.push_back(x) : winVals.push_back(x);
}
cardsByHowManyRight.push_back(cardHasHowManyRight(winVals, candVals));
}
return cardsByHowManyRight;
}
int main()
{
vector<int> cards = initCardsFromIO();
cout << "Part 1: " << solvePart1(cards) << endl;
cout << "Part 2: " << solvePart2(cards) << endl;
}