-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.cpp
More file actions
313 lines (266 loc) · 8.29 KB
/
day14.cpp
File metadata and controls
313 lines (266 loc) · 8.29 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
#include "day14.h"
#include "helpers.h"
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
namespace day14
{
long long solvePart1(std::ifstream& file);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 14 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day14_example.txt" : "inputs/day14_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';
}
enum class Rock
{
empty,
round,
square
};
struct Platform
{
std::vector<std::vector<Rock>> field{};
void print() const
{
std::cout << '\n';
for (const auto& row : field)
{
for (const auto rock : row)
{
switch (rock) {
case Rock::empty:
std::cout << '.';
break;
case Rock::round:
std::cout << 'O';
break;
case Rock::square:
std::cout << '#';
break;
}
}
std::cout << '\n';
}
}
void rollNorth()
{
for(size_t x = 0; x < field[0].size(); x++)
{
size_t nextAvailableSpot{};
for (size_t y = 0; y < field.size(); y++)
{
switch (field[y][x])
{
case Rock::round:
// Move the round rock up
field[y][x] = Rock::empty;
field[nextAvailableSpot][x] = Rock::round;
nextAvailableSpot++;
break;
case Rock::square:
nextAvailableSpot = y + 1;
break;
case Rock::empty:
break;
}
}
}
}
void rollSouth()
{
for (size_t x = 0; x < field[0].size(); x++)
{
size_t nextAvailableSpot{field.size() - 1};
for (size_t y = field.size() - 1; y < field.size(); y--)
{
switch (field[y][x])
{
case Rock::round:
// Move the round rock down
field[y][x] = Rock::empty;
field[nextAvailableSpot][x] = Rock::round;
nextAvailableSpot--;
break;
case Rock::square:
nextAvailableSpot = y - 1;
break;
case Rock::empty:
break;
}
}
}
}
void rollWest()
{
for (size_t y = 0; y < field.size(); y++)
{
size_t nextAvailableSpot{};
for (size_t x = 0; x < field[0].size(); x++)
{
switch (field[y][x])
{
case Rock::round:
// Move the round rock west
field[y][x] = Rock::empty;
field[y][nextAvailableSpot] = Rock::round;
nextAvailableSpot++;
break;
case Rock::square:
nextAvailableSpot = x + 1;
break;
case Rock::empty:
break;
}
}
}
}
void rollEast()
{
for (size_t y = 0; y < field.size(); y++)
{
size_t nextAvailableSpot{field[0].size() - 1};
for (size_t x = field[0].size() - 1; x < field[0].size(); x--)
{
switch (field[y][x])
{
case Rock::round:
// Move the round rock east
field[y][x] = Rock::empty;
field[y][nextAvailableSpot] = Rock::round;
nextAvailableSpot--;
break;
case Rock::square:
nextAvailableSpot = x - 1;
break;
case Rock::empty:
break;
}
}
}
}
void cycleRoll(bool debug = false)
{
rollNorth();
if (debug)
{
print();
}
rollWest();
if (debug)
{
print();
}
rollSouth();
if (debug)
{
print();
}
rollEast();
if (debug)
{
print();
}
}
[[nodiscard]] long long scoreNorthWeight() const
{
long long sum{};
for (size_t x = 0; x < field[0].size(); x++)
{
for (size_t y = 0; y < field.size(); y++)
{
if (field[y][x] == Rock::round)
{
sum += static_cast<long long>(field.size() - y);
}
}
}
return sum;
}
bool operator==(const Platform& other) const
{
for (size_t y = 0; y < field.size(); y++)
{
for (size_t x = 0; x < field[0].size(); x++)
{
if (other.field[y][x] != field[y][x])
{
return false;
}
}
}
return true;
}
};
Platform parseInput(std::ifstream& file)
{
std::string line;
std::vector<std::vector<Rock>> field{};
while(!file.eof())
{
std::vector<Rock> row;
std::getline(file, line);
for (const char i : line)
{
switch (i)
{
case 'O':
row.push_back(Rock::round);
break;
case '#':
row.push_back(Rock::square);
break;
default:
row.push_back(Rock::empty);
break;
}
}
field.push_back(row);
}
return Platform{ field };
}
long long solvePart1(std::ifstream& file)
{
auto platform = parseInput(file);
platform.rollNorth();
// platform.print();
return platform.scoreNorthWeight();
}
long long solvePart2(std::ifstream& file)
{
auto platform = parseInput(file);
// Assumption: the cycling will hit a fairly short cycle pretty quickly, and then we can just calculate final result from that.
std::vector<Platform> previousStates{};
long long iteration{};
long long offsetOfCycle;
long long cycleLength;
while(true)
{
auto match = std::ranges::find(previousStates, platform);
if (match != previousStates.end())
{
offsetOfCycle = match - previousStates.begin();
cycleLength = iteration - offsetOfCycle;
break;
}
previousStates.push_back(platform);
iteration++;
platform.cycleRoll();
}
// Know the cycle now, we can calculate from here:
constexpr long long targetIterations = 1000000000;
const long long placeInCycle{ (targetIterations - offsetOfCycle) % cycleLength };
const auto finalPlatform{ previousStates[static_cast<size_t>(offsetOfCycle + placeInCycle)] };
// platform.print();
return finalPlatform.scoreNorthWeight();
}
}