From f30118aff6f9df90324b2350e4774b09109313ad Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sat, 4 Dec 2021 14:35:14 +0100 Subject: [PATCH] cleanup --- 04a.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/04a.c b/04a.c index 638b4fa..5824f07 100644 --- a/04a.c +++ b/04a.c @@ -3,10 +3,6 @@ #include #include -struct board { - int numbers[5][5]; -}; - int parse_draw(int draw[]) { char buf[1000]; @@ -22,14 +18,14 @@ int parse_draw(int draw[]) 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++; @@ -37,13 +33,13 @@ int parse_boards(struct board boards[]) 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; @@ -56,30 +52,30 @@ int main(void) 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]); } -- 2.20.1