-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.cpp
executable file
·109 lines (92 loc) · 2.11 KB
/
day04.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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#ifndef FIRST_PART
#define FIRST_PART 0
#endif
int cards = 10;
void part1(vector<string> input) {
string ss[100];
int ans = 0;
for (int i = 0; i < input.size(); i++) {
set<int> s;
istringstream line(input[i]);
line >> ss[0];
int n;
line >> n >> ss[1];
for (int i = 0; i < cards; i++) {
int j;
line >> j;
s.insert(j);
}
line >> ss[2];
int sc = 0;
while (!line.eof()) {
int m;
line >> m;
if (s.find(m) != s.end()) {
if (sc == 0) {
sc = 1;
} else {
sc *= 2;
}
}
}
ans += sc;
}
cout << ans << "\n";
}
void part2(vector<string> input) {
string ss[100];
int ans = 0;
vector<int> copies(input.size());
for (int i = 0; i < copies.size(); i++) {
copies[i]++;
}
for (int i = 0; i < input.size(); i++) {
set<int> s;
istringstream line(input[i]);
line >> ss[0];
int n;
line >> n >> ss[1];
for (int i = 0; i < cards; i++) {
int j;
line >> j;
s.insert(j);
}
line >> ss[2];
int ci = i + 1;
while (!line.eof()) {
int m;
line >> m;
if (s.find(m) != s.end()) {
copies[ci++] += copies[i];
}
}
}
for (int i = 0; i < copies.size() - 1; i++) {
ans += copies[i];
}
cout << ans << "\n";
}
int main() {
vector<string> input;
while (!cin.eof()) {
string s;
getline(cin, s);
input.push_back(s);
}
#if FIRST_PART
part1(input);
#else
part2(input);
#endif
}