-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday20.cpp
More file actions
298 lines (254 loc) · 8.59 KB
/
day20.cpp
File metadata and controls
298 lines (254 loc) · 8.59 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
#include "day20.h"
#include "helpers.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <fstream>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <queue>
#include <ranges>
#include <sstream>
#include <string>
#include <vector>
namespace day20
{
long long solvePart1(std::ifstream& file);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 20 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day20_example.txt" : "inputs/day20_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file) << '\n';
file.close();
// Kind of weird to have a part 2 that doesn't function for the examples....
if (!example)
{
file.open(fileName);
std::cout << "Part 2 answer: " << solvePart2(file) << '\n';
}
}
enum class ModuleType
{
broadcaster,
flipFlop,
conjunction,
other
};
struct Pulse
{
std::string source{};
std::string target{};
bool high{};
};
struct Module
{
std::string name{};
ModuleType type{};
std::vector<std::string> targets{};
bool flipFlopOn{};
std::unordered_map<std::string, bool> conjunctionSourcesHigh{};
Module() = default;
Module(std::string_view string)
{
std::stringstream ss{};
ss << string;
std::string section{};
ss >> section;
if (section[0] == '%')
{
type = ModuleType::flipFlop;
name = section.substr(1, section.size() - 1);
}
else if (section[0] == '&')
{
type = ModuleType::conjunction;
name = section.substr(1, section.size() - 1);
}
else if (section == "broadcaster")
{
type = ModuleType::broadcaster;
name = section;
}
else
{
type = ModuleType::other;
name = section;
}
ss >> section;
while(!ss.eof())
{
ss >> section;
if (section.ends_with(','))
{
targets.push_back(section.substr(0, section.size() - 1));
}
else
{
targets.push_back(section);
}
}
}
void initializeConjuntionSources(const std::vector<std::string>& sources)
{
for (const auto& source : sources)
{
conjunctionSourcesHigh.emplace(source, false);
}
}
std::vector<Pulse> processPulse(const Pulse& p)
{
std::vector<Pulse> results{};
if (type == ModuleType::broadcaster)
{
for (const std::string& target : targets)
{
results.push_back(Pulse{name, target, p.high});
}
}
else if (type == ModuleType::flipFlop)
{
if (!p.high)
{
flipFlopOn = !flipFlopOn;
for (const std::string& target : targets)
{
results.push_back(Pulse{ name, target, flipFlopOn });
}
}
}
else if (type == ModuleType::conjunction)
{
conjunctionSourcesHigh[p.source] = p.high;
const bool conjunctionOutputIsHigh = !std::ranges::all_of(conjunctionSourcesHigh, [](const std::pair<std::string, bool>& source) {return source.second; });
for (const std::string& target : targets)
{
results.push_back(Pulse{ name, target, conjunctionOutputIsHigh });
}
}
return results;
}
};
struct System
{
std::unordered_map<std::string, Module> moduleLookup{};
void parseInput(std::ifstream& file)
{
while(!file.eof())
{
std::string line;
std::getline(file, line);
Module m{ line };
moduleLookup.emplace(m.name, m);
}
// Set up the source memory for each conjunction module:
for (auto& module : moduleLookup | std::views::values)
{
if (module.type == ModuleType::conjunction)
{
std::vector<std::string> sources{};
for (const auto& otherModule : moduleLookup | std::views::values)
{
if (std::ranges::find(otherModule.targets, module.name) != otherModule.targets.end())
{
sources.push_back(otherModule.name);
}
}
module.initializeConjuntionSources(sources);
}
}
}
long long scoreButtonPresses(const int count)
{
long long totalHighPulses{};
long long totalLowPulses{};
for (int i =0; i<count;i++)
{
std::queue<Pulse> pulseQueue{};
pulseQueue.emplace("button", "broadcaster", false);
while(!pulseQueue.empty())
{
const Pulse& p{ pulseQueue.front() };
if (p.high)
{
totalHighPulses++;
}
else
{
totalLowPulses++;
}
auto newPulses{ moduleLookup[p.target].processPulse(p) };
pulseQueue.pop();
for (const auto& newPulse : newPulses)
{
pulseQueue.push(newPulse);
}
}
}
return totalHighPulses * totalLowPulses;
}
long long buttonPressesForRx()
{
const std::string sourceOfRxName = "df";
const auto& sourceOfRx{ moduleLookup[sourceOfRxName] };
std::unordered_map<std::string, long long> firstValidAtForSourcesOfDf{};
long long presses{};
bool stillLookingForCycles{ true };
while(stillLookingForCycles)
{
presses++;
std::queue<Pulse> pulseQueue{};
pulseQueue.emplace("button", "broadcaster", false);
while (!pulseQueue.empty())
{
const Pulse& p{ pulseQueue.front() };
if (p.target == "rx" && !p.high)
{
return presses;
}
for (const auto& conjunctionSourcesHigh : sourceOfRx.conjunctionSourcesHigh)
{
if (conjunctionSourcesHigh.second)
{
if (!firstValidAtForSourcesOfDf.contains(conjunctionSourcesHigh.first))
{
firstValidAtForSourcesOfDf[conjunctionSourcesHigh.first] = presses;
if (firstValidAtForSourcesOfDf.size() == 4)
{
stillLookingForCycles = false;
}
}
}
}
auto newPulses{ moduleLookup[p.target].processPulse(p) };
pulseQueue.pop();
for (const auto& newPulse : newPulses)
{
pulseQueue.push(newPulse);
}
}
}
// This works for puzzle input, but seems unlikely to work/hard to prove to work for any valid problem input?
long long lcm{ 1 };
for (const long long i : firstValidAtForSourcesOfDf | std::views::values)
{
lcm = std::lcm(lcm, i);
}
return lcm;
}
};
long long solvePart1(std::ifstream& file)
{
System system{};
system.parseInput(file);
return system.scoreButtonPresses(1000);
}
long long solvePart2(std::ifstream& file)
{
System system{};
system.parseInput(file);
return system.buttonPressesForRx();
}
}