summaryrefslogtreecommitdiff
path: root/4.c
blob: 48512487ee644c36527217355e0a1d596af4b5c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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;
}