add some helper functions

This commit is contained in:
Sandipsinh Rathod 2024-10-09 20:36:29 -04:00
parent f42154621f
commit c8388bd7e6

@ -2,6 +2,9 @@
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <bits/fs_fwd.h>
#include <bits/fs_path.h>
#define pt cout <<
#define nl '\n'
@ -36,11 +39,13 @@ bool assert_alloc() {
struct Move {
char player;
int moveX, moveY;
int moveX;
int moveY;
};
struct Moves {
int x{}, y{};
int x;
int y;
vector<Move> moves;
};
@ -48,6 +53,8 @@ Moves serialize(const string &s) {
Moves result;
istringstream iss(s);
iss >> result.x >> result.y;
result.x <<= 1;
result.y <<= 1;
string player;
int x, y;
@ -58,18 +65,46 @@ Moves serialize(const string &s) {
return result;
}
void solve(const string& fileInp) {
Moves moves = serialize(fileInp);
void initBoard(char **board, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; j++) {
board[i][j] = (j&1 || i&1) ? ' ': '.';
}
}
}
char **allocAndInitBoard(int x, int y) {
char **board = static_cast<char **>(alloc(sizeof(char *) * x));
for (int i = 0; i < x; ++i) {
board[i] = static_cast<char *>(alloc(sizeof(char) * y));
}
initBoard(board, x, y);
return board;
}
void deallocBoard(char **board, int x) {
for (int i = 0; i < x; ++i) {
dealloc(board[i]);
}
dealloc(board);
}
void printBoard(char **board, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
pt board[i][j];
}
pt nl;
}
}
void solve(Moves moves, char **board) {
}
int main(int argc, char **argv) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <input_file>" << nl;
return 1;
}
char* fileName = argv[1];
int init(char *fileName) {
// --- IO START ---
FILE *inp = fopen(fileName, "r");
if (inp == nullptr) {
cerr << "File not found" << endl;
@ -77,7 +112,7 @@ int main(int argc, char **argv) {
}
fseek(inp, 0L, SEEK_END);
long sz = ftell(inp);
const long sz = ftell(inp);
fseek(inp, 0L, SEEK_SET);
char *fileInpPtr = static_cast<char *>(alloc(sz + 1 * sizeof(char)));
@ -86,16 +121,31 @@ int main(int argc, char **argv) {
for (int i = 0; i < sz; ++i) {
fscanf(inp, "%c", &fileInpPtr[i]);
}
// --- IO END ---
solve(fileInpPtr);
// --- ALGORITHM START ---
Moves moves = serialize(fileInpPtr);
char **board = allocAndInitBoard(moves.x, moves.y);
solve(moves, board);
// --- ALGORITHM END ---
// --- CLEANUP START ---
dealloc(fileInpPtr);
deallocBoard(board, moves.x);
fclose(inp);
if (!assert_alloc()) {
cerr << "Memory leak detected" << endl;
return 1;
}
// --- CLEANUP END ---
return 0;
}
int main(int argc, char **argv) {
if (argc < 2) {
cerr << "No input file provided" << endl;
return 1;
}
return init(argv[1]);
}