aboutsummaryrefslogtreecommitdiff
path: root/2025
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-12-06 11:35:06 +0100
committerThomas Schmucker <ts@its1.de>2025-12-06 11:35:06 +0100
commitc11df06541075ef464559adba1882b1f2ade2925 (patch)
treee20e24953980931217e905862cecb4e1860ef17f /2025
parent7634cf6ee1a1681586cc550942eaff87076add02 (diff)
downloadadvent-of-code-c11df06541075ef464559adba1882b1f2ade2925.tar.gz
advent-of-code-c11df06541075ef464559adba1882b1f2ade2925.tar.bz2
advent-of-code-c11df06541075ef464559adba1882b1f2ade2925.zip
aoc 2025, day 6, cleaned up
Diffstat (limited to '2025')
-rw-r--r--2025/src/day06.cpp125
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
9using namespace std; 9using namespace std;
@@ -26,93 +26,100 @@ read_file(const filesystem::path& filename)
26 return lines; 26 return lines;
27} 27}
28 28
29vector<char> 29Ops
30read_ops(const string& line) 30read_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
40vector<long> 40Ops
41read_numbers(const string& line) 41extract_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
51void 50template<typename Matrix>
52part1(vector<string> lines) 51Matrix
52rotateLeft90(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
79vector<string> 74long
80rotateLeft90(const vector<string>& matrix) 75calculate(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(); 90void
87 auto cols = matrix[0].size(); 91part1(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
101void 112void
102part2(vector<string> lines) 113part2(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