-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday19.cpp
More file actions
344 lines (290 loc) · 10.3 KB
/
day19.cpp
File metadata and controls
344 lines (290 loc) · 10.3 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
#include "day19.h"
#include "helpers.h"
#include <array>
#include <cassert>
#include <fstream>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <ranges>
#include <set>
#include <sstream>
#include <string>
#include <vector>
namespace day19
{
long long solvePart1(std::ifstream& file);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 19 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day19_example.txt" : "inputs/day19_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file) << '\n';
file.close();
file.open(fileName);
std::cout << "Part 2 answer: " << solvePart2(file) << '\n';
}
struct Part
{
// x, m, a, s
std::array<long long, 4> categories{};
Part(const std::string& inputLine)
{
std::stringstream ss{};
ss << inputLine;
ss.ignore(3);
ss >> categories[0];
ss.ignore(3);
ss >> categories[1];
ss.ignore(3);
ss >> categories[2];
ss.ignore(3);
ss >> categories[3];
}
[[nodiscard]] long long score() const
{
return categories[0] + categories[1] + categories[2] + categories[3];
}
};
struct Rule
{
bool hasCondition{};
size_t conditionIndex{};
bool lessThan{};
int compareValue{};
std::string target{};
Rule(const std::string_view ruleString)
{
if(ruleString.find(':') == std::string::npos)
{
// Fallback case
hasCondition = false;
target = ruleString;
}
else
{
// Conditional case
hasCondition = true;
switch (ruleString[0])
{
case 'x':
conditionIndex = 0;
break;
case 'm':
conditionIndex = 1;
break;
case 'a':
conditionIndex = 2;
break;
case 's':
conditionIndex = 3;
break;
default:
assert(false);
}
lessThan = ruleString[1] == '<';
std::stringstream ss{};
ss << ruleString;
ss.ignore(2);
ss >> compareValue;
const size_t sepPos{ ruleString.find(':') };
target = ruleString.substr(sepPos + 1, ruleString.size() - sepPos - 1);
}
}
};
struct Workflow
{
std::string name;
std::vector<Rule> rules;
Workflow()
= default;
Workflow(const std::string_view inputLine)
{
const auto bracketPos{ inputLine.find('{') };
name = inputLine.substr(0, bracketPos);
const std::string rulesString{ inputLine.substr(bracketPos + 1, inputLine.size() - bracketPos - 2) };
const auto ruleStrings{ splitStringBySeperator(rulesString, ',') };
for (const auto& ruleString : ruleStrings)
{
rules.emplace_back(ruleString);
}
}
};
struct RatingsRegion
{
std::array<std::pair<int, int>, 4> rangePerCategory{};
std::string currentWorkflowName{};
[[nodiscard]] long long nrOfOptions() const
{
long long options{ 1 };
for (size_t i= 0; i<4;i++)
{
options *= static_cast<long long>(rangePerCategory[i].second - rangePerCategory[i].first + 1);
}
return options;
}
};
struct Puzzle
{
std::vector<Part> parts{};
std::vector<Workflow> flows{};
std::unordered_map<std::string, Workflow> flowLookup{};
std::vector<Part> acceptedParts{};
std::vector<RatingsRegion> acceptedRatingRegions{};
void parseInput(std::ifstream& file)
{
std::string line;
std::getline(file, line);
while(!line.empty())
{
Workflow f(line);
flows.push_back(f);
flowLookup[f.name] = f;
std::getline(file, line);
}
while(!file.eof())
{
std::getline(file, line);
parts.emplace_back(line);
}
}
void runPartsThroughFlows()
{
for (auto part : parts)
{
std::string currentFlowName{ "in" };
while(currentFlowName != "A" && currentFlowName != "R")
{
const Workflow& f{ flowLookup[currentFlowName] };
for (const auto& rule : f.rules)
{
if (!rule.hasCondition)
{
currentFlowName = rule.target;
break;
}
if (rule.lessThan
? part.categories[rule.conditionIndex] < rule.compareValue
: part.categories[rule.conditionIndex] > rule.compareValue)
{
currentFlowName = rule.target;
break;
}
}
}
if (currentFlowName == "A")
{
acceptedParts.push_back(part);
}
}
}
void determineRatingRegions()
{
const RatingsRegion initialFullRegion{
std::array{std::pair{1, 4000}, std::pair{1, 4000}, std::pair{1, 4000}, std::pair{1, 4000}},
"in"
};
std::vector<RatingsRegion> regionsToProcess{ initialFullRegion };
while(!regionsToProcess.empty())
{
RatingsRegion region{ regionsToProcess.back() };
regionsToProcess.pop_back();
if(region.currentWorkflowName == "R")
{
// Rejected
continue;
}
if (region.currentWorkflowName == "A")
{
// Accepted
acceptedRatingRegions.push_back(region);
continue;
}
// Applied to a workflow
const Workflow& flow{ flowLookup[region.currentWorkflowName] };
for (const auto& rule : flow.rules)
{
if (!rule.hasCondition)
{
// Send full remainder of region to target
regionsToProcess.emplace_back(region.rangePerCategory, rule.target);
break;
}
if (rule.lessThan)
{
if (region.rangePerCategory[rule.conditionIndex].second < rule.compareValue)
{
// Entire range falls in condition
regionsToProcess.emplace_back(region.rangePerCategory, rule.target);
break;
}
if (region.rangePerCategory[rule.conditionIndex].first > rule.compareValue)
{
// Entire range falls out of condition
continue;
}
// Have to split range up
RatingsRegion regionInCondition{ region.rangePerCategory, rule.target };
regionInCondition.rangePerCategory[rule.conditionIndex].second = rule.compareValue - 1;
regionsToProcess.push_back(regionInCondition);
// Update current region to remainder
region.rangePerCategory[rule.conditionIndex].first = rule.compareValue;
}
else
{
if (region.rangePerCategory[rule.conditionIndex].first > rule.compareValue)
{
// Entire range falls in condition
regionsToProcess.emplace_back(region.rangePerCategory, rule.target);
break;
}
if (region.rangePerCategory[rule.conditionIndex].second < rule.compareValue)
{
// Entire range falls out of condition
continue;
}
// Have to split range up
RatingsRegion regionInCondition{ region.rangePerCategory, rule.target };
regionInCondition.rangePerCategory[rule.conditionIndex].first = rule.compareValue + 1;
regionsToProcess.push_back(regionInCondition);
// Update current region to remainder
region.rangePerCategory[rule.conditionIndex].second = rule.compareValue;
}
}
}
}
[[nodiscard]] long long scoreAcceptedParts() const
{
long long sum{};
for (const auto& acceptedPart : acceptedParts)
{
sum += acceptedPart.score();
}
return sum;
}
[[nodiscard]] long long scoreRatingPossibilities() const
{
long long sum{};
for (const auto& acceptedRegion: acceptedRatingRegions)
{
sum += acceptedRegion.nrOfOptions();
}
return sum;
}
};
long long solvePart1(std::ifstream& file)
{
Puzzle puzzle{};
puzzle.parseInput(file);
puzzle.runPartsThroughFlows();
return puzzle.scoreAcceptedParts();
}
long long solvePart2(std::ifstream& file)
{
Puzzle puzzle{};
puzzle.parseInput(file);
puzzle.determineRatingRegions();
return puzzle.scoreRatingPossibilities();
}
}