diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/day04.cpp | 26 | ||||
| -rw-r--r-- | src/day05.cpp | 27 | ||||
| -rw-r--r-- | src/day09.cpp | 13 | ||||
| -rw-r--r-- | src/day10.cpp | 19 | ||||
| -rw-r--r-- | src/day12.cpp | 7 | ||||
| -rw-r--r-- | src/day14.cpp | 22 | ||||
| -rw-r--r-- | src/day16.cpp | 4 |
7 files changed, 50 insertions, 68 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 | } |
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 | } |
diff --git a/src/day09.cpp b/src/day09.cpp index 0019d05..4b30e6e 100644 --- a/src/day09.cpp +++ b/src/day09.cpp | |||
| @@ -45,10 +45,8 @@ solve_rec(const vector<T>& values) | |||
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | void | 47 | void |
| 48 | part1() | 48 | part1(const vector<string>& lines) |
| 49 | { | 49 | { |
| 50 | auto lines = read_file("data/day09.txt"); | ||
| 51 | |||
| 52 | long sum = 0; | 50 | long sum = 0; |
| 53 | for ( const auto& line: lines ) { | 51 | for ( const auto& line: lines ) { |
| 54 | sum += solve_rec(read_ints(line)); | 52 | sum += solve_rec(read_ints(line)); |
| @@ -57,10 +55,10 @@ part1() | |||
| 57 | } | 55 | } |
| 58 | 56 | ||
| 59 | void | 57 | void |
| 60 | part2() | 58 | part2(const vector<string>& lines) |
| 61 | { | 59 | { |
| 62 | long sum = 0; | 60 | long sum = 0; |
| 63 | for ( const auto& line: read_file("data/day09.txt") ) { | 61 | for ( const auto& line: lines ) { |
| 64 | auto values = read_ints(line); | 62 | auto values = read_ints(line); |
| 65 | reverse(values.begin(), values.end()); | 63 | reverse(values.begin(), values.end()); |
| 66 | sum += solve_rec(values); | 64 | sum += solve_rec(values); |
| @@ -71,6 +69,7 @@ part2() | |||
| 71 | int | 69 | int |
| 72 | main() | 70 | main() |
| 73 | { | 71 | { |
| 74 | part1(); | 72 | auto lines = read_file("data/day09.txt"); |
| 75 | part2(); | 73 | part1(lines); |
| 74 | part2(lines); | ||
| 76 | } | 75 | } |
diff --git a/src/day10.cpp b/src/day10.cpp index 5591290..264e111 100644 --- a/src/day10.cpp +++ b/src/day10.cpp | |||
| @@ -38,8 +38,7 @@ find_start_pos(const puzzle_t& puzzle) | |||
| 38 | bool | 38 | bool |
| 39 | predict_direction(const puzzle_t& puzzle, pos_t& current, char& direction) | 39 | predict_direction(const puzzle_t& puzzle, pos_t& current, char& direction) |
| 40 | { | 40 | { |
| 41 | auto x = get<0>(current); // NOLINT | 41 | auto [x, y] = current; |
| 42 | auto y = get<1>(current); // NOLINT | ||
| 43 | 42 | ||
| 44 | if ( direction == 'S' ) { | 43 | if ( direction == 'S' ) { |
| 45 | ++y; | 44 | ++y; |
| @@ -115,10 +114,8 @@ predict_direction(const puzzle_t& puzzle, pos_t& current, char& direction) | |||
| 115 | }; | 114 | }; |
| 116 | 115 | ||
| 117 | void | 116 | void |
| 118 | part1() | 117 | part1(const puzzle_t& puzzle) |
| 119 | { | 118 | { |
| 120 | const auto puzzle = read_file("data/day10.txt"); | ||
| 121 | |||
| 122 | const auto start_pos = find_start_pos(puzzle); | 119 | const auto start_pos = find_start_pos(puzzle); |
| 123 | 120 | ||
| 124 | auto max_steps = 0; | 121 | auto max_steps = 0; |
| @@ -163,10 +160,8 @@ flood_fill(puzzle_t& puzzle, size_t x, size_t y) | |||
| 163 | }; | 160 | }; |
| 164 | 161 | ||
| 165 | void | 162 | void |
| 166 | part2() | 163 | part2(const puzzle_t& puzzle_original) |
| 167 | { | 164 | { |
| 168 | const auto puzzle_original = read_file("data/day10.txt"); | ||
| 169 | |||
| 170 | puzzle_t puzzle{ puzzle_original.size(), string(puzzle_original[0].size(), ' ') }; | 165 | puzzle_t puzzle{ puzzle_original.size(), string(puzzle_original[0].size(), ' ') }; |
| 171 | 166 | ||
| 172 | const auto start_pos = find_start_pos(puzzle_original); | 167 | const auto start_pos = find_start_pos(puzzle_original); |
| @@ -176,8 +171,7 @@ part2() | |||
| 176 | auto current_pos = start_pos; | 171 | auto current_pos = start_pos; |
| 177 | 172 | ||
| 178 | for ( ;; ) { | 173 | for ( ;; ) { |
| 179 | auto x = get<0>(current_pos); // NOLINT | 174 | auto [x, y] = current_pos; |
| 180 | auto y = get<1>(current_pos); // NOLINT | ||
| 181 | 175 | ||
| 182 | puzzle[y][x] = puzzle_original[y][x]; | 176 | puzzle[y][x] = puzzle_original[y][x]; |
| 183 | 177 | ||
| @@ -216,6 +210,7 @@ part2() | |||
| 216 | int | 210 | int |
| 217 | main() | 211 | main() |
| 218 | { | 212 | { |
| 219 | part1(); | 213 | const auto puzzle = read_file("data/day10.txt"); |
| 220 | part2(); | 214 | part1(puzzle); |
| 215 | part2(puzzle); | ||
| 221 | } | 216 | } |
diff --git a/src/day12.cpp b/src/day12.cpp index 31a2739..b01b2fb 100644 --- a/src/day12.cpp +++ b/src/day12.cpp | |||
| @@ -104,10 +104,8 @@ brute_force(string_view puzzle, const vector<long>& nums) | |||
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | void | 106 | void |
| 107 | part1() | 107 | part1(const vector<string>& input) |
| 108 | { | 108 | { |
| 109 | const auto input = read_file("data/day12.txt"); | ||
| 110 | |||
| 111 | auto sum = 0L; | 109 | auto sum = 0L; |
| 112 | for ( const auto& line: input ) { | 110 | for ( const auto& line: input ) { |
| 113 | auto parts = split(line, ' '); | 111 | auto parts = split(line, ' '); |
| @@ -122,5 +120,6 @@ part1() | |||
| 122 | int | 120 | int |
| 123 | main() | 121 | main() |
| 124 | { | 122 | { |
| 125 | part1(); | 123 | const auto input = read_file("data/day12.txt"); |
| 124 | part1(input); | ||
| 126 | } | 125 | } |
diff --git a/src/day14.cpp b/src/day14.cpp index 2b7a5b8..8816ce1 100644 --- a/src/day14.cpp +++ b/src/day14.cpp | |||
| @@ -21,10 +21,8 @@ read_file(string_view filename) | |||
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | void | 23 | void |
| 24 | part1() | 24 | part1(const vector<string>& lines) |
| 25 | { | 25 | { |
| 26 | const auto lines = read_file("data/day14.txt"); | ||
| 27 | |||
| 28 | auto start = chrono::steady_clock::now(); | 26 | auto start = chrono::steady_clock::now(); |
| 29 | 27 | ||
| 30 | size_t sum = 0; | 28 | size_t sum = 0; |
| @@ -182,10 +180,9 @@ tilt_east(vector<string>& lines) | |||
| 182 | void | 180 | void |
| 183 | tilt(vector<string>& lines) | 181 | tilt(vector<string>& lines) |
| 184 | { | 182 | { |
| 185 | tilt_north(lines); | 183 | for ( auto tilt: { tilt_north, tilt_west, tilt_south, tilt_east } ) { |
| 186 | tilt_west(lines); | 184 | tilt(lines); |
| 187 | tilt_south(lines); | 185 | } |
| 188 | tilt_east(lines); | ||
| 189 | } | 186 | } |
| 190 | 187 | ||
| 191 | size_t | 188 | size_t |
| @@ -201,16 +198,14 @@ calc(const vector<string>& lines) | |||
| 201 | } | 198 | } |
| 202 | 199 | ||
| 203 | void | 200 | void |
| 204 | part2() | 201 | part2(vector<string> lines) |
| 205 | { | 202 | { |
| 206 | auto lines = read_file("data/day14.txt"); | ||
| 207 | |||
| 208 | auto start = chrono::steady_clock::now(); | 203 | auto start = chrono::steady_clock::now(); |
| 209 | map<vector<string>, long> cache; | 204 | map<vector<string>, long> cache; |
| 210 | 205 | ||
| 211 | const long dest = 1'000'000'000; | 206 | const long dest = 1'000'000'000; |
| 212 | 207 | ||
| 213 | for ( long index = 0; true; ++index ) { | 208 | for ( long index = 0;; ++index ) { |
| 214 | auto iter = cache.find(lines); | 209 | auto iter = cache.find(lines); |
| 215 | if ( iter != cache.end() ) { | 210 | if ( iter != cache.end() ) { |
| 216 | auto offset = iter->second; | 211 | auto offset = iter->second; |
| @@ -238,6 +233,7 @@ part2() | |||
| 238 | int | 233 | int |
| 239 | main() | 234 | main() |
| 240 | { | 235 | { |
| 241 | part1(); | 236 | const auto lines = read_file("data/day14.txt"); |
| 242 | part2(); | 237 | part1(lines); |
| 238 | part2(lines); | ||
| 243 | } | 239 | } |
diff --git a/src/day16.cpp b/src/day16.cpp index c2cd365..93c7479 100644 --- a/src/day16.cpp +++ b/src/day16.cpp | |||
| @@ -44,9 +44,7 @@ solve(const vector<string>& lines, tuple<size_t, size_t, unsigned int> start) | |||
| 44 | positions.emplace(start); | 44 | positions.emplace(start); |
| 45 | 45 | ||
| 46 | while ( !positions.empty() ) { | 46 | while ( !positions.empty() ) { |
| 47 | auto row = get<0>(positions.front()); | 47 | auto [row, col, dir] = positions.front(); |
| 48 | auto col = get<1>(positions.front()); | ||
| 49 | auto dir = get<2>(positions.front()); | ||
| 50 | positions.pop(); | 48 | positions.pop(); |
| 51 | 49 | ||
| 52 | while ( true ) { | 50 | while ( true ) { |
