diff options
Diffstat (limited to '2023/04/src/part2.cpp')
-rw-r--r-- | 2023/04/src/part2.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/2023/04/src/part2.cpp b/2023/04/src/part2.cpp new file mode 100644 index 0000000..1e369d8 --- /dev/null +++ b/2023/04/src/part2.cpp | |||
@@ -0,0 +1,66 @@ | |||
1 | #include <iostream> | ||
2 | #include <sstream> | ||
3 | #include <fstream> | ||
4 | #include <vector> | ||
5 | #include <algorithm> | ||
6 | #include <cmath> | ||
7 | |||
8 | int matchings(const std::string& line, int ws = 10, int ns = 25) | ||
9 | { | ||
10 | int matchings{}; | ||
11 | std::istringstream in{line}; | ||
12 | |||
13 | int a; | ||
14 | std::string skip; | ||
15 | in >> skip >> skip; | ||
16 | |||
17 | std::vector<int> winning(ws); | ||
18 | for (int w = 0; w < ws; ++w) | ||
19 | { | ||
20 | in >> a; | ||
21 | winning.push_back(a); | ||
22 | } | ||
23 | |||
24 | in >> skip; | ||
25 | |||
26 | for (int n = 0; n < ns; ++n) | ||
27 | { | ||
28 | in >> a; | ||
29 | if (std::any_of(winning.cbegin(), winning.cend(), [a](int w){ return w == a; })) | ||
30 | { | ||
31 | ++matchings; | ||
32 | } | ||
33 | } | ||
34 | |||
35 | return matchings; | ||
36 | } | ||
37 | |||
38 | int main(void) | ||
39 | { | ||
40 | int answer{}; | ||
41 | std::vector<int> cards; | ||
42 | |||
43 | std::ifstream input{ "resources/input.txt" }; | ||
44 | if (input.is_open()) | ||
45 | { | ||
46 | std::string line; | ||
47 | while (not std::getline(input,line).eof()) | ||
48 | { | ||
49 | cards.push_back(matchings(line)); | ||
50 | } | ||
51 | } | ||
52 | input.close(); | ||
53 | |||
54 | std::vector<int> mult(cards.size(), 1); | ||
55 | for (int a = 0; a < cards.size(); ++a) | ||
56 | { | ||
57 | for (int b = 1; b <= cards[a] and a + b < cards.size(); ++b) | ||
58 | { | ||
59 | mult[a + b] += mult[a]; | ||
60 | } | ||
61 | answer += mult[a]; | ||
62 | } | ||
63 | |||
64 | std::cout << answer << std::endl; | ||
65 | return 0; | ||
66 | } | ||