-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20-Pulse-Propagation.cpp
More file actions
369 lines (283 loc) · 11.1 KB
/
20-Pulse-Propagation.cpp
File metadata and controls
369 lines (283 loc) · 11.1 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (C) 2024 Joe Baker (JoeBlakeB)
// Advent of Code 2023 - Day 20: Pulse Propagation
// Usage:
// scripts/cppRun.sh 2023/20-Pulse-Propagation.cpp < 2023/inputs/20.txt
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct PulseQueueItem {
short sender;
short receiver;
bool highPulse;
};
typedef queue<PulseQueueItem> PulseQueue;
class Module {
protected:
short id;
vector<short> inputModules, outputModules;
// Sends pulse to all output modules
inline void output(bool highPulse, PulseQueue &pulseQueue) {
for (size_t i = 0; i < outputModules.size(); i++) {
pulseQueue.push({this->id, outputModules[i], highPulse});
}
}
public:
Module(short id, vector<short> outputModules) : id(id), inputModules(), outputModules(outputModules) {}
virtual ~Module() {}
// Processes a pulse and saves any resulting output pulses to the pulse queue
virtual void pulse(bool highPulse, short sender, PulseQueue& pulseQueue) {
(void)highPulse; (void)sender; (void)pulseQueue;
}
// Gets the current state of the module, if any
virtual char state() { return 0; }
// Resets the modules state, if any
virtual void reset() {}
// Add an input module to this module
virtual void addInput(short newInput) { inputModules.push_back(newInput); }
// Get the list of input module IDs
virtual vector<short> getInputList() { return inputModules; }
};
class FlipFlop : public Module {
private:
bool power;
public:
FlipFlop(short id, vector<short> outputModules) : Module(id, outputModules), power(false) { }
// Flips between sending high and low pulses, only when low are received
void pulse(bool highPulse, short, PulseQueue& pulseQueue) override {
if (!highPulse) {
power = !power;
output(power, pulseQueue);
}
}
// The current power state
char state() override { return power; }
// Resets the power to off
void reset() override { power = false; }
};
class Conjunction : public Module {
private:
vector<pair<short, bool>> inputModules;
bool receivedHigh;
public:
Conjunction(short id, vector<short> outputModules) : Module(id, outputModules), inputModules(), receivedHigh(false) {}
// Remembers the pulses from its input modules, if all are high,
// it will send a low to its recepients, otherwise it will send high
void pulse(bool inputHighPulse, short sender, PulseQueue& pulseQueue) override {
bool outputHighPulse = false;
for (size_t i = 0; i < inputModules.size() && (sender || !outputHighPulse); i++) {
if (sender && inputModules[i].first == sender) {
inputModules[i].second = inputHighPulse;
sender = 0;
}
if (!inputModules[i].second) {
outputHighPulse = true;
receivedHigh = true;
}
}
output(outputHighPulse, pulseQueue);
}
// The last pulses for its input modules, the last bit for if a high pulse has been received yet
char state() override {
int value = 0;
for (size_t i = 0; i < inputModules.size(); i++) {
value = (value + inputModules[i].second) << 1;
}
return value + receivedHigh;
}
// Resets all input modues previous state to low
void reset() override {
receivedHigh = false;
for (size_t i = 0; i < inputModules.size(); i++) {
inputModules[i].second = false;
}
}
// Add a module to its list with its previous pulse being set to low
void addInput(short newInput) override { inputModules.push_back({newInput, false}); }
// Get the list of input module IDs
vector<short> getInputList() override {
vector<short> inputList;
for (size_t i = 0; i < inputModules.size(); i++) {
inputList.push_back(inputModules[i].first);
}
return inputList;
}
};
class Broadcaster : public Module {
public:
Broadcaster(short id, vector<short> outputModules) : Module(id, outputModules) {}
// Sends its pulse to all connected modules
void pulse(bool highPulse, short, PulseQueue& pulseQueue) override {
output(highPulse, pulseQueue);
}
// Broadcasters do not have a state, return 0
char state() override { return 0; }
};
class Output : public Module {
private:
char lastPulse;
public:
Output(short id, vector<short> outputModules) : Module(id, outputModules), lastPulse(0) { }
// Saves the last pulse, low being 1, high being 2
void pulse(bool inputHighPulse, short, PulseQueue&) override {
lastPulse = inputHighPulse + 1;
}
// The current power state
char state() override { return lastPulse; }
// Resets the power to off
void reset() override { lastPulse = 0; }
};
constexpr short getID(string moduleName) {
return moduleName[0] + (moduleName.size() >= 2 ? (moduleName[1] << 8) : 0);
}
class Network {
private:
map<short, Module*> modules;
public:
Network(istream &inputstream) {
string line;
vector<pair<short, vector<short>>> modulesAndOutputsTemp;
// Create the modules
while (getline(inputstream, line) && line != "") {
vector<short> outputModules;
size_t i = 0;
// Save the first and second chars in a short for efficiency
short thisModuleID = line[1];
if (line[2] != ' ') {
thisModuleID += line[2] << 8;
}
if (line[0] == 'b') {
thisModuleID = 0;
}
if (modules.find(thisModuleID) != modules.end()) {
exit(1);
}
// Go to the start of the output list
do { i++; } while (line[i] != '>' || line[i+1] != ' '); i+= 2;
// Get the output modules for this module
while (i < line.size()) {
short outputModule = line[i++];
if (i < line.size() && line[i] != ',') {
outputModule += line[i++] << 8;
}
while (i < line.size()) {
if (line[i++] == ' ') {
break;
}
}
outputModules.push_back(outputModule);
}
modulesAndOutputsTemp.push_back({thisModuleID, outputModules});
Module* thisModule = nullptr;
if (line[0] == 'b') {
thisModule = new Broadcaster(thisModuleID, outputModules);
} else if (line[0] == '%') {
thisModule = new FlipFlop(thisModuleID, outputModules);
} else if (line[0] == '&') {
thisModule = new Conjunction(thisModuleID, outputModules);
} else {
cout << "Error: Invalid line, unknown module type: " << line[0] << endl;
exit(1);
}
modules.insert({thisModuleID, thisModule});
}
// Go through the modules and output lists again to add the inputs
for (auto it = modulesAndOutputsTemp.begin(); it != modulesAndOutputsTemp.end(); it++) {
const short& inputModule = it->first;
const vector<short>& moduleList = it->second;
for (size_t i = 0; i < moduleList.size(); i++) {
// Module does not exist, create dummy input only module (output, tx)
if (!exists(moduleList[i])) {
modules.insert({moduleList[i], new Output(moduleList[i], {})});
}
modules[moduleList[i]]->addInput(inputModule);
}
}
}
~Network() {
for (auto it = modules.begin(); it != modules.end(); it++) {
delete it->second;
}
}
Network(const Network& other) = delete;
Network(Network&& other) = delete;
Network& operator=(const Network& other) = delete;
Network& operator=(Network&& other) = delete;
// Press the button once and process its following pulses, returns a pair of the count of low and high pulses
pair<long long int, long long int> pressButton() {
PulseQueue pulseQueue({{0, 0, false}});
int pulseCounts[2] = {0, 0};
while (!pulseQueue.empty()) {
PulseQueueItem nextPulse = pulseQueue.front();
pulseQueue.pop();
// cout << nextPulse.sender << " > " << nextPulse.highPulse << " > " << nextPulse.receiver << endl;
pulseCounts[nextPulse.highPulse]++;
modules[nextPulse.receiver]->pulse(nextPulse.highPulse, nextPulse.sender, pulseQueue);
}
return {pulseCounts[0], pulseCounts[1]};
}
// Press the button a certain amount of times
pair<long long int, long long int> pressButton(int count) {
pair<long long int, long long int> pulseCounts = {0, 0};
for (int i = 0; i < count; i++) {
auto nextCounts = pressButton();
pulseCounts.first += nextCounts.first;
pulseCounts.second += nextCounts.second;
}
return pulseCounts;
}
// Reset all modules in the network
void reset() {
for (auto it = modules.begin(); it != modules.end(); it++) {
it->second->reset();
}
}
char state(short moduleID) {
return modules[moduleID]->state();
}
bool exists(short moduleID) {
return modules.count(moduleID);
}
// Get the closest junction (module with more than one input) to the output (rx)
vector<short> getInputsForFirstJunction(short currentModuleID) {
int inputCount;
vector<short> inputsList;
do {
inputsList = modules[currentModuleID]->getInputList();
currentModuleID = inputsList.front();
inputCount = inputsList.size();
} while (inputCount == 1);
return inputsList;
}
};
int main() {
Network network(cin);
auto pulseCounts = network.pressButton(1000);
cout << "Product of push types after 1000 pushes: " << pulseCounts.first * pulseCounts.second << endl;
constexpr short outputID = getID("rx");
if (network.exists(outputID)) {
network.reset();
// Get the list of inputs to the final conjunction
vector<short> finalConjunctionInputs = network.getInputsForFirstJunction(outputID);
long long int product = 1;
int presses = 0;
// Get the product of the amount of times it takes for each of the inputs
// to the final conjunction to receive their first low pulse
while (!finalConjunctionInputs.empty()) {
network.pressButton();
presses++;
for (size_t i = 0; i < finalConjunctionInputs.size(); i++) {
if (network.state(finalConjunctionInputs[i]) % 2) {
product *= presses;
finalConjunctionInputs.erase(finalConjunctionInputs.begin() + i);
}
}
}
cout << "Presses required for a low pulse to the \"rx\" module: " << product << endl;
} else {
cout << "Network does not contain the \"rx\" module" << endl;
}
return 0;
}