From 7af390a45aee5133ae83114983f4d461d76a837b Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 19 Jan 2025 22:48:45 +0100 Subject: aoc 2016, day 10 --- 2016/src/day10.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 2016/src/day10.cpp (limited to '2016/src') diff --git a/2016/src/day10.cpp b/2016/src/day10.cpp new file mode 100644 index 0000000..7c1f4f9 --- /dev/null +++ b/2016/src/day10.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +using Sink = tuple; + +vector +split(const string& line, char sep) +{ + vector parts; + stringstream input{ line }; + + for ( string part; getline(input, part, sep); ) { + parts.emplace_back(part); + } + + return parts; +} + +map>> +read_file(string_view filename) +{ + fstream input{ filename }; + + map>> bots; + + for ( string line; getline(input, line); ) { + const auto parts = split(line, ' '); + + if ( parts.at(0) == "bot" ) { + const Sink low_sink = { parts.at(5), stoi(parts.at(6)) }; + const Sink hi_sink = { parts.at(10), stoi(parts.at(11)) }; + + auto& bot = bots[stoi(parts.at(1))]; + + get<0>(bot) = low_sink; + get<1>(bot) = hi_sink; + } + else if ( parts.at(0) == "value" ) { + auto& bot = bots[stoi(parts.at(5))]; + + get<2>(bot).push_back(stoi(parts.at(1))); + } + } + + return bots; +} + +void +solve(map>> bots) +{ + map outputs; + + const auto store = [&](const Sink& sink, int value) { + if ( get<0>(sink) == "bot" ) { + auto& to_bot = bots[get<1>(sink)]; + get<2>(to_bot).push_back(value); + } + else { + outputs[get<1>(sink)] = value; + } + }; + + while ( true ) { + auto it = ranges::find_if(bots, [](auto& bot) { + const auto& [id, values] = bot; + return (get<2>(values).size() == 2); + }); + if ( it == bots.end() ) { + break; + } + + auto& [id, values] = *it; + auto& [low_sink, hi_sink, chips] = values; + + ranges::sort(chips); + store(low_sink, chips.at(0)); + store(hi_sink, chips.at(1)); + + // part1 + if ( chips.at(0) == 17 && chips.at(1) == 61 ) { + cout << id << endl; + } + + chips.clear(); + } + + // part2 + cout << outputs[0] * outputs[1] * outputs[2] << endl; +} + +int +main() +{ + auto bots = read_file("data/day10.txt"); + solve(bots); +} -- cgit v1.3