diff --git a/main.cpp b/main.cpp index f1e1790..10219ec 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,7 @@ #define pt cout << #define pter cerr << -#define nl '\n' +#define nl "\n" #define in cin >> using namespace std; @@ -117,37 +117,64 @@ char toLowerCase(char c) { return c | 32; } -int normalizePlayer(char c) { +int normalize(char c) { return toLowerCase(c) - 'a'; } -void printScores(map &points) { - // TODO: change this to custom sort before submission - vector > ranking(points.begin(), points.end()); - sort(ranking.begin(), ranking.end(), [](const pair &a, const pair &b) { - return a.second < b.second; - }); +void swap(pair *a, int i, int j) { + pair temp = a[i]; + a[i] = a[j]; + a[j] = temp; +} - // Print the ranks - for (const auto &rank: ranking) { - pt rank.first << ": " << rank.second << " boxes" << nl; +void bubbleSort(pair *a) { + for (int i = 0; i < 25; i++) { + for (int j = 0; j < 25 - i; j++) { + if (a[j].second > a[j + 1].second) + swap(a, j, j + 1); + } } } + +void printScores(int *points) { + pair *arr = static_cast *>(alloc(26 * sizeof(pair))); + for (int i = 0; i < 26; i++) { + arr[i] = {static_cast(i + 'a'), points[i]}; + } + + bubbleSort(arr); + + int prev = -1; + + for (int i = 0; i < 26; i++) { + if (arr[i].second != -1) { + pt arr[i].first << ": " << arr[i].second << " boxes" << (i == 25 || prev == arr[i].second ? " (win) \n" : nl); + prev = arr[i].second; + } + } + + dealloc(arr); +} + bool isValidMove(int x, int y) { // TODO: simplify this return !(x & 1 != y & 1 && !(x & 1)); } void addPoint( - map &points, + int *points, char **board, int x, int y, char player ) { if (board[x][y] == ' ') { - points[player]++; + if (points[normalize(player)] == -1) { + points[normalize(player)] = 1; + } else { + points[normalize(player)]++; + } board[x][y] = player; } } @@ -156,7 +183,7 @@ void solveVertical( int moveX, int moveY, char **board, - map &points, + int *points, int x, int y, char player @@ -190,7 +217,7 @@ void solveHorizontal( int moveX, int moveY, char **board, - map &points, + int *points, int y, char player ) { @@ -219,7 +246,10 @@ void solveHorizontal( } void solve(Moves moves, char **board) { - map points; + int *points = static_cast(alloc(26 * (sizeof(int)))); + for (int i = 0; i < 26; i++) { + points[i] = -1; + } for (Move &move: moves.moves) { int moveX = move.moveX; @@ -254,8 +284,9 @@ void solve(Moves moves, char **board) { } } - printScores(points); printBoard(board, moves.x, moves.y); + printScores(points); + dealloc(points); } /*