diff options
Diffstat (limited to '2025/src')
| -rw-r--r-- | 2025/src/day03.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/2025/src/day03.cpp b/2025/src/day03.cpp new file mode 100644 index 0000000..c131255 --- /dev/null +++ b/2025/src/day03.cpp | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cctype> | ||
| 3 | #include <filesystem> | ||
| 4 | #include <fstream> | ||
| 5 | #include <iostream> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | namespace { | ||
| 11 | |||
| 12 | vector<vector<long>> | ||
| 13 | read_file(const filesystem::path& filename) | ||
| 14 | { | ||
| 15 | ifstream file{ filename }; | ||
| 16 | vector<vector<long>> lines; | ||
| 17 | |||
| 18 | for ( string line; getline(file, line); ) { | ||
| 19 | vector<long> numbers; | ||
| 20 | for ( auto chr: line ) { | ||
| 21 | if ( isdigit(chr) ) { | ||
| 22 | numbers.emplace_back(chr - '0'); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | lines.emplace_back(std::move(numbers)); | ||
| 26 | } | ||
| 27 | |||
| 28 | return lines; | ||
| 29 | } | ||
| 30 | |||
| 31 | void | ||
| 32 | part1(const vector<vector<long>>& lines) | ||
| 33 | { | ||
| 34 | long sum = 0; | ||
| 35 | for ( const auto& line: lines ) { | ||
| 36 | auto first = max_element(line.begin(), prev(line.end())); | ||
| 37 | auto second = max_element(next(first), line.end()); | ||
| 38 | sum += *first * 10 + *second; | ||
| 39 | } | ||
| 40 | cout << "Part 1: " << sum << '\n'; | ||
| 41 | } | ||
| 42 | |||
| 43 | } // namespace | ||
| 44 | |||
| 45 | int | ||
| 46 | main() | ||
| 47 | { | ||
| 48 | auto lines = read_file("data/day03.txt"); | ||
| 49 | part1(lines); | ||
| 50 | } | ||
