-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.cc
200 lines (167 loc) · 4.79 KB
/
7.cc
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <cassert>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
class Node {
public:
virtual uint16_t evaluate() const = 0;
virtual void reset() {}
};
class ConstantNode : public Node {
public:
ConstantNode(int value)
: m_value(value)
{
}
virtual uint16_t evaluate() const override { return m_value; }
private:
int m_value;
};
class VariableNode : public Node {
public:
VariableNode(std::string name, const std::map<std::string, Node*>& variables)
: m_name(name)
, m_variables(variables)
, m_cachedValue(0)
, m_hasCachedValue(false)
{
}
virtual uint16_t evaluate() const override {
if (m_hasCachedValue) {
return m_cachedValue;
}
m_hasCachedValue = true;
return m_cachedValue = m_variables.at(m_name)->evaluate();
}
virtual void reset() override {
m_hasCachedValue = false;
}
private:
std::string m_name;
const std::map<std::string, Node*>& m_variables;
mutable uint16_t m_cachedValue;
mutable bool m_hasCachedValue;
};
class NotNode : public Node {
public:
NotNode(Node& node)
: m_node(node)
{
}
virtual uint16_t evaluate() const override { return ~m_node.evaluate(); }
virtual void reset() override {
m_node.reset();
}
private:
Node& m_node;
};
class BinaryOperationNode : public Node {
public:
BinaryOperationNode(Node& node1, Node& node2)
: m_node1(node1)
, m_node2(node2)
{
}
virtual int apply(int, int) const = 0;
virtual uint16_t evaluate() const override { return apply(m_node1.evaluate(), m_node2.evaluate()); }
virtual void reset() override {
m_node1.reset();
m_node2.reset();
}
private:
Node& m_node1;
Node& m_node2;
};
class AndNode : public BinaryOperationNode {
public:
using BinaryOperationNode::BinaryOperationNode;
virtual int apply(int a, int b) const override { return a & b; }
};
class OrNode : public BinaryOperationNode {
public:
using BinaryOperationNode::BinaryOperationNode;
virtual int apply(int a, int b) const override { return a | b; }
};
class LeftShiftNode : public BinaryOperationNode {
public:
using BinaryOperationNode::BinaryOperationNode;
virtual int apply(int a, int b) const override { return a << b; }
};
class RightShiftNode : public BinaryOperationNode {
public:
using BinaryOperationNode::BinaryOperationNode;
virtual int apply(int a, int b) const override { return a >> b; }
};
bool isIntegral(const std::string& string) {
for (char c : string) {
if (!isdigit(c)) {
return false;
}
}
return true;
}
Node* parseExpression(const std::string& expression, const std::map<std::string, Node*>& variables) {
if (isIntegral(expression)) {
return new ConstantNode(stoi(expression));
}
return new VariableNode(expression, variables);
}
Node* parseBinaryOperation(Node& left, std::vector<std::string>::iterator& tokens, const std::map<std::string, Node*>& variables) {
std::string binaryOperator = *tokens;
++tokens;
auto* right = parseExpression(*tokens, variables);
if (binaryOperator == "AND") {
return new AndNode(left, *right);
} else if (binaryOperator == "OR") {
return new OrNode(left, *right);
} else if (binaryOperator == "LSHIFT") {
return new LeftShiftNode(left, *right);
} else if (binaryOperator == "RSHIFT") {
return new RightShiftNode(left, *right);
}
assert(false);
return nullptr;
}
void assignVariable(std::map<std::string, Node*>& variables, std::vector<std::string>::iterator tokens) {
Node* node;
if (*tokens == "NOT") {
++tokens;
auto* operand = parseExpression(*tokens, variables);
++tokens;
node = new NotNode(*operand);
} else {
auto* left = parseExpression(*tokens, variables);
++tokens;
if (*tokens == "->") {
node = left;
} else {
node = parseBinaryOperation(*left, tokens, variables);
++tokens;
}
}
assert(*tokens == "->");
++tokens;
variables[*tokens] = node;
}
int main() {
std::string line;
std::map<std::string, Node*> variables;
while (std::getline(std::cin, line)) {
std::stringstream stream(line);
std::vector<std::string> tokens;
std::string word;
while (std::getline(stream, word, ' ')) {
tokens.push_back(word);
}
assignVariable(variables, tokens.begin());
}
int a = variables["a"]->evaluate();
std::cout << a << std::endl;
for (auto pair : variables) {
pair.second->reset();
}
variables["b"] = new ConstantNode(a);
std::cout << variables["a"]->evaluate() << std::endl;
return 0;
}