diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-11 12:40:07 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-11 12:40:07 +0100 |
| commit | f0638b6cef379829127dc352a227b3d43ba383cc (patch) | |
| tree | 5cfd828dfb9a331486f7c7db8b7ea59ec3820cc2 | |
| parent | a2ade0cda1d725b274450bd51eed8214d480cee8 (diff) | |
| download | advent-of-code-f0638b6cef379829127dc352a227b3d43ba383cc.tar.gz advent-of-code-f0638b6cef379829127dc352a227b3d43ba383cc.tar.bz2 advent-of-code-f0638b6cef379829127dc352a227b3d43ba383cc.zip | |
day 10, part 1, aoc 2022
| -rw-r--r-- | 2022/src/day10.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/2022/src/day10.cpp b/2022/src/day10.cpp new file mode 100644 index 0000000..76c953c --- /dev/null +++ b/2022/src/day10.cpp | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <string> | ||
| 4 | #include <vector> | ||
| 5 | using namespace std; | ||
| 6 | |||
| 7 | vector<int> | ||
| 8 | read_file(string_view filename) | ||
| 9 | { | ||
| 10 | fstream input{ filename }; | ||
| 11 | vector<int> cycles{}; | ||
| 12 | int reg_x = 1; | ||
| 13 | |||
| 14 | static const string add_instr = "addx "; | ||
| 15 | |||
| 16 | for ( string line; getline(input, line); ) { | ||
| 17 | cycles.emplace_back(reg_x); | ||
| 18 | |||
| 19 | if ( line.starts_with(add_instr) ) { | ||
| 20 | cycles.emplace_back(reg_x); | ||
| 21 | reg_x += stoi(line.substr(add_instr.size())); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | cycles.emplace_back(reg_x); | ||
| 25 | |||
| 26 | return cycles; | ||
| 27 | } | ||
| 28 | |||
| 29 | void | ||
| 30 | part1(const vector<int>& cycles) | ||
| 31 | { | ||
| 32 | int sum = 0; | ||
| 33 | for ( size_t cycle = 19; cycle < cycles.size(); cycle += 40 ) { | ||
| 34 | sum += static_cast<int>(cycle + 1) * cycles[cycle]; | ||
| 35 | } | ||
| 36 | cout << sum << endl; | ||
| 37 | } | ||
| 38 | |||
| 39 | int | ||
| 40 | main() | ||
| 41 | { | ||
| 42 | const auto cycles = read_file("data/day10.txt"); | ||
| 43 | part1(cycles); | ||
| 44 | } | ||
