diff options
Diffstat (limited to '2024')
| -rw-r--r-- | 2024/src/day24.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/2024/src/day24.cpp b/2024/src/day24.cpp new file mode 100644 index 0000000..bf439c7 --- /dev/null +++ b/2024/src/day24.cpp | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | vector<string> | ||
| 11 | split(string_view line, string_view delimiter) | ||
| 12 | { | ||
| 13 | size_t pos_start = 0; | ||
| 14 | size_t pos_end = 0; | ||
| 15 | |||
| 16 | vector<string> res; | ||
| 17 | |||
| 18 | while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) { | ||
| 19 | auto token = line.substr(pos_start, pos_end - pos_start); | ||
| 20 | pos_start = pos_end + delimiter.length(); | ||
| 21 | |||
| 22 | res.emplace_back(token); | ||
| 23 | } | ||
| 24 | |||
| 25 | res.emplace_back(line.substr(pos_start)); | ||
| 26 | return res; | ||
| 27 | } | ||
| 28 | |||
| 29 | tuple<map<string, unsigned long>, map<string, tuple<string, string, string>>> | ||
| 30 | read_file(string_view filename) | ||
| 31 | { | ||
| 32 | fstream input{ filename }; | ||
| 33 | const auto parts = split(string{ istreambuf_iterator<char>{ input }, {} }, "\n\n"); | ||
| 34 | |||
| 35 | map<string, unsigned long> inputs; | ||
| 36 | |||
| 37 | // register => { reg1 operator reg2 } | ||
| 38 | map<string, tuple<string, string, string>> deps; | ||
| 39 | |||
| 40 | stringstream spart1{ parts[0] }; | ||
| 41 | for ( string line; getline(spart1, line); ) { | ||
| 42 | const auto foo = split(line, ": "); | ||
| 43 | inputs[foo[0]] = stoul(foo[1]); | ||
| 44 | } | ||
| 45 | |||
| 46 | stringstream spart2{ parts[1] }; | ||
| 47 | for ( string line; getline(spart2, line); ) { | ||
| 48 | const auto foo = split(line, " "); | ||
| 49 | deps[foo[4]] = { foo[0], foo[1], foo[2] }; | ||
| 50 | } | ||
| 51 | |||
| 52 | return { inputs, deps }; | ||
| 53 | } | ||
| 54 | |||
| 55 | void | ||
| 56 | part1(const tuple<map<string, unsigned long>, map<string, tuple<string, string, string>>>& data) | ||
| 57 | { | ||
| 58 | auto inputs = get<0>(data); | ||
| 59 | const auto& deps = get<1>(data); | ||
| 60 | |||
| 61 | function<unsigned long(const string&)> eval = [&](const string& input) -> unsigned long { | ||
| 62 | if ( inputs.contains(input) ) { | ||
| 63 | return inputs.at(input); | ||
| 64 | } | ||
| 65 | |||
| 66 | const auto& [lhs, op, rhs] = deps.at(input); | ||
| 67 | |||
| 68 | unsigned long value = 0; | ||
| 69 | |||
| 70 | if ( op == "XOR" ) { | ||
| 71 | value = eval(lhs) ^ eval(rhs); | ||
| 72 | } | ||
| 73 | else if ( op == "AND" ) { | ||
| 74 | value = eval(lhs) & eval(rhs); | ||
| 75 | } | ||
| 76 | else if ( op == "OR" ) { | ||
| 77 | value = eval(lhs) | eval(rhs); | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | cerr << "Das darf nicht passieren! Input: " << input << endl; | ||
| 81 | } | ||
| 82 | |||
| 83 | inputs[input] = value; | ||
| 84 | |||
| 85 | return value; | ||
| 86 | }; | ||
| 87 | |||
| 88 | unsigned long value = 0; | ||
| 89 | for ( const auto& [first, second]: deps ) { | ||
| 90 | if ( first.starts_with("z") ) { | ||
| 91 | if ( eval(first) != 0 ) { | ||
| 92 | auto bit = stoul(first.substr(1)); | ||
| 93 | value |= 1UL << bit; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | cout << value << endl; | ||
| 98 | } | ||
| 99 | |||
| 100 | int | ||
| 101 | main() | ||
| 102 | { | ||
| 103 | const auto data = read_file("data/day24.txt"); | ||
| 104 | part1(data); | ||
| 105 | } | ||
