diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-03 20:16:40 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-03 20:16:40 +0100 |
| commit | dd82472232ebdbf372b4240e377845aa4da023ef (patch) | |
| tree | 0ec622ed01af6c5d87e2750a7363ac8492d46760 /src | |
| parent | 5708a617b10e328c499da837d764a51e98d8bcc0 (diff) | |
| download | advent-of-code-dd82472232ebdbf372b4240e377845aa4da023ef.tar.gz advent-of-code-dd82472232ebdbf372b4240e377845aa4da023ef.tar.bz2 advent-of-code-dd82472232ebdbf372b4240e377845aa4da023ef.zip | |
Trim den Code... ;-)
Diffstat (limited to 'src')
| -rw-r--r-- | src/day03.cpp | 77 |
1 files changed, 33 insertions, 44 deletions
diff --git a/src/day03.cpp b/src/day03.cpp index d5e3fff..9c5c276 100644 --- a/src/day03.cpp +++ b/src/day03.cpp | |||
| @@ -26,39 +26,46 @@ is_digit(char chr) | |||
| 26 | return std::isdigit(static_cast<unsigned char>(chr)) != 0; | 26 | return std::isdigit(static_cast<unsigned char>(chr)) != 0; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | vector<tuple<size_t, size_t>> | ||
| 30 | positions(size_t row, size_t col) | ||
| 31 | { | ||
| 32 | return vector<tuple<size_t, size_t>> { | ||
| 33 | { row - 1, col - 1 }, | ||
| 34 | { row - 0, col - 1 }, | ||
| 35 | { row + 1, col - 1 }, | ||
| 36 | |||
| 37 | { row - 1, col }, | ||
| 38 | { row + 1, col }, | ||
| 39 | |||
| 40 | { row - 1, col + 1 }, | ||
| 41 | { row - 0, col + 1 }, | ||
| 42 | { row + 1, col + 1 }, | ||
| 43 | }; | ||
| 44 | } | ||
| 45 | |||
| 29 | void | 46 | void |
| 30 | part1() // NOLINT | 47 | part1() // NOLINT |
| 31 | { | 48 | { |
| 32 | auto lines = read_file("data/day03.txt"); | 49 | auto lines = read_file("data/day03.txt"); |
| 33 | 50 | ||
| 34 | auto is_symbol = [&lines](int row, int col) -> bool { | 51 | auto is_symbol = [&lines](size_t row, size_t col) -> bool { |
| 35 | static const string symbols{ "*#+$%=-@&/" }; | 52 | static const string symbols{ "*#+$%=-@&/" }; |
| 36 | 53 | ||
| 37 | if ( row < 0 || col < 0 ) { | 54 | if ( row >= lines.size() || col >= lines[row].size() ) { |
| 38 | return false; | ||
| 39 | } | ||
| 40 | if ( size_t(row) >= lines.size() || size_t(col) >= lines[size_t(row)].size() ) { | ||
| 41 | return false; | 55 | return false; |
| 42 | } | 56 | } |
| 43 | 57 | ||
| 44 | auto symbol = lines[size_t(row)][size_t(col)]; | 58 | auto symbol = lines[row][col]; |
| 45 | 59 | ||
| 46 | return symbols.find(symbol) != string::npos; | 60 | return symbols.find(symbol) != string::npos; |
| 47 | }; | 61 | }; |
| 48 | 62 | ||
| 49 | auto bordering = [&is_symbol](int row, int col) -> bool { | 63 | auto bordering = [&is_symbol](size_t row, size_t col) -> bool { |
| 50 | bool result = false; | 64 | bool result = false; |
| 51 | 65 | ||
| 52 | result |= is_symbol(row - 1, col - 1); | 66 | for (const auto& position: positions(row, col)) { |
| 53 | result |= is_symbol(row - 0, col - 1); | 67 | result |= is_symbol(get<0>(position), get<1>(position)); |
| 54 | result |= is_symbol(row + 1, col - 1); | 68 | } |
| 55 | |||
| 56 | result |= is_symbol(row - 1, col); | ||
| 57 | result |= is_symbol(row + 1, col); | ||
| 58 | |||
| 59 | result |= is_symbol(row - 1, col + 1); | ||
| 60 | result |= is_symbol(row - 0, col + 1); | ||
| 61 | result |= is_symbol(row + 1, col + 1); | ||
| 62 | 69 | ||
| 63 | return result; | 70 | return result; |
| 64 | }; | 71 | }; |
| @@ -82,7 +89,7 @@ part1() // NOLINT | |||
| 82 | auto result = false; | 89 | auto result = false; |
| 83 | 90 | ||
| 84 | for ( auto col = start; col != end; ++col ) { | 91 | for ( auto col = start; col != end; ++col ) { |
| 85 | result |= bordering(int(row), int(col)); | 92 | result |= bordering(row, col); |
| 86 | } | 93 | } |
| 87 | 94 | ||
| 88 | if ( result ) { | 95 | if ( result ) { |
| @@ -97,7 +104,7 @@ part1() // NOLINT | |||
| 97 | } | 104 | } |
| 98 | 105 | ||
| 99 | void | 106 | void |
| 100 | part2() | 107 | part2() // NOLINT |
| 101 | { | 108 | { |
| 102 | auto lines = read_file("data/day03.txt"); | 109 | auto lines = read_file("data/day03.txt"); |
| 103 | 110 | ||
| @@ -138,35 +145,16 @@ part2() | |||
| 138 | 145 | ||
| 139 | auto col = line.find('*'); | 146 | auto col = line.find('*'); |
| 140 | while ( col != string::npos ) { | 147 | while ( col != string::npos ) { |
| 141 | int value = 0; | ||
| 142 | vector<int> values; | 148 | vector<int> values; |
| 149 | |||
| 143 | auto lines_copy{ lines }; | 150 | auto lines_copy{ lines }; |
| 144 | 151 | ||
| 145 | if ( find_integer(lines_copy, row - 1, col - 1, value) ) { | 152 | for ( const auto& position: positions(row, col) ) { |
| 146 | values.emplace_back(value); | 153 | auto value = 0; |
| 147 | } | ||
| 148 | if ( find_integer(lines_copy, row - 0, col - 1, value) ) { | ||
| 149 | values.emplace_back(value); | ||
| 150 | } | ||
| 151 | if ( find_integer(lines_copy, row + 1, col - 1, value) ) { | ||
| 152 | values.emplace_back(value); | ||
| 153 | } | ||
| 154 | 154 | ||
| 155 | if ( find_integer(lines_copy, row - 1, col, value) ) { | 155 | if ( find_integer(lines_copy, get<0>(position), get<1>(position), value) ) { |
| 156 | values.emplace_back(value); | 156 | values.emplace_back(value); |
| 157 | } | 157 | } |
| 158 | if ( find_integer(lines_copy, row + 1, col, value) ) { | ||
| 159 | values.emplace_back(value); | ||
| 160 | } | ||
| 161 | |||
| 162 | if ( find_integer(lines_copy, row - 1, col + 1, value) ) { | ||
| 163 | values.emplace_back(value); | ||
| 164 | } | ||
| 165 | if ( find_integer(lines_copy, row - 0, col + 1, value) ) { | ||
| 166 | values.emplace_back(value); | ||
| 167 | } | ||
| 168 | if ( find_integer(lines_copy, row + 1, col + 1, value) ) { | ||
| 169 | values.emplace_back(value); | ||
| 170 | } | 158 | } |
| 171 | 159 | ||
| 172 | if ( values.size() == 2 ) { | 160 | if ( values.size() == 2 ) { |
| @@ -183,5 +171,6 @@ part2() | |||
| 183 | int | 171 | int |
| 184 | main() | 172 | main() |
| 185 | { | 173 | { |
| 174 | part1(); | ||
| 186 | part2(); | 175 | part2(); |
| 187 | } | 176 | } |
