diff options
Diffstat (limited to '2015/src/day07.cpp')
| -rw-r--r-- | 2015/src/day07.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/2015/src/day07.cpp b/2015/src/day07.cpp new file mode 100644 index 0000000..f24d5af --- /dev/null +++ b/2015/src/day07.cpp | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | #include <cinttypes> | ||
| 2 | #include <cstdio> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <map> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | auto | ||
| 12 | read_file(string_view filename) | ||
| 13 | { | ||
| 14 | fstream input{ filename }; | ||
| 15 | map<string, string> deps; | ||
| 16 | |||
| 17 | for ( string line; getline(input, line); ) { | ||
| 18 | const auto pos = line.find(" -> "); | ||
| 19 | if ( pos == string::npos ) { | ||
| 20 | continue; | ||
| 21 | } | ||
| 22 | |||
| 23 | deps[line.substr(pos + 4)] = line.substr(0, pos); | ||
| 24 | } | ||
| 25 | |||
| 26 | return deps; | ||
| 27 | } | ||
| 28 | |||
| 29 | // NOLINTBEGIN | ||
| 30 | uint16_t | ||
| 31 | solve(map<string, string>& deps, const string& dep) | ||
| 32 | { | ||
| 33 | map<string, uint16_t> cache; | ||
| 34 | |||
| 35 | function<uint16_t(const string&)> solve = [&](const string& dep) -> uint16_t { | ||
| 36 | if (cache.contains(dep)) { | ||
| 37 | return cache[dep]; | ||
| 38 | } | ||
| 39 | |||
| 40 | uint16_t value = 0; | ||
| 41 | if (sscanf(dep.c_str(), "%" SCNu16, &value) == 1) { | ||
| 42 | return cache[dep] = value; | ||
| 43 | } | ||
| 44 | |||
| 45 | const auto& line = deps[dep]; | ||
| 46 | |||
| 47 | static const size_t MAX = 11; | ||
| 48 | char lhs[MAX], rhs[MAX]; | ||
| 49 | |||
| 50 | if ( sscanf(line.c_str(), "%10s AND %10s", lhs, rhs) == 2 ) { | ||
| 51 | value = solve(lhs) & solve(rhs); | ||
| 52 | } | ||
| 53 | else if ( sscanf(line.c_str(), "%10s OR %10s", lhs, rhs) == 2 ) { | ||
| 54 | value = solve(lhs) | solve(rhs); | ||
| 55 | } | ||
| 56 | else if ( sscanf(line.c_str(), "%10s LSHIFT %10s", lhs, rhs) == 2 ) { | ||
| 57 | value = static_cast<uint16_t>(solve(lhs) << solve(rhs)); | ||
| 58 | } | ||
| 59 | else if ( sscanf(line.c_str(), "%10s RSHIFT %10s", lhs, rhs) == 2 ) { | ||
| 60 | value = static_cast<uint16_t>(solve(lhs) >> solve(rhs)); | ||
| 61 | } | ||
| 62 | else if ( sscanf(line.c_str(), "NOT %10s", lhs) == 1 ) { | ||
| 63 | value = ~solve(lhs); | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | value = solve(line); | ||
| 67 | } | ||
| 68 | |||
| 69 | return cache[dep] = value; | ||
| 70 | }; | ||
| 71 | |||
| 72 | return solve(dep); | ||
| 73 | } | ||
| 74 | // NOLINTEND | ||
| 75 | |||
| 76 | auto | ||
| 77 | part1(map<string, string>& deps) | ||
| 78 | { | ||
| 79 | auto result = solve(deps, "a"); | ||
| 80 | cout << result << endl; | ||
| 81 | return result; | ||
| 82 | } | ||
| 83 | |||
| 84 | void | ||
| 85 | part2(map<string, string>& deps, uint16_t result) | ||
| 86 | { | ||
| 87 | deps["b"] = to_string(result); | ||
| 88 | cout << solve(deps, "a") << endl; | ||
| 89 | } | ||
| 90 | |||
| 91 | int | ||
| 92 | main() | ||
| 93 | { | ||
| 94 | auto deps = read_file("data/day07.txt"); | ||
| 95 | auto result = part1(deps); | ||
| 96 | part2(deps, result); | ||
| 97 | } | ||
