-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge20.cpp
249 lines (207 loc) · 9.61 KB
/
challenge20.cpp
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
#include "challenge20.hpp"
#include "helper.hpp"
#include "print.hpp"
#include <algorithm>
#include <iterator>
#include <numeric>
#include <ranges>
#include <unordered_map>
#include <variant>
using namespace std::string_view_literals;
namespace {
using ModuleName = std::string_view;
using Connections = std::vector<ModuleName>;
enum class Pulse : bool { Low, High };
struct FlipFlop {
std::string_view Name;
bool IsOn = false;
};
struct Conjunction {
std::string_view Name;
std::unordered_map<ModuleName, Pulse> PulseCache = {};
std::unordered_map<ModuleName, std::int64_t> PulseLoop = {};
int HighPulses = 0;
int HighPulsesNeeded = 0;
};
using Module = std::variant<FlipFlop, Conjunction>;
struct ModuleAndConnection {
::Module Module;
::Connections Connections;
};
struct ModuleConfiguration {
Connections Broadcaster;
std::unordered_map<ModuleName, ModuleAndConnection> Modules;
ModuleName RxPredecessor;
};
Connections parseConnections(std::string_view line) {
constexpr auto prefix = "->"sv;
throwIfInvalid(line.starts_with(prefix));
line.remove_prefix(prefix.size());
Connections ret;
std::ranges::copy(splitString(line, ',') | std::views::transform([](std::string_view name) {
throwIfInvalid(name.starts_with(' '));
name.remove_prefix(1);
return name;
}),
std::back_inserter(ret));
return ret;
}
constexpr auto BroadCasterString = "broadcaster"sv;
constexpr auto RxString = "rx"sv;
auto parse(const std::vector<std::string_view>& input) {
ModuleConfiguration ret;
for ( auto line : input ) {
throwIfInvalid(!line.empty());
const auto nameEnd = line.find(' ');
throwIfInvalid(nameEnd != std::string_view::npos);
auto name = line.substr(0, nameEnd);
auto connectionsString = line.substr(nameEnd + 1);
switch ( line.front() ) {
case '%' : {
name.remove_prefix(1);
ret.Modules.emplace(name, ModuleAndConnection{FlipFlop{name}, parseConnections(connectionsString)});
break;
} //case '%'
case '&' : {
name.remove_prefix(1);
ret.Modules.emplace(name,
ModuleAndConnection{Conjunction{name}, parseConnections(connectionsString)});
break;
} //case '&'
default : {
throwIfInvalid(ret.Broadcaster.empty());
throwIfInvalid(name == BroadCasterString);
ret.Broadcaster = parseConnections(line.substr(nameEnd + 1));
break;
} //default
} //switch ( line.front() )
if ( connectionsString.contains(RxString) ) {
throwIfInvalid(ret.RxPredecessor.empty());
ret.RxPredecessor = name;
} //if ( connectionsString.contains(RxString) )
} //for ( auto line : input )
auto addConjunctionPredeccesor = [&ret](ModuleName from, const Connections& connections) noexcept {
for ( auto to : connections ) {
auto iter = ret.Modules.find(to);
if ( iter == ret.Modules.end() ) {
continue;
} //if ( iter == ret.Modules.end() )
if ( std::holds_alternative<Conjunction>(iter->second.Module) ) {
auto& conjunction = std::get<Conjunction>(iter->second.Module);
conjunction.PulseCache.emplace(from, Pulse::Low);
conjunction.PulseLoop.emplace(from, 0);
++conjunction.HighPulsesNeeded;
} //if ( std::holds_alternative<Conjunction>(iter->second.Module) )
} //for ( auto to : connections )
return;
};
addConjunctionPredeccesor(BroadCasterString, ret.Broadcaster);
for ( const auto& [moduleName, moduleAndConnection] : ret.Modules ) {
addConjunctionPredeccesor(moduleName, moduleAndConnection.Connections);
} //for ( const auto& [moduleName, moduleAndConnection] : ret.Modules )
return ret;
}
struct ActivePulse {
ModuleName Sender;
ModuleName Receiver;
::Pulse Pulse;
ActivePulse(const std::tuple<const ModuleName&, const ModuleName&, const ::Pulse&>& stuff) noexcept :
Sender{std::get<0>(stuff)}, Receiver{std::get<1>(stuff)}, Pulse{std::get<2>(stuff)} {
return;
}
};
struct Run {
ModuleConfiguration Configuration;
std::vector<ActivePulse> CurrentPulses = {};
std::vector<ActivePulse> NextPulses = {};
std::int64_t LowPulses = 0;
std::int64_t HighPulses = 0;
std::int64_t ButtonPresses = 1;
Conjunction* RxPredeccesor = nullptr;
Run(ModuleConfiguration configuration) noexcept : Configuration{std::move(configuration)} {
if ( !Configuration.RxPredecessor.empty() ) {
auto iter = Configuration.Modules.find(Configuration.RxPredecessor);
if ( iter != Configuration.Modules.end() && std::holds_alternative<Conjunction>(iter->second.Module) ) {
RxPredeccesor = std::addressof(std::get<Conjunction>(iter->second.Module));
} //if ( iter != Configuration.Modules.end() )
} //if ( !Configuration.RxPredecessor.empty() )
return;
}
void applyPulse(FlipFlop& flip, ModuleName /*from*/, const Connections& receivers, Pulse pulse) noexcept {
if ( pulse == Pulse::High ) {
return;
} //if ( pulse == Pulse::High )
addPulses(flip.Name, receivers, flip.IsOn ? Pulse::Low : Pulse::High);
flip.IsOn = !flip.IsOn;
return;
}
void applyPulse(Conjunction& conjunction, ModuleName from, const Connections& receivers, Pulse pulse) noexcept {
auto& cache = conjunction.PulseCache.find(from)->second;
if ( cache != pulse ) {
if ( pulse == Pulse::High ) {
++conjunction.HighPulses;
auto& loopCache = conjunction.PulseLoop.find(from)->second;
if ( loopCache == 0 ) {
loopCache = ButtonPresses;
} //if ( loopCache == 0 )
} //if ( pulse == Pulse::High )
else {
--conjunction.HighPulses;
} //else -> if ( pulse == Pulse::High )
cache = pulse;
} //if ( cache != pulse )
addPulses(conjunction.Name, receivers,
conjunction.HighPulses == conjunction.HighPulsesNeeded ? Pulse::Low : Pulse::High);
return;
}
void addPulses(ModuleName sender, const Connections& receivers, Pulse pulse) noexcept {
std::ranges::copy(std::views::zip(std::views::repeat(sender), receivers, std::views::repeat(pulse)),
std::back_inserter(NextPulses));
return;
}
void press(int presses) noexcept {
for ( ; presses > 0; --presses, ++ButtonPresses ) {
//The Button:
++LowPulses;
addPulses(BroadCasterString, Configuration.Broadcaster, Pulse::Low);
do { //while ( !nextPulses.empty() )
std::swap(CurrentPulses, NextPulses);
for ( auto [sender, receiver, pulse] : CurrentPulses ) {
++(pulse == Pulse::Low ? LowPulses : HighPulses);
const auto moduleIter = Configuration.Modules.find(receiver);
if ( moduleIter == Configuration.Modules.end() ) {
//Senke.
continue;
} //if ( moduleIter == Configuration.Modules.end() )
auto& [module, connections] = moduleIter->second;
std::visit(
[this, &connections, pulse, sender](auto& realModule) noexcept {
applyPulse(realModule, sender, connections, pulse);
return;
},
module);
} //for ( auto [sender, receiver, pulse] : CurrentPulses )
CurrentPulses.clear();
} while ( !NextPulses.empty() );
} //for ( ; presses > 0; --presses, ++ButtonPresses )
return;
}
};
} //namespace
bool challenge20(const std::vector<std::string_view>& input) {
const auto moduleConfiguration = parse(input);
Run run{moduleConfiguration};
run.press(1000);
auto result1 = run.LowPulses * run.HighPulses;
myPrint(" == Result of Part 1: {:d} ==\n", result1);
run = Run{moduleConfiguration};
throwIfInvalid(run.RxPredeccesor);
while ( std::ranges::any_of(run.RxPredeccesor->PulseLoop, [](auto pair) noexcept { return pair.second == 0; }) ) {
run.press(1);
} //while ( std::ranges::any_of(run.RxPredeccesor->PulseLoop, [](auto pair) noexcept { return pair.second == 0; }) )
auto result = std::ranges::fold_left(run.RxPredeccesor->PulseLoop |
std::views::transform(&std::pair<const ModuleName, std::int64_t>::second),
1, std::lcm<std::int64_t, std::int64_t>);
myPrint(" == Result of Part 2: {:d} ==\n", result);
return result1 == 879'834'312 && result == 243037165713371;
}