setup major background and helper functions

This commit is contained in:
Sandipsinh Rathod 2024-10-09 21:53:49 -04:00
parent c8388bd7e6
commit f7e0dcfd86

147
main.cpp

@ -1,10 +1,8 @@
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <sstream>
#include <vector>
#include <bits/fs_fwd.h>
#include <bits/fs_path.h>
#define pt cout <<
#define nl '\n'
@ -14,25 +12,25 @@ using namespace std;
// -------- START RAII HELPERS--------
unsigned long allocated_mem = 0;
unsigned long allocatedMem = 0;
void *alloc(size_t size) {
void *ptr = malloc(size);
if (ptr == nullptr) {
cerr << "Memory allocation failed" << endl;
cerr << "Memory allocation failed" << nl;
return nullptr;
}
allocated_mem += 1;
allocatedMem += 1;
return ptr;
}
void dealloc(void *ptr) {
if (ptr) free(ptr);
allocated_mem -= 1;
allocatedMem -= 1;
}
bool assert_alloc() {
return !allocated_mem;
return !allocatedMem;
}
// -------- END RAII HELPERS--------
@ -49,6 +47,10 @@ struct Moves {
vector<Move> moves;
};
bool isValidPlayer(char c) {
return c >= 'A' && c <= 'Z' && c != 'X';
}
Moves serialize(const string &s) {
Moves result;
istringstream iss(s);
@ -60,6 +62,10 @@ Moves serialize(const string &s) {
int x, y;
while (iss >> player >> x >> y) {
if (player == "END") break;
if (!isValidPlayer(player[0])) {
string err = "Invalid player: " + player;
throw std::invalid_argument(err);
}
result.moves.push_back({player[0], x, y});
}
@ -69,7 +75,7 @@ Moves serialize(const string &s) {
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) ? ' ': '.';
board[i][j] = (j & 1 || i & 1) ? ' ' : '.';
}
}
}
@ -99,15 +105,118 @@ void printBoard(char **board, int x, int y) {
}
}
void solve(Moves moves, char **board) {
char toLowerCase(char c) {
return c | 32;
}
int normalizePlayer(char c) {
return toLowerCase(c) - 'a';
}
void printScores(map<char, int> &points) {
// TODO: change this to custom sort before submission
vector<pair<char, int> > ranking(points.begin(), points.end());
sort(ranking.begin(), ranking.end(), [](const pair<char, int> &a, const pair<char, int> &b) {
return a.second < b.second;
});
// Print the ranks
for (const auto &rank: ranking) {
pt rank.first << ": " << rank.second << " boxes" << nl;
}
}
bool isValidMove(int x, int y) {
// TODO: simplify this
return !(x & 1 != y & 1 && !(x & 1));
}
void solve(Moves moves, char **board) {
map<char, int> points;
for (Move &move: moves.moves) {
int moveX = move.moveX;
int moveY = move.moveY;
char player = move.player;
if (!isValidMove(moveX, moveY)) {
pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl;
board[moveX][moveY] = 'X';
printBoard(board, moves.x, moves.y);
printScores(points);
pt "Exiting.." << nl;
return;
}
board[moveX][moveY] = toLowerCase(player);
// TODO:
if (moveX % 2 == 0 && moveY % 2 == 1) {
} else if (moveX % 2 == 1 && moveY % 2 == 0) {
}
}
printScores(points);
printBoard(board, moves.x, moves.y);
}
/*
void solve(Moves moves, char **board) {
/*
TODO: use custom arr for points over map
*int *points = static_cast<int *>(alloc(26));
for (int i = 0; i < 26; i++) {
points[i] = 0;
}#1#
map<char, int> points;
for (const auto &move: moves.moves) {
int moveX = move.moveX;
int moveY = move.moveY;
char player = move.player;
board[moveX][moveY] = toLowerCase(player);
if (moveX % 2 == 0 && moveY % 2 == 1) {
if (moveX > 0 && board[moveX - 1][moveY] == '|' && board[moveX - 1][moveY - 1] == '-' && board[moveX - 1][moveY + 1] == '-') {
points[player]++;
}
if (moveX < moves.x - 1 && board[moveX + 1][moveY] == '|' && board[moveX + 1][moveY - 1] == '-' && board[moveX + 1][moveY + 1] == '-') {
points[player]++;
}
} else if (moveX % 2 == 1 && moveY % 2 == 0) {
if (moveY > 0 && board[moveX][moveY - 1] == '-' && board[moveX - 1][moveY - 1] == '|' && board[moveX + 1][moveY - 1] == '|') {
points[player]++;
}
if (moveY < moves.y - 1 && board[moveX][moveY + 1] == '-' && board[moveX - 1][moveY + 1] == '|' && board[moveX + 1][moveY + 1] == '|') {
points[player]++;
}
}
}
vector<pair<char, int> > ranking(points.begin(), points.end());
sort(ranking.begin(), ranking.end(), [](const pair<char, int> &a, const pair<char, int> &b) {
return a.second < b.second;
});
for (const auto &rank: ranking) {
pt rank.first << ": " << rank.second << " boxes" << nl;
}
printBoard(board, moves.x, moves.y);
// dealloc(points);
}
*/
int init(char *fileName) {
// --- IO START ---
FILE *inp = fopen(fileName, "r");
if (inp == nullptr) {
cerr << "File not found" << endl;
cerr << "File not found" << nl;
return 1;
}
@ -135,7 +244,7 @@ int init(char *fileName) {
fclose(inp);
if (!assert_alloc()) {
cerr << "Memory leak detected" << endl;
cerr << "Memory leak detected" << nl;
return 1;
}
// --- CLEANUP END ---
@ -144,8 +253,14 @@ int init(char *fileName) {
int main(int argc, char **argv) {
if (argc < 2) {
cerr << "No input file provided" << endl;
cerr << "No input file provided" << nl;
return 1;
}
try {
return init(argv[1]);
} catch (const std::invalid_argument &e) {
cerr << e.what() << nl;
return 1;
}
return init(argv[1]);
}