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/day05.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/day05.cpp')
| -rw-r--r-- | src/day05.cpp | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/day05.cpp b/src/day05.cpp index 92e39fc..42fda29 100644 --- a/src/day05.cpp +++ b/src/day05.cpp | |||
| @@ -49,7 +49,7 @@ read_file(string_view filename) | |||
| 49 | vector<string> | 49 | vector<string> |
| 50 | split(const string& line, char sep) | 50 | split(const string& line, char sep) |
| 51 | { | 51 | { |
| 52 | vector<string> parts{}; | 52 | vector<string> parts; |
| 53 | stringstream input{ line }; | 53 | stringstream input{ line }; |
| 54 | 54 | ||
| 55 | for ( string part; getline(input, part, sep); ) { | 55 | for ( string part; getline(input, part, sep); ) { |
| @@ -60,10 +60,8 @@ split(const string& line, char sep) | |||
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | void | 62 | void |
| 63 | part1() | 63 | part1(const vector<string>& lines) |
| 64 | { | 64 | { |
| 65 | auto lines = read_file("data/day05.txt"); | ||
| 66 | |||
| 67 | auto read_ints = [](const string& line) { | 65 | auto read_ints = [](const string& line) { |
| 68 | stringstream iss{ line }; | 66 | stringstream iss{ line }; |
| 69 | return vector<long>{ istream_iterator<long>{ iss }, istream_iterator<long>{} }; | 67 | return vector<long>{ istream_iterator<long>{ iss }, istream_iterator<long>{} }; |
| @@ -71,8 +69,8 @@ part1() | |||
| 71 | 69 | ||
| 72 | auto seeds = read_ints(lines[0].substr(6)); | 70 | auto seeds = read_ints(lines[0].substr(6)); |
| 73 | 71 | ||
| 74 | map<string, string> category_mapping{}; | 72 | map<string, string> category_mapping; |
| 75 | map<string, vector<Entry>> range_mapping{}; | 73 | map<string, vector<Entry>> range_mapping; |
| 76 | 74 | ||
| 77 | // read blocks | 75 | // read blocks |
| 78 | for ( size_t idx = 1; idx < lines.size(); ) { | 76 | for ( size_t idx = 1; idx < lines.size(); ) { |
| @@ -84,7 +82,7 @@ part1() | |||
| 84 | 82 | ||
| 85 | category_mapping[parts[0]] = parts[2]; | 83 | category_mapping[parts[0]] = parts[2]; |
| 86 | 84 | ||
| 87 | vector<Entry> mapping{}; | 85 | vector<Entry> mapping; |
| 88 | 86 | ||
| 89 | for ( ; idx < lines.size() && !lines[idx].empty(); ++idx ) { | 87 | for ( ; idx < lines.size() && !lines[idx].empty(); ++idx ) { |
| 90 | auto values = read_ints(lines[idx]); | 88 | auto values = read_ints(lines[idx]); |
| @@ -117,10 +115,8 @@ part1() | |||
| 117 | } | 115 | } |
| 118 | 116 | ||
| 119 | void | 117 | void |
| 120 | part2() | 118 | part2(const vector<string>& lines) |
| 121 | { | 119 | { |
| 122 | auto lines = read_file("data/day05-sample1.txt"); | ||
| 123 | |||
| 124 | auto read_ints = [](const string& line) { | 120 | auto read_ints = [](const string& line) { |
| 125 | stringstream iss{ line }; | 121 | stringstream iss{ line }; |
| 126 | return vector<long>{ istream_iterator<long>{ iss }, istream_iterator<long>{} }; | 122 | return vector<long>{ istream_iterator<long>{ iss }, istream_iterator<long>{} }; |
| @@ -128,8 +124,8 @@ part2() | |||
| 128 | 124 | ||
| 129 | auto seeds = read_ints(lines[0].substr(6)); | 125 | auto seeds = read_ints(lines[0].substr(6)); |
| 130 | 126 | ||
| 131 | map<string, string> category_mapping{}; | 127 | map<string, string> category_mapping; |
| 132 | map<string, vector<Entry>> range_mapping{}; | 128 | map<string, vector<Entry>> range_mapping; |
| 133 | 129 | ||
| 134 | // read blocks | 130 | // read blocks |
| 135 | for ( size_t idx = 1; idx < lines.size(); ) { | 131 | for ( size_t idx = 1; idx < lines.size(); ) { |
| @@ -141,7 +137,7 @@ part2() | |||
| 141 | 137 | ||
| 142 | category_mapping[parts[0]] = parts[2]; | 138 | category_mapping[parts[0]] = parts[2]; |
| 143 | 139 | ||
| 144 | vector<Entry> mapping{}; | 140 | vector<Entry> mapping; |
| 145 | 141 | ||
| 146 | for ( ; idx < lines.size() && !lines[idx].empty(); ++idx ) { | 142 | for ( ; idx < lines.size() && !lines[idx].empty(); ++idx ) { |
| 147 | auto values = read_ints(lines[idx]); | 143 | auto values = read_ints(lines[idx]); |
| @@ -181,6 +177,7 @@ part2() | |||
| 181 | int | 177 | int |
| 182 | main() | 178 | main() |
| 183 | { | 179 | { |
| 184 | part1(); | 180 | auto lines = read_file("data/day05.txt"); |
| 185 | part2(); | 181 | part1(lines); |
| 182 | part2(lines); | ||
| 186 | } | 183 | } |
