diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
| commit | 756f22d58bb198b8f34589c112e1003614ccdcd6 (patch) | |
| tree | 4cdeba335f1ea67f7ead9e9f763ba1c3c206fc4d /2017/src/day08.cpp | |
| parent | fcbb722bd4c5ca2bd34a7cf9c0ba48f2f955da13 (diff) | |
| download | advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.gz advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.bz2 advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.zip | |
aoc 2017, days 1-20
Diffstat (limited to '2017/src/day08.cpp')
| -rw-r--r-- | 2017/src/day08.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/2017/src/day08.cpp b/2017/src/day08.cpp new file mode 100644 index 0000000..64c1ea0 --- /dev/null +++ b/2017/src/day08.cpp | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | #include <filesystem> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | vector<string> | ||
| 14 | split(const string& line, char sep = ' ') | ||
| 15 | { | ||
| 16 | stringstream strm{ line }; | ||
| 17 | vector<string> parts; | ||
| 18 | |||
| 19 | for ( string part; getline(strm, part, sep); ) { | ||
| 20 | parts.emplace_back(part); | ||
| 21 | } | ||
| 22 | |||
| 23 | return parts; | ||
| 24 | } | ||
| 25 | |||
| 26 | vector<vector<string>> | ||
| 27 | read_file(const filesystem::path& filename) | ||
| 28 | { | ||
| 29 | ifstream file{ filename }; | ||
| 30 | vector<vector<string>> data; | ||
| 31 | |||
| 32 | for ( string line; getline(file, line); ) { | ||
| 33 | auto parts = split(line); | ||
| 34 | data.emplace_back(parts); | ||
| 35 | } | ||
| 36 | |||
| 37 | return data; | ||
| 38 | } | ||
| 39 | |||
| 40 | void | ||
| 41 | compute(const vector<vector<string>>& data, map<string, int>& memory, int& max_value) | ||
| 42 | { | ||
| 43 | auto match = [](int lhs, string_view rel, int rhs) { | ||
| 44 | if ( rel == "==" ) { | ||
| 45 | return lhs == rhs; | ||
| 46 | } | ||
| 47 | else if ( rel == "!=" ) { | ||
| 48 | return lhs != rhs; | ||
| 49 | } | ||
| 50 | else if ( rel == "<" ) { | ||
| 51 | return lhs < rhs; | ||
| 52 | } | ||
| 53 | else if ( rel == "<=" ) { | ||
| 54 | return lhs <= rhs; | ||
| 55 | } | ||
| 56 | else if ( rel == ">" ) { | ||
| 57 | return lhs > rhs; | ||
| 58 | } | ||
| 59 | else if ( rel == ">=" ) { | ||
| 60 | return lhs >= rhs; | ||
| 61 | } | ||
| 62 | |||
| 63 | throw runtime_error("unknown relation operator: "s + string(rel)); | ||
| 64 | }; | ||
| 65 | |||
| 66 | auto execute = [&](const string& reg, string_view opcode, int value) { | ||
| 67 | if ( opcode == "inc" ) { | ||
| 68 | memory[reg] += value; | ||
| 69 | } | ||
| 70 | else if ( opcode == "dec" ) { | ||
| 71 | memory[reg] -= value; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | throw runtime_error("unknown opcode: "s + string(opcode)); | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | for ( const auto& instr: data ) { | ||
| 79 | const auto& reg = instr.at(0); | ||
| 80 | const auto& opcode = instr.at(1); | ||
| 81 | const auto value = stoi(instr.at(2)); | ||
| 82 | const auto lhs = memory[instr.at(4)]; | ||
| 83 | const auto& rel = instr.at(5); | ||
| 84 | const auto rhs = stoi(instr.at(6)); | ||
| 85 | |||
| 86 | if ( match(lhs, rel, rhs) ) { | ||
| 87 | execute(reg, opcode, value); | ||
| 88 | max_value = max(max_value, memory[reg]); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | void | ||
| 94 | solve(const vector<vector<string>>& data) | ||
| 95 | { | ||
| 96 | map<string, int> memory; | ||
| 97 | auto max_value = numeric_limits<int>::min(); | ||
| 98 | |||
| 99 | compute(data, memory, max_value); | ||
| 100 | |||
| 101 | auto itr = ranges::max_element(memory, [](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; }); | ||
| 102 | cout << "Part1: " << itr->second << '\n'; | ||
| 103 | cout << "Part2: " << max_value << '\n'; | ||
| 104 | } | ||
| 105 | |||
| 106 | } // namespace | ||
| 107 | |||
| 108 | int | ||
| 109 | main() | ||
| 110 | { | ||
| 111 | auto data = read_file("data/day08.txt"); | ||
| 112 | try { | ||
| 113 | solve(data); | ||
| 114 | } | ||
| 115 | catch ( exception& e ) { | ||
| 116 | cerr << "Fehler: " << e.what() << '\n'; | ||
| 117 | } | ||
| 118 | } | ||
