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
98
99
100
101
102
103
104
105
|
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<string>
split(string_view line, string_view delimiter)
{
size_t pos_start = 0;
size_t pos_end = 0;
vector<string> res;
while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) {
auto token = line.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delimiter.length();
res.emplace_back(token);
}
res.emplace_back(line.substr(pos_start));
return res;
}
tuple<map<string, unsigned long>, map<string, tuple<string, string, string>>>
read_file(string_view filename)
{
fstream input{ filename };
const auto parts = split(string{ istreambuf_iterator<char>{ input }, {} }, "\n\n");
map<string, unsigned long> inputs;
// register => { reg1 operator reg2 }
map<string, tuple<string, string, string>> deps;
stringstream spart1{ parts[0] };
for ( string line; getline(spart1, line); ) {
const auto foo = split(line, ": ");
inputs[foo[0]] = stoul(foo[1]);
}
stringstream spart2{ parts[1] };
for ( string line; getline(spart2, line); ) {
const auto foo = split(line, " ");
deps[foo[4]] = { foo[0], foo[1], foo[2] };
}
return { inputs, deps };
}
void
part1(const tuple<map<string, unsigned long>, map<string, tuple<string, string, string>>>& data)
{
auto inputs = get<0>(data);
const auto& deps = get<1>(data);
function<unsigned long(const string&)> eval = [&](const string& input) -> unsigned long {
if ( inputs.contains(input) ) {
return inputs.at(input);
}
const auto& [lhs, op, rhs] = deps.at(input);
unsigned long value = 0;
if ( op == "XOR" ) {
value = eval(lhs) ^ eval(rhs);
}
else if ( op == "AND" ) {
value = eval(lhs) & eval(rhs);
}
else if ( op == "OR" ) {
value = eval(lhs) | eval(rhs);
}
else {
cerr << "Das darf nicht passieren! Input: " << input << endl;
}
inputs[input] = value;
return value;
};
unsigned long value = 0;
for ( const auto& [first, second]: deps ) {
if ( first.starts_with("z") ) {
if ( eval(first) != 0 ) {
auto bit = stoul(first.substr(1));
value |= 1UL << bit;
}
}
}
cout << value << endl;
}
int
main()
{
const auto data = read_file("data/day24.txt");
part1(data);
}
|