#include <stdlib.h>
#include <stdbool.h>
-struct board {
- int numbers[5][5];
-};
-
int parse_draw(int draw[])
{
char buf[1000];
return i;
}
-int parse_boards(struct board boards[])
+int parse_boards(int boards[][5][5])
{
int i = 0;
while(getchar() == '\n') {
for (int row = 0; row<5; row++) {
for (int col = 0; col<5; col++) {
char buf[3] = {getchar(), getchar(), getchar()};
- boards[i].numbers[row][col] = atoi(buf);
+ boards[i][row][col] = atoi(buf);
}
}
i++;
return i;
}
-bool board_wins(struct board board)
+bool board_wins(int board[5][5])
{
for (int row = 0; row<5; row++) {
bool rowwin = true, colwin = true;
for (int col = 0; col<5 && (rowwin || colwin); col++) {
- rowwin &= board.numbers[row][col] == -1;
- colwin &= board.numbers[col][row] == -1;
+ rowwin &= board[row][col] == -1;
+ colwin &= board[col][row] == -1;
}
if (rowwin || colwin)
return true;
int draws[1000];
int ndraws = parse_draw(draws);
- struct board boards[1000];
+ int boards[1000][5][5];
int nboards = parse_boards(boards);
- struct board *winner = NULL;
+ int winner = -1;
int draw;
- for (draw = 0; draw < ndraws && winner == NULL; draw++) {
+ for (draw = 0; draw < ndraws && winner == -1; draw++) {
//Mark boards
for (int board = 0; board < nboards; board++)
for (int row = 0; row<5; row++)
for (int col = 0; col<5; col++)
- if (boards[board].numbers[row][col] == draws[draw])
- boards[board].numbers[row][col] = -1;
+ if (boards[board][row][col] == draws[draw])
+ boards[board][row][col] = -1;
//Check winners
- for (int board = 0; board < nboards && winner == NULL; board++)
+ for (int board = 0; board < nboards && winner == -1; board++)
if (board_wins(boards[board]))
- winner = &boards[board];
+ winner = board;
}
//Calculate sum of unmarked numbers
int sum = 0;
for (int row = 0; row<5; row++)
for (int col = 0; col<5; col++)
- if (winner->numbers[row][col] != -1)
- sum += winner->numbers[row][col];
+ if (boards[winner][row][col] != -1)
+ sum += boards[winner][row][col];
printf("%d\n", sum*draws[draw-1]);
}