diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-12-03 17:02:20 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-12-03 17:02:20 +0100 |
| commit | 765223e3a1bd4d3be8cb2c8b11e2b836e5690038 (patch) | |
| tree | 101206208bf048761363df60e8a38fdb0e7a4070 | |
| parent | 32914de7c6aba10963ada32ba6fdfeb47063b4c3 (diff) | |
| download | advent-of-code-765223e3a1bd4d3be8cb2c8b11e2b836e5690038.tar.gz advent-of-code-765223e3a1bd4d3be8cb2c8b11e2b836e5690038.tar.bz2 advent-of-code-765223e3a1bd4d3be8cb2c8b11e2b836e5690038.zip | |
aoc 2025, day 3, part 1
| -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 | } | ||
