-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21.cpp
More file actions
248 lines (204 loc) · 7.07 KB
/
day21.cpp
File metadata and controls
248 lines (204 loc) · 7.07 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
#include "day21.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 day21
{
long long solvePart1(std::ifstream& file, bool example);
long long solvePart2(std::ifstream& file, bool example);
void run_day(bool example)
{
std::cout << "Running day 21 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day21_example.txt" : "inputs/day21_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file, example) << '\n';
file.close();
file.open(fileName);
if (example)
{
std::cout << "Current solution here only really works for real input!\n";
std::cout << "Part 2 answer: " << solvePart2(file, example) << '\n';
}
else
{
std::cout << "Current solution doesn't actually solve, just gives values to determine quadratic formula with!\n";
std::cout << "Part 2 answer: " << solvePart2(file, example) << '\n';
}
}
struct Point
{
size_t x{};
size_t y{};
[[nodiscard]] Point operator+(const Point& other) const
{
return Point{ x + other.x, y + other.y };
}
};
std::array<Point, 4> directionOffsets{
Point(-1, 0),
Point(1, 0),
Point(0, -1),
Point(0, 1)
};
struct WalkState
{
Point p{};
int stepsTaken{};
};
struct Garden
{
std::vector<std::vector<bool>> gardenPlots{};
std::vector<std::vector<int>> nrOfStepsNeeded{};
size_t width;
size_t height;
size_t startX;
size_t startY;
void parseInput(std::ifstream& file)
{
size_t y{};
while(!file.eof())
{
std::vector<bool> gardenRow{};
std::string line;
std::getline(file, line);
size_t x{};
for (const char c : line)
{
gardenRow.push_back(c == '.' || c == 'S');
if (c == 'S')
{
startX = x;
startY = y;
}
x++;
}
gardenPlots.push_back(gardenRow);
y++;
}
width = gardenPlots[0].size();
height = gardenPlots.size();
nrOfStepsNeeded = std::vector(height, std::vector(width, -1));
}
// Simple breadth first fill out of (bounded) min steps
void fillOutStepsNeeded()
{
std::queue<WalkState> queue{};
queue.emplace(Point{ startX, startY }, 0);
while(!queue.empty())
{
const WalkState& w{ queue.front() };
if(nrOfStepsNeeded[w.p.y][w.p.x] >= 0)
{
queue.pop();
continue;
}
nrOfStepsNeeded[w.p.y][w.p.x] = w.stepsTaken;
for (const auto& directionOffset : directionOffsets)
{
Point newPoint{ w.p + directionOffset };
// Using overflow to check for lower bound
if (newPoint.x < width && newPoint.y < height
&& gardenPlots[newPoint.y][newPoint.x])
{
queue.emplace(newPoint, w.stepsTaken + 1);
}
}
queue.pop();
}
}
long long partTwoBreadthFirstScore(const long long nrOfSteps)
{
// Horrible ugly solution generating extended map
const auto nrOfExtraCopies{ (nrOfSteps + (width/2)) / width };
std::vector nrOfStepsNeededWithCopies(height * (1 + 2 * nrOfExtraCopies), std::vector(width * (1 + 2 * nrOfExtraCopies), -1));
std::queue<WalkState> queue{};
// start offset by nrOfExtraCopies copies on x & y
queue.emplace(Point{ startX + nrOfExtraCopies * width, startY + nrOfExtraCopies * height }, 0);
while (!queue.empty())
{
const WalkState& w{ queue.front() };
if (w.stepsTaken > nrOfSteps)
{
break;
}
if (nrOfStepsNeededWithCopies[w.p.y][w.p.x] >= 0)
{
queue.pop();
continue;
}
nrOfStepsNeededWithCopies[w.p.y][w.p.x] = w.stepsTaken;
for (const auto& directionOffset : directionOffsets)
{
// note skipping over logic for edge walking here since we added extra copies anyway
Point newPoint{ w.p + directionOffset };
if (gardenPlots[newPoint.y % height][newPoint.x % width])
{
queue.emplace(newPoint, w.stepsTaken + 1);
}
}
queue.pop();
}
long long sum{};
for (const auto& row : nrOfStepsNeededWithCopies)
{
for (const auto r : row)
{
if (r % 2 == (nrOfSteps % 2))
{
sum++;
}
}
}
return sum;
}
[[nodiscard]] long long scoreNrOfGardensPartOne(const int nrOfSteps) const
{
long long sum{};
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
if (!gardenPlots[y][x])
{
continue;
}
const auto steps{ nrOfStepsNeeded[y][x] };
// Can always 'skip' two steps by walking back and forth, so anything
// below nrOfSteps is reachable if matches %2.
if (steps <= nrOfSteps && (steps % 2) == (nrOfSteps % 2))
{
sum++;
}
}
}
return sum;
}
};
long long solvePart1(std::ifstream& file, bool example)
{
Garden garden{};
garden.parseInput(file);
garden.fillOutStepsNeeded();
return garden.scoreNrOfGardensPartOne(example ? 6 : 64);
}
long long solvePart2(std::ifstream& file, bool example)
{
Garden garden{};
garden.parseInput(file);
std::cout << garden.partTwoBreadthFirstScore(65LL) << '\n';
std::cout << garden.partTwoBreadthFirstScore(65LL + garden.width) << '\n';
std::cout << garden.partTwoBreadthFirstScore(65LL + 2 * garden.width) << '\n';
std::cout << garden.partTwoBreadthFirstScore(65LL + 3 * garden.width) << '\n';
return 0;
}
}