From 756f22d58bb198b8f34589c112e1003614ccdcd6 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 2 Nov 2025 21:41:58 +0100 Subject: aoc 2017, days 1-20 --- 2017/src/day08.cpp | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 2017/src/day08.cpp (limited to '2017/src/day08.cpp') 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 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +namespace { + +vector +split(const string& line, char sep = ' ') +{ + stringstream strm{ line }; + vector parts; + + for ( string part; getline(strm, part, sep); ) { + parts.emplace_back(part); + } + + return parts; +} + +vector> +read_file(const filesystem::path& filename) +{ + ifstream file{ filename }; + vector> data; + + for ( string line; getline(file, line); ) { + auto parts = split(line); + data.emplace_back(parts); + } + + return data; +} + +void +compute(const vector>& data, map& memory, int& max_value) +{ + auto match = [](int lhs, string_view rel, int rhs) { + if ( rel == "==" ) { + return lhs == rhs; + } + else if ( rel == "!=" ) { + return lhs != rhs; + } + else if ( rel == "<" ) { + return lhs < rhs; + } + else if ( rel == "<=" ) { + return lhs <= rhs; + } + else if ( rel == ">" ) { + return lhs > rhs; + } + else if ( rel == ">=" ) { + return lhs >= rhs; + } + + throw runtime_error("unknown relation operator: "s + string(rel)); + }; + + auto execute = [&](const string& reg, string_view opcode, int value) { + if ( opcode == "inc" ) { + memory[reg] += value; + } + else if ( opcode == "dec" ) { + memory[reg] -= value; + } + else { + throw runtime_error("unknown opcode: "s + string(opcode)); + } + }; + + for ( const auto& instr: data ) { + const auto& reg = instr.at(0); + const auto& opcode = instr.at(1); + const auto value = stoi(instr.at(2)); + const auto lhs = memory[instr.at(4)]; + const auto& rel = instr.at(5); + const auto rhs = stoi(instr.at(6)); + + if ( match(lhs, rel, rhs) ) { + execute(reg, opcode, value); + max_value = max(max_value, memory[reg]); + } + } +} + +void +solve(const vector>& data) +{ + map memory; + auto max_value = numeric_limits::min(); + + compute(data, memory, max_value); + + auto itr = ranges::max_element(memory, [](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; }); + cout << "Part1: " << itr->second << '\n'; + cout << "Part2: " << max_value << '\n'; +} + +} // namespace + +int +main() +{ + auto data = read_file("data/day08.txt"); + try { + solve(data); + } + catch ( exception& e ) { + cerr << "Fehler: " << e.what() << '\n'; + } +} -- cgit v1.3