aboutsummaryrefslogtreecommitdiff
path: root/2023/src/day04.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-01-03 23:35:54 +0100
committerThomas Schmucker <ts@its1.de>2024-01-03 23:35:54 +0100
commit56e890cec0a28c0a485212ccebfaf774235a79a2 (patch)
treed9c6241a1aa247e06ab5ba2f6f11967b77d458ec /2023/src/day04.cpp
parente9a1cb0441d137d7a26cb303b7e72d3424b7392d (diff)
downloadadvent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.gz
advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.bz2
advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.zip
prepare for more puzzles ... :)
Diffstat (limited to '2023/src/day04.cpp')
-rw-r--r--2023/src/day04.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/2023/src/day04.cpp b/2023/src/day04.cpp
new file mode 100644
index 0000000..0d3e552
--- /dev/null
+++ b/2023/src/day04.cpp
@@ -0,0 +1,114 @@
1#include <fstream>
2#include <iostream>
3#include <set>
4#include <sstream>
5#include <string>
6#include <string_view>
7#include <vector>
8
9using namespace std;
10
11vector<string>
12read_file(string_view filename)
13{
14 fstream input{ filename };
15 vector<string> data;
16
17 for ( string line; getline(input, line); ) {
18 data.emplace_back(line);
19 }
20
21 return data;
22}
23
24vector<string>
25split(const string& line, char sep)
26{
27 stringstream input{ line };
28 vector<string> parts;
29
30 for ( string part; getline(input, part, sep); ) {
31 parts.emplace_back(part);
32 }
33
34 return parts;
35}
36
37template<typename Container>
38Container
39read_ints(const string& line)
40{
41 Container container{};
42 stringstream input{ line };
43
44 for ( typename Container::value_type value; input >> value; ) {
45 container.emplace(value);
46 }
47
48 return container;
49}
50
51auto
52build_data_set(string_view filename)
53{
54 vector<unsigned long> values;
55
56 for ( const auto& line: read_file(filename) ) {
57 const auto cards = split(line, ':');
58 const auto numbers = split(cards[1], '|');
59
60 const auto winning_numbers = read_ints<set<int>>(numbers[0]);
61 const auto card_numbers = read_ints<set<int>>(numbers[1]);
62
63 auto count = 0U;
64 for ( auto number: winning_numbers ) {
65 if ( card_numbers.contains(number) ) {
66 ++count;
67 }
68 }
69 values.emplace_back(count);
70 }
71
72 return values;
73}
74
75void
76pass1(const vector<unsigned long>& values)
77{
78 auto sum = 0U;
79 for ( auto value: values ) {
80 if ( value != 0 ) {
81 sum += (1U << (value - 1));
82 }
83 }
84
85 cout << sum << endl;
86}
87
88void
89pass2(const vector<unsigned long>& values)
90{
91 auto count = 0U;
92
93 function<void(size_t, size_t)> process = [&](size_t start, size_t end) -> void {
94 for ( ; start != end; ++start ) {
95 auto value = values[start];
96
97 ++count;
98
99 process(start + 1, start + value + 1);
100 }
101 };
102
103 process(0, values.size());
104
105 cout << count << endl;
106}
107
108int
109main()
110{
111 const auto values = build_data_set("data/day04.txt");
112 pass1(values);
113 pass2(values);
114}