diff options
| author | Ian C <ianc@noddybox.co.uk> | 2021-12-09 13:32:48 +0000 | 
|---|---|---|
| committer | Ian C <ianc@noddybox.co.uk> | 2021-12-09 13:32:48 +0000 | 
| commit | 8833b7a5b246cad02e253838c07ba1689834e523 (patch) | |
| tree | de8ae5cbb97a802a7d2ce30e7db37798ac02be00 /4a.c | |
Initial checkin
Diffstat (limited to '4a.c')
| -rw-r--r-- | 4a.c | 132 | 
1 files changed, 132 insertions, 0 deletions
| @@ -0,0 +1,132 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define MAX_BOARDS 100 + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ +    if ((p=fgets(p, size, fp))) +    { +        size_t l = strlen(p); + +        while(l && p[l-1] == '\n') +        { +            p[--l] = 0; +        } +    } + +    return p; +} + +int main(void) +{ +    char buff[1024]; +    int *num = NULL; +    int num_count = 0; +    int board[MAX_BOARDS][5][5] = {0}; +    int board_count = 0; +    char *p = NULL; +    int f = 0; +    int x = 0; +    int y = 0; +    int i = 0; +    int winner_board = -1; +    int winner_num = -1; +    int sum = 0; + +    ReadLine(buff, sizeof buff, stdin); + +    p = strtok(buff, ","); + +    while(p) +    { +        num = realloc(num, (num_count + 1) * sizeof *num); +        num[num_count++] = atoi(p); +        p = strtok(NULL, ","); +    } + +    while (ReadLine(buff, sizeof buff, stdin)) +    { +        if (board_count == MAX_BOARDS) +        { +            printf("Too many boards\n"); +            return 1; +        } + +        for(y = 0; y < 5; y++) +        { +            ReadLine(buff, sizeof buff, stdin); + +            for(x = 0; x < 5; x++) +            { +                p = strtok(x == 0 ? buff : NULL, " "); +                board[board_count][y][x] = atoi(p); +            } +        } + +        board_count++; +    } + +    for(i = 0; i < num_count && winner_board == -1; i++) +    { +        for(f = 0; f < board_count; f++) +        { +            for(y = 0; y < 5; y++) +            { +                for(x = 0; x < 5; x++) +                { +                    if (board[f][y][x] == num[i]) +                    { +                        board[f][y][x] = 0; +                    } +                } +            } +        } + +        for(f = 0; f < board_count && winner_board == -1; f++) +        { +            for(y = 0; y < 5; y++) +            { +                if (board[f][y][0] == 0 && +                    board[f][y][1] == 0 && +                    board[f][y][2] == 0 && +                    board[f][y][3] == 0 && +                    board[f][y][4] == 0) +                { +                    winner_board = f; +                    winner_num = num[i]; +                } +            } + +            for(x = 0; x < 5; x++) +            { +                if (board[f][0][x] == 0 && +                    board[f][1][x] == 0 && +                    board[f][2][x] == 0 && +                    board[f][3][x] == 0 && +                    board[f][4][x] == 0) +                { +                    winner_board = f; +                    winner_num = num[i]; +                } +            } +        } +    } + +    printf("winner_board = %d\n", winner_board); +    printf("winner_num = %d\n", winner_num); + +    for(y = 0; y < 5; y++) +    { +        for(x = 0; x < 5; x++) +        { +            sum += board[winner_board][y][x]; +        } +    } + +    printf("sum = %d\n", sum); +    printf("product = %d\n", sum * winner_num); + +    return 0; +} | 
