diff options
Diffstat (limited to '2025/src')
| -rw-r--r-- | 2025/src/day01.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/2025/src/day01.cpp b/2025/src/day01.cpp new file mode 100644 index 0000000..aa1ea42 --- /dev/null +++ b/2025/src/day01.cpp | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | #include <filesystem> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <string> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | namespace { | ||
| 10 | |||
| 11 | vector<string> | ||
| 12 | read_file(const filesystem::path& filename) | ||
| 13 | { | ||
| 14 | ifstream file{ filename }; | ||
| 15 | vector<string> lines; | ||
| 16 | |||
| 17 | for ( string line; getline(file, line); ) { | ||
| 18 | lines.emplace_back(line); | ||
| 19 | } | ||
| 20 | |||
| 21 | return lines; | ||
| 22 | } | ||
| 23 | |||
| 24 | void | ||
| 25 | part1(const vector<string>& lines) | ||
| 26 | { | ||
| 27 | auto zeros = 0; | ||
| 28 | auto pos = 50; | ||
| 29 | |||
| 30 | for ( const auto& line: lines ) { | ||
| 31 | auto value = stoi(line.substr(1)); | ||
| 32 | value %= 100; | ||
| 33 | pos += (line[0] == 'L') ? (100 - value) : value; | ||
| 34 | if ( pos % 100 == 0 ) { | ||
| 35 | ++zeros; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | cout << "Part 1: " << zeros << '\n'; | ||
| 39 | } | ||
| 40 | |||
| 41 | void | ||
| 42 | part2(const vector<string>& lines) | ||
| 43 | { | ||
| 44 | auto zeros = 0; | ||
| 45 | auto pos = 50; | ||
| 46 | |||
| 47 | for ( const auto& line: lines ) { | ||
| 48 | auto value = stoi(line.substr(1)); | ||
| 49 | while ( value-- > 0 ) { | ||
| 50 | pos += (line[0] == 'L') ? 99 : 1; | ||
| 51 | if ( pos % 100 == 0 ) { | ||
| 52 | ++zeros; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | cout << "Part 2: " << zeros << '\n'; | ||
| 57 | } | ||
| 58 | |||
| 59 | } // namespace | ||
| 60 | |||
| 61 | int | ||
| 62 | main() | ||
| 63 | { | ||
| 64 | auto data = read_file("data/day01.txt"); | ||
| 65 | part1(data); | ||
| 66 | part2(data); | ||
| 67 | } | ||
