first commit

This commit is contained in:
2025-11-21 23:12:15 -06:00
commit aa5cb355ee
8 changed files with 419 additions and 0 deletions

150
src/main-solution-1.cpp Normal file
View File

@@ -0,0 +1,150 @@
#include "spdlog/fmt/bundled/format.h"
#include "spdlog/spdlog.h"
#include <fstream>
#include <ios>
#include <cstdlib> // for rand
#include <ctime> // for time
#include <iostream>
#include <ostream>
#include <vector>
#include <queue>
#include <utility>
#include <filesystem>
namespace fs = std::filesystem;
int solution(int n, int m, const std::vector<std::vector<char>>& grid2d) {
if (n == 0 || m == 0) return -1;
// Flatten 2D grid into 1D vector
std::vector<char> grid_flat;
grid_flat.reserve(n * m);
for (const auto& row : grid2d) {
for (char c : row) {
grid_flat.push_back(c);
}
}
const char* base = grid_flat.data(); // pointer to flattened grid
std::vector<char> visited(n * m, 0); // visited array: 0 = unvisited, 1 = visited
char* vptr = visited.data(); // pointer to visited
// Directions: up, down, left, right
static const int dx[4] = {-1, 1, 0, 0};
static const int dy[4] = {0, 0, -1, 1};
std::queue<std::pair<int,int>> q;
q.push({0, 0});
*vptr = 1; // mark start as visited
int steps = 0;
while (!q.empty()) {
int layer_size = q.size();
while (layer_size--) {
auto p = q.front(); q.pop();
int x = p.first;
int y = p.second;
const char* cell = base + x * m + y;
if (*cell == '*') return steps;
for (int d = 0; d < 4; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
continue;
int idx = nx * m + ny;
if (*(vptr + idx)) continue;
const char* neighbor = base + idx;
if (*neighbor == '#') continue;
*(vptr + idx) = 1;
q.push({nx, ny});
}
}
++steps;
}
return -1; // goal unreachable
}
std::vector<std::vector<char>> generate_maze(int n, int m, double wall_prob = 0.3) {
std::srand(std::time(nullptr)); // seed RNG
std::vector<std::vector<char>> grid(n, std::vector<char>(m, '0'));
// Fill random walls
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if ((i == 0 && j == 0) || (i == n-1 && j == m-1)) continue; // start & goal safe
double r = static_cast<double>(std::rand()) / RAND_MAX;
if (r < wall_prob) {
grid[i][j] = '#';
}
}
}
// Set goal
grid[n-1][m-1] = '*';
return grid;
}
std::string format_grid(const std::vector<std::vector<char>>& grid) {
std::string result;
for (std::size_t i = 0; i < grid.size(); ++i) {
for (std::size_t j = 0; j < grid[i].size(); ++j) {
result += fmt::format("{}", grid[i][j]);
if (j + 1 < grid[i].size()) result += " ";
}
if (i + 1 < grid.size()) result += "\n";
}
return result;
}
int main () {
spdlog::info("Logging is functional");
std::ios::sync_with_stdio(false);
fs::path folder = "solution-1-files"; // folder you want
try {
// If folder exists, remove it and all contents
if (fs::exists(folder)) {
fs::remove_all(folder);
}
// Create fresh folder
fs::create_directory(folder);
spdlog::debug("Folder {} is ready", folder.string());
} catch (const fs::filesystem_error& e) {
std::cerr << "Filesystem error: " << e.what() << std::endl;
return 1;
}
int counter = {0};
while(counter<1000){
for (int n={5}; n<100; n++) {
for (int m={5}; m<100; m++) {
for (double wall_prob={0.01}; wall_prob<0.5; wall_prob+=0.01) {
std::vector<std::vector<char>> grid = generate_maze(n,m,wall_prob);
int answer = solution(n, m, grid);
if (answer==-1) continue;
std::ofstream input_file = std::ofstream{fmt::format("{}/{}-{}-{}.in", folder.string(), n,m,wall_prob)};
std::ofstream out_file = std::ofstream{fmt::format("{}/{}-{}-{}.out", folder.string(), n,m,wall_prob)};
input_file << fmt::format("{} {}\n", n,m);
input_file << format_grid(grid);
input_file.flush(); out_file.flush(); input_file.close(); out_file.close();
counter++;
}
}
}
}
return 0;
}