Created
April 14, 2013 02:21
-
-
Save hatt/5381098 to your computer and use it in GitHub Desktop.
Google Code Jam 2013 Qualifying Round A
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <string.h> | |
int cmp( char a, char b); | |
int check_corners( uint8_t cur, char board[4][4]); | |
int check_stripes( uint8_t cur, char board[4][4]); | |
int check_ongoing( uint8_t cur, char board[4][4]); | |
int main( int argc, char **argv ) { | |
FILE *file = fopen( argv[1], "r" ); | |
if ( !file ) { | |
printf("Unable to open file: %s\n", argv[1]); | |
return 1; | |
} | |
int boards; | |
fscanf(file, "%1d", &boards); | |
uint8_t a, b, current, result; | |
for ( current = 1; current <= boards; current++ ) { | |
char board[4][4]; | |
for ( uint8_t i = 0; i < 4; i++ ) { | |
fread(board[i], 1, 4, file); // read entire line at once, it's aligned anyway | |
fseek(file, 1, SEEK_CUR); // newline between rows | |
} | |
fseek(file, 1, SEEK_CUR); // newline between boards | |
do { | |
// check if corners match | |
if (! check_corners(current, board) ) { | |
result = 1; | |
} | |
// check if stripes match | |
else if (! check_stripes(current, board) ) { | |
result = 1; | |
} | |
// check if game isn't over yet | |
else if (! check_ongoing(current, board) ) { | |
result = 1; | |
} | |
// oh snap it's a draw | |
else { | |
printf("Case #%d: Draw\n", current); | |
result = 1; | |
} | |
} while (result == 0); | |
} | |
fclose(file); | |
return 0; | |
} | |
int cmp( char a, char b ) { | |
if ( strcmp(&a, ".") || strcmp(&b, ".") ) | |
return 1; | |
if ( strcmp(&a, &b) || strcmp(&a, "T") || strcmp(&b, "T") ) | |
return 0; | |
return 1; | |
} | |
int check_corners( uint8_t cur, char board[4][4] ) { | |
// start from top left | |
char s = board[0][0]; | |
// compare to top right | |
if ( cmp(s, board[0][3]) ) { | |
if ( cmp(s, board[0][1]) && cmp(s, board[0][2]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[0][3]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} | |
// compare to bottom left | |
else if ( cmp(s, board[3][0]) ) { | |
if ( cmp(s, board[1][0]) && cmp(s, board[2][0]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[3][0]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} | |
// compare to bottom right | |
else if ( cmp(s, board[3][3]) ) { | |
if ( cmp(s, board[1][1]) && cmp(s, board[2][2]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[2][3]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} else { | |
// switch to top right | |
s = board[0][3]; | |
// compare to bottom left | |
if ( cmp(s, board[3][0]) ) { | |
if ( cmp(s, board[1][2]) && cmp(s, board[2][1]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[2][3]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} | |
// compare to bottom right | |
else if ( cmp(s, board[3][3]) ) { | |
if ( cmp(s, board[1][3]) && cmp(s, board[2][3]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[2][3]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} | |
else { | |
// switch to bottom left | |
s = board[3][0]; | |
// compare to bottom right | |
if ( cmp(s, board[3][3]) ) { | |
if ( cmp(s, board[3][1]) && cmp(s, board[3][2]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[3][3]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} | |
} | |
} | |
// no matching corners | |
return 1; | |
} | |
int check_stripes( uint8_t cur, char board[4][4] ) { | |
char s = board[0][1]; | |
if ( cmp(s, board[3][1]) ) { | |
if ( cmp(s, board[1][1]) && cmp(s, board[2][1]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[3][1]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} else { | |
s = board[0][2]; | |
if ( cmp(s, board[3][2]) ) { | |
if ( cmp(s, board[1][2]) && cmp(s, board[2][2]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[3][2]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} else { | |
s = board[1][0]; | |
if ( cmp(s, board[1][3]) ) { | |
if ( cmp(s, board[1][1]) && cmp(s, board[1][2]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[1][0]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} else { | |
s = board[2][0]; | |
if ( cmp(s, board[2][3]) ) { | |
if ( cmp(s, board[2][1]) && cmp(s, board[2][2]) ) { | |
if (! strcmp(&s, "T") ) { | |
printf("Case #%d: %s won\n", cur, &board[2][0]); | |
} else { | |
printf("Case #%d: %s won\n", cur, &s); | |
} | |
return 0; | |
} | |
} | |
} | |
} | |
} | |
// no matching strips | |
return 1; | |
} | |
int check_ongoing( uint8_t cur, char board[4][4] ) { | |
for ( uint8_t i = 0; i < 16; i++ ) { | |
if ( strcmp(board[i], ".") ) { | |
printf("Case #%d: Game has not completed\n", cur); | |
return 0; | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment