diff options
Diffstat (limited to '2025/src')
| -rw-r--r-- | 2025/src/day06.cpp | 125 |
1 files changed, 59 insertions, 66 deletions
diff --git a/2025/src/day06.cpp b/2025/src/day06.cpp index af562cd..63d7fe2 100644 --- a/2025/src/day06.cpp +++ b/2025/src/day06.cpp | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | #include <filesystem> | 1 | #include <filesystem> |
| 2 | #include <fstream> | 2 | #include <fstream> |
| 3 | #include <iostream> | 3 | #include <iostream> |
| 4 | #include <numeric> | ||
| 4 | #include <sstream> | 5 | #include <sstream> |
| 5 | #include <string> | 6 | #include <string> |
| 6 | #include <tuple> | ||
| 7 | #include <vector> | 7 | #include <vector> |
| 8 | 8 | ||
| 9 | using namespace std; | 9 | using namespace std; |
| @@ -26,93 +26,100 @@ read_file(const filesystem::path& filename) | |||
| 26 | return lines; | 26 | return lines; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | vector<char> | 29 | Ops |
| 30 | read_ops(const string& line) | 30 | read_ops(const string& line) |
| 31 | { | 31 | { |
| 32 | stringstream strm{ line }; | 32 | stringstream strm{ line }; |
| 33 | vector<char> ops; | 33 | Ops ops; |
| 34 | for ( char chr{}; strm >> chr; ) { | 34 | for ( char chr{}; strm >> chr; ) { |
| 35 | ops.emplace_back(chr); | 35 | ops.emplace_back(chr); |
| 36 | } | 36 | } |
| 37 | return ops; | 37 | return ops; |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | vector<long> | 40 | Ops |
| 41 | read_numbers(const string& line) | 41 | extract_ops(vector<string>& lines) |
| 42 | { | 42 | { |
| 43 | stringstream strm{ line }; | 43 | auto ops = read_ops(lines.back()); |
| 44 | vector<long> numbers; | 44 | ranges::reverse(ops); |
| 45 | for ( long number{}; strm >> number; ) { | 45 | lines.pop_back(); |
| 46 | numbers.emplace_back(number); | 46 | |
| 47 | } | 47 | return ops; |
| 48 | return numbers; | ||
| 49 | } | 48 | } |
| 50 | 49 | ||
| 51 | void | 50 | template<typename Matrix> |
| 52 | part1(vector<string> lines) | 51 | Matrix |
| 52 | rotateLeft90(const Matrix& matrix) | ||
| 53 | { | 53 | { |
| 54 | ranges::reverse(lines); | 54 | using Row = typename Matrix::value_type; |
| 55 | using T = typename Row::value_type; | ||
| 55 | 56 | ||
| 56 | const auto ops = read_ops(lines[0]); | 57 | if ( matrix.empty() ) { |
| 57 | 58 | return Matrix{}; | |
| 58 | vector<vector<long>> grid; | ||
| 59 | for ( size_t i = 1; i < lines.size(); ++i ) { | ||
| 60 | grid.emplace_back(read_numbers(lines[i])); | ||
| 61 | } | 59 | } |
| 62 | 60 | ||
| 63 | long sum = 0; | 61 | const size_t rows = matrix.size(); |
| 64 | for ( size_t col = 0; col != ops.size(); ++col ) { | 62 | const size_t cols = matrix[0].size(); |
| 65 | long value = ops[col] == '*' ? 1 : 0; | 63 | |
| 66 | for ( auto& row: grid ) { | 64 | Matrix result(cols, Row(rows, T{})); |
| 67 | if ( ops[col] == '*' ) { | 65 | |
| 68 | value *= row[col]; | 66 | for ( size_t row = 0; row != rows; ++row ) { |
| 69 | } | 67 | for ( size_t col = 0; col != cols; ++col ) { |
| 70 | else { | 68 | result[cols - 1 - col][row] = matrix[row][col]; |
| 71 | value += row[col]; | ||
| 72 | } | ||
| 73 | } | 69 | } |
| 74 | sum += value; | ||
| 75 | } | 70 | } |
| 76 | cout << "Part 1: " << sum << '\n'; | 71 | return result; |
| 77 | } | 72 | } |
| 78 | 73 | ||
| 79 | vector<string> | 74 | long |
| 80 | rotateLeft90(const vector<string>& matrix) | 75 | calculate(const Grid& grid, const Ops& ops) |
| 81 | { | 76 | { |
| 82 | if ( matrix.empty() ) { | 77 | long sum = 0; |
| 83 | return {}; | 78 | for ( size_t row = 0; row != ops.size(); ++row ) { |
| 79 | const auto& line = grid[row]; | ||
| 80 | if ( ops[row] == '*' ) { | ||
| 81 | sum += accumulate(line.begin(), line.end(), 1L, multiplies<>()); | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | sum += accumulate(line.begin(), line.end(), 0L); | ||
| 85 | } | ||
| 84 | } | 86 | } |
| 87 | return sum; | ||
| 88 | } | ||
| 85 | 89 | ||
| 86 | auto rows = matrix.size(); | 90 | void |
| 87 | auto cols = matrix[0].size(); | 91 | part1(vector<string> lines) |
| 88 | 92 | { | |
| 89 | vector<string> res(cols, string(rows, ' ')); | 93 | const auto ops = extract_ops(lines); |
| 90 | 94 | ||
| 91 | for ( size_t row = 0; row != rows; ++row ) { | 95 | Grid grid; |
| 92 | for ( size_t col = 0; col != cols; ++col ) { | 96 | grid.reserve(lines.size()); |
| 93 | // Position aus ursprünglicher Matrix auf neue übertragen | 97 | for ( const auto& line: lines ) { |
| 94 | res[cols - 1 - col][row] = matrix[row][col]; | 98 | stringstream strm{ line }; |
| 99 | vector<long> numbers; | ||
| 100 | for ( long number{}; strm >> number; ) { | ||
| 101 | numbers.emplace_back(number); | ||
| 95 | } | 102 | } |
| 103 | |||
| 104 | grid.emplace_back(numbers); | ||
| 96 | } | 105 | } |
| 97 | 106 | ||
| 98 | return res; | 107 | grid = rotateLeft90(grid); |
| 108 | |||
| 109 | cout << "Part 1: " << calculate(grid, ops) << '\n'; | ||
| 99 | } | 110 | } |
| 100 | 111 | ||
| 101 | void | 112 | void |
| 102 | part2(vector<string> lines) | 113 | part2(vector<string> lines) |
| 103 | { | 114 | { |
| 104 | auto ops = read_ops(lines.back()); | 115 | const auto ops = extract_ops(lines); |
| 105 | ranges::reverse(ops); | ||
| 106 | lines.pop_back(); // ops entfernen | ||
| 107 | 116 | ||
| 108 | // Matrix drehen | ||
| 109 | lines = rotateLeft90(lines); | 117 | lines = rotateLeft90(lines); |
| 110 | lines.emplace_back(""); | 118 | lines.emplace_back(""); |
| 111 | 119 | ||
| 112 | vector<vector<long>> grid; | 120 | Grid grid; |
| 113 | vector<long> curr; | 121 | vector<long> curr; |
| 114 | 122 | ||
| 115 | // Matrix neu aufbauen | ||
| 116 | for ( const auto& line: lines ) { | 123 | for ( const auto& line: lines ) { |
| 117 | if ( long number{}; sscanf(line.data(), "%ld", &number) == 1 ) { | 124 | if ( long number{}; sscanf(line.data(), "%ld", &number) == 1 ) { |
| 118 | curr.emplace_back(number); | 125 | curr.emplace_back(number); |
| @@ -123,21 +130,7 @@ part2(vector<string> lines) | |||
| 123 | } | 130 | } |
| 124 | } | 131 | } |
| 125 | 132 | ||
| 126 | // ausrechnen | 133 | cout << "Part 2: " << calculate(grid, ops) << '\n'; |
| 127 | long sum = 0; | ||
| 128 | for ( size_t row = 0; row != ops.size(); ++row ) { | ||
| 129 | long value = ops[row] == '*' ? 1 : 0; | ||
| 130 | for ( const auto number: grid[row] ) { | ||
| 131 | if ( ops[row] == '*' ) { | ||
| 132 | value *= number; | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | value += number; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | sum += value; | ||
| 139 | } | ||
| 140 | cout << "Part 2: " << sum << '\n'; | ||
| 141 | } | 134 | } |
| 142 | 135 | ||
| 143 | } // namespace | 136 | } // namespace |
