diff options
Diffstat (limited to '2025/src/day06.cpp')
| -rw-r--r-- | 2025/src/day06.cpp | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/2025/src/day06.cpp b/2025/src/day06.cpp new file mode 100644 index 0000000..af562cd --- /dev/null +++ b/2025/src/day06.cpp | |||
| @@ -0,0 +1,151 @@ | |||
| 1 | #include <filesystem> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <sstream> | ||
| 5 | #include <string> | ||
| 6 | #include <tuple> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | using Grid = vector<vector<long>>; | ||
| 14 | using Ops = vector<char>; | ||
| 15 | |||
| 16 | vector<string> | ||
| 17 | read_file(const filesystem::path& filename) | ||
| 18 | { | ||
| 19 | ifstream file{ filename }; | ||
| 20 | vector<string> lines; | ||
| 21 | |||
| 22 | for ( string line; getline(file, line); ) { | ||
| 23 | lines.emplace_back(line); | ||
| 24 | } | ||
| 25 | |||
| 26 | return lines; | ||
| 27 | } | ||
| 28 | |||
| 29 | vector<char> | ||
| 30 | read_ops(const string& line) | ||
| 31 | { | ||
| 32 | stringstream strm{ line }; | ||
| 33 | vector<char> ops; | ||
| 34 | for ( char chr{}; strm >> chr; ) { | ||
| 35 | ops.emplace_back(chr); | ||
| 36 | } | ||
| 37 | return ops; | ||
| 38 | } | ||
| 39 | |||
| 40 | vector<long> | ||
| 41 | read_numbers(const string& line) | ||
| 42 | { | ||
| 43 | stringstream strm{ line }; | ||
| 44 | vector<long> numbers; | ||
| 45 | for ( long number{}; strm >> number; ) { | ||
| 46 | numbers.emplace_back(number); | ||
| 47 | } | ||
| 48 | return numbers; | ||
| 49 | } | ||
| 50 | |||
| 51 | void | ||
| 52 | part1(vector<string> lines) | ||
| 53 | { | ||
| 54 | ranges::reverse(lines); | ||
| 55 | |||
| 56 | const auto ops = read_ops(lines[0]); | ||
| 57 | |||
| 58 | vector<vector<long>> grid; | ||
| 59 | for ( size_t i = 1; i < lines.size(); ++i ) { | ||
| 60 | grid.emplace_back(read_numbers(lines[i])); | ||
| 61 | } | ||
| 62 | |||
| 63 | long sum = 0; | ||
| 64 | for ( size_t col = 0; col != ops.size(); ++col ) { | ||
| 65 | long value = ops[col] == '*' ? 1 : 0; | ||
| 66 | for ( auto& row: grid ) { | ||
| 67 | if ( ops[col] == '*' ) { | ||
| 68 | value *= row[col]; | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | value += row[col]; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | sum += value; | ||
| 75 | } | ||
| 76 | cout << "Part 1: " << sum << '\n'; | ||
| 77 | } | ||
| 78 | |||
| 79 | vector<string> | ||
| 80 | rotateLeft90(const vector<string>& matrix) | ||
| 81 | { | ||
| 82 | if ( matrix.empty() ) { | ||
| 83 | return {}; | ||
| 84 | } | ||
| 85 | |||
| 86 | auto rows = matrix.size(); | ||
| 87 | auto cols = matrix[0].size(); | ||
| 88 | |||
| 89 | vector<string> res(cols, string(rows, ' ')); | ||
| 90 | |||
| 91 | for ( size_t row = 0; row != rows; ++row ) { | ||
| 92 | for ( size_t col = 0; col != cols; ++col ) { | ||
| 93 | // Position aus ursprünglicher Matrix auf neue übertragen | ||
| 94 | res[cols - 1 - col][row] = matrix[row][col]; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | return res; | ||
| 99 | } | ||
| 100 | |||
| 101 | void | ||
| 102 | part2(vector<string> lines) | ||
| 103 | { | ||
| 104 | auto ops = read_ops(lines.back()); | ||
| 105 | ranges::reverse(ops); | ||
| 106 | lines.pop_back(); // ops entfernen | ||
| 107 | |||
| 108 | // Matrix drehen | ||
| 109 | lines = rotateLeft90(lines); | ||
| 110 | lines.emplace_back(""); | ||
| 111 | |||
| 112 | vector<vector<long>> grid; | ||
| 113 | vector<long> curr; | ||
| 114 | |||
| 115 | // Matrix neu aufbauen | ||
| 116 | for ( const auto& line: lines ) { | ||
| 117 | if ( long number{}; sscanf(line.data(), "%ld", &number) == 1 ) { | ||
| 118 | curr.emplace_back(number); | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | grid.emplace_back(curr); | ||
| 122 | curr.clear(); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | // ausrechnen | ||
| 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 | } | ||
| 142 | |||
| 143 | } // namespace | ||
| 144 | |||
| 145 | int | ||
| 146 | main() | ||
| 147 | { | ||
| 148 | const auto lines = read_file("data/day06.txt"); | ||
| 149 | part1(lines); | ||
| 150 | part2(lines); | ||
| 151 | } | ||
