cleanup
authorMart Lubbers <mart@martlubbers.net>
Sat, 4 Dec 2021 13:35:14 +0000 (14:35 +0100)
committerMart Lubbers <mart@martlubbers.net>
Sat, 4 Dec 2021 13:35:14 +0000 (14:35 +0100)
04a.c

diff --git a/04a.c b/04a.c
index 638b4fa..5824f07 100644 (file)
--- a/04a.c
+++ b/04a.c
@@ -3,10 +3,6 @@
 #include <stdlib.h>
 #include <stdbool.h>
 
-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]);
 }