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