Skip to content

Commit b05d5ab

Browse files
committed
feat: possibly working
1 parent 4eca599 commit b05d5ab

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

src/apps/2024day6/src/strategies/MinimalLoopStrategy.h

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ class MinimalLoopStrategy : public Core::IStrategy {
3030
std::vector<GameObject> obstructions;
3131
GameObject guard;
3232
std::vector<GameObject> path;
33-
double pathTotalVelocity = 0;
33+
std::vector<GameObject> distinctPositions;
34+
std::vector<std::string> recentObstructionCoords;
3435

3536
int boundaryHeight;
3637
int boundaryWidth;
3738

38-
int scale = 40;
39+
int scale = 10;
3940
int defaultVelocity = 100;
4041

4142
double totalTime = 0;
42-
double lastPositionChange = 0;
4343

4444
void Init(Core::Window& window, Core::Renderer& renderer) override {
4545
Core::AssetStore::Instance().AddFont("pico8", "assets/fonts/pico8.ttf", 24);
4646

4747
auto size = 1 * scale;
4848

49-
const std::string filePath = "assets/input-example-1.txt";
49+
const std::string filePath = "assets/input.txt";
5050
auto input = ParseInput(filePath);
5151

5252
boundaryWidth = input[0].size() * size;
@@ -109,71 +109,99 @@ class MinimalLoopStrategy : public Core::IStrategy {
109109
guard.x += guard.velX * deltaTime;
110110
guard.y += guard.velY * deltaTime;
111111

112-
std::cout << totalTime << std::endl;
113-
114112
for (auto& obstruction : obstructions) {
115-
GameObject guardTemp = guard;
116-
guardTemp.x += guard.velX * deltaTime;
117-
guardTemp.y += guard.velY * deltaTime;
118-
119-
if (areOverlapping(guardTemp, obstruction) &&
120-
totalTime - lastPositionChange > 0.4) {
113+
auto obstructionCoords =
114+
std::to_string(obstruction.x) + "-" + std::to_string(obstruction.y);
115+
auto isInRecentObstructionCoords = std::find(
116+
recentObstructionCoords.begin(),
117+
recentObstructionCoords.end(),
118+
obstructionCoords
119+
) != recentObstructionCoords.end();
120+
121+
if (areOverlapping(guard, obstruction) && !isInRecentObstructionCoords) {
121122
auto pastVelY = guard.velY;
122123
auto pastVelX = guard.velX;
123124

124125
guard.velY = 0;
125126
guard.velX = 0;
126127

127128
if (pastVelY < 0) {
128-
std::cout << "a" << std::endl;
129129
guard.velY = 0;
130130
guard.velX = defaultVelocity;
131131
}
132132

133133
if (pastVelX > 0) {
134-
std::cout << "b" << std::endl;
135-
136134
guard.velX = 0;
137135
guard.velY = defaultVelocity;
138136
}
139137

140138
if (pastVelY > 0) {
141-
std::cout << "c" << std::endl;
142-
143139
guard.velY = 0;
144140
guard.velX = -defaultVelocity;
145141
}
146142

147143
if (pastVelX < 0) {
148-
std::cout << "d" << std::endl;
149-
150144
guard.velX = 0;
151145
guard.velY = -defaultVelocity;
152146
}
153147

154-
lastPositionChange = totalTime;
148+
recentObstructionCoords.push_back(obstructionCoords);
149+
if (recentObstructionCoords.size() > 2) {
150+
recentObstructionCoords.erase(recentObstructionCoords.begin());
151+
}
155152
}
156153
}
157154

155+
// fix y if x is moving
156+
if (std::abs(guard.velX) > 0) {
157+
guard.y = std::round(guard.y / scale) * scale;
158+
}
159+
160+
// fix x if y is moving
161+
if (std::abs(guard.velY) > 0) {
162+
guard.x = std::round(guard.x / scale) * scale;
163+
}
164+
158165
auto pathElement = GameObject(
159166
1,
160167
1,
161-
guard.x + guard.width/2,
162-
guard.y + guard.height/2,
168+
guard.x + guard.width / 2,
169+
guard.y + guard.height / 2,
163170
0,
164171
0,
165172
{0, 255, 0}
166173
);
167174

168175
// if path element out of bounds
169-
if (pathElement.x > boundaryWidth || pathElement.y > boundaryHeight) {
176+
if (pathElement.x > boundaryWidth || pathElement.y > boundaryHeight ||
177+
pathElement.x < 0 || pathElement.y < 0) {
170178
guard.velX = 0;
171179
guard.velY = 0;
172180

173-
std::cout << "Path total velocity: " << pathTotalVelocity << std::endl;
181+
// end game
182+
std::cout << "Game over" << std::endl;
183+
std::cout << "Distinct positions: " << distinctPositions.size()
184+
<< std::endl;
174185
} else {
175-
pathTotalVelocity += std::abs(guard.velX) + std::abs(guard.velY);
176186
path.push_back(pathElement);
187+
188+
auto snappedX = std::round(guard.x / scale) * scale;
189+
auto snappedY = std::round(guard.y / scale) * scale;
190+
191+
auto distinctPathPiece =
192+
GameObject(scale, scale, snappedX, snappedY, 0, 0, {0, 255, 0});
193+
194+
auto isOverlapping = false;
195+
for (auto& distinctPiece : distinctPositions) {
196+
if (areOverlapping(distinctPiece, distinctPathPiece)) {
197+
isOverlapping = true;
198+
break;
199+
}
200+
}
201+
202+
if (!isOverlapping) {
203+
distinctPositions.push_back(distinctPathPiece);
204+
}
177205
}
178206
}
179207

@@ -185,6 +213,9 @@ class MinimalLoopStrategy : public Core::IStrategy {
185213
for (auto& pathElement : path) {
186214
pathElement.Render(renderer);
187215
}
216+
for (auto& distinctPiece : distinctPositions) {
217+
distinctPiece.Render(renderer);
218+
}
188219

189220
SDL_Rect rect = {0, 0, boundaryWidth, boundaryHeight};
190221
SDL_SetRenderDrawColor(renderer.Get().get(), 255, 255, 255, 255);

0 commit comments

Comments
 (0)