diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-02 20:23:16 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-02 20:23:16 +0100 |
| commit | 1c2555b3574b9498347b8254ec5be90d9e0015f8 (patch) | |
| tree | f43a5e8dc523af49cdd5a2c7389a3977b0da9c88 /src/day04.cpp | |
| parent | e95d22d3927549caa5c259a59aedaa67d3b636ca (diff) | |
| download | advent-of-code-1c2555b3574b9498347b8254ec5be90d9e0015f8.tar.gz advent-of-code-1c2555b3574b9498347b8254ec5be90d9e0015f8.tar.bz2 advent-of-code-1c2555b3574b9498347b8254ec5be90d9e0015f8.zip | |
clean up
Diffstat (limited to 'src/day04.cpp')
| -rw-r--r-- | src/day04.cpp | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/day04.cpp b/src/day04.cpp index 8afcb2c..0d3e552 100644 --- a/src/day04.cpp +++ b/src/day04.cpp | |||
| @@ -1,4 +1,3 @@ | |||
| 1 | #include <cctype> | ||
| 2 | #include <fstream> | 1 | #include <fstream> |
| 3 | #include <iostream> | 2 | #include <iostream> |
| 4 | #include <set> | 3 | #include <set> |
| @@ -25,8 +24,8 @@ read_file(string_view filename) | |||
| 25 | vector<string> | 24 | vector<string> |
| 26 | split(const string& line, char sep) | 25 | split(const string& line, char sep) |
| 27 | { | 26 | { |
| 28 | vector<string> parts{}; | ||
| 29 | stringstream input{ line }; | 27 | stringstream input{ line }; |
| 28 | vector<string> parts; | ||
| 30 | 29 | ||
| 31 | for ( string part; getline(input, part, sep); ) { | 30 | for ( string part; getline(input, part, sep); ) { |
| 32 | parts.emplace_back(part); | 31 | parts.emplace_back(part); |
| @@ -49,13 +48,12 @@ read_ints(const string& line) | |||
| 49 | return container; | 48 | return container; |
| 50 | } | 49 | } |
| 51 | 50 | ||
| 52 | vector<unsigned long> | 51 | auto |
| 53 | build_data_set() | 52 | build_data_set(string_view filename) |
| 54 | { | 53 | { |
| 55 | const auto lines = read_file("data/day04.txt"); | 54 | vector<unsigned long> values; |
| 56 | vector<unsigned long> values{}; | ||
| 57 | 55 | ||
| 58 | for ( const auto& line: lines ) { | 56 | for ( const auto& line: read_file(filename) ) { |
| 59 | const auto cards = split(line, ':'); | 57 | const auto cards = split(line, ':'); |
| 60 | const auto numbers = split(cards[1], '|'); | 58 | const auto numbers = split(cards[1], '|'); |
| 61 | 59 | ||
| @@ -75,10 +73,10 @@ build_data_set() | |||
| 75 | } | 73 | } |
| 76 | 74 | ||
| 77 | void | 75 | void |
| 78 | pass1() | 76 | pass1(const vector<unsigned long>& values) |
| 79 | { | 77 | { |
| 80 | auto sum = 0U; | 78 | auto sum = 0U; |
| 81 | for ( auto value: build_data_set() ) { | 79 | for ( auto value: values ) { |
| 82 | if ( value != 0 ) { | 80 | if ( value != 0 ) { |
| 83 | sum += (1U << (value - 1)); | 81 | sum += (1U << (value - 1)); |
| 84 | } | 82 | } |
| @@ -88,10 +86,9 @@ pass1() | |||
| 88 | } | 86 | } |
| 89 | 87 | ||
| 90 | void | 88 | void |
| 91 | pass2() | 89 | pass2(const vector<unsigned long>& values) |
| 92 | { | 90 | { |
| 93 | const auto values = build_data_set(); | 91 | auto count = 0U; |
| 94 | auto count = 0U; | ||
| 95 | 92 | ||
| 96 | function<void(size_t, size_t)> process = [&](size_t start, size_t end) -> void { | 93 | function<void(size_t, size_t)> process = [&](size_t start, size_t end) -> void { |
| 97 | for ( ; start != end; ++start ) { | 94 | for ( ; start != end; ++start ) { |
| @@ -111,6 +108,7 @@ pass2() | |||
| 111 | int | 108 | int |
| 112 | main() | 109 | main() |
| 113 | { | 110 | { |
| 114 | pass1(); | 111 | const auto values = build_data_set("data/day04.txt"); |
| 115 | pass2(); | 112 | pass1(values); |
| 113 | pass2(values); | ||
| 116 | } | 114 | } |
