summaryrefslogtreecommitdiff
path: root/4a.c
blob: 0c2a3e5ee44fea1e00832b99821dd95c92a01a43 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#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[MAX_BOARDS] = {0};
    int winner_count = 1;
    int last_board = 0;
    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_count != board_count + 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] = -1;
                    }
                }
            }
        }

        for(f = 0; f < board_count; f++)
        {
            for(y = 0; y < 5; y++)
            {
                if (board[f][y][0] == -1 &&
                    board[f][y][1] == -1 &&
                    board[f][y][2] == -1 &&
                    board[f][y][3] == -1 &&
                    board[f][y][4] == -1)
                {
                    if (winner[f] == 0)
                    {
                        winner[f] = winner_count++;
                    }
                }
            }

            for(x = 0; x < 5; x++)
            {
                if (board[f][0][x] == -1 &&
                    board[f][1][x] == -1 &&
                    board[f][2][x] == -1 &&
                    board[f][3][x] == -1 &&
                    board[f][4][x] == -1)
                {
                    if (winner[f] == 0)
                    {
                        winner[f] = winner_count++;
                    }
                }
            }
        }

        winner_num = num[i];
    }

    printf("i/num_count = %d/%d\n", i, num_count);

    for(f = 0; f < board_count; f++)
    {
        if (winner[f] == board_count)
        {
            last_board = f;
        }
    }

    printf("board_count = %d\n", board_count);
    printf("winner_num = %d\n", winner_num);
    printf("last_board = %d\n", f);

    sum = 0;

    for(y = 0; y < 5; y++)
    {
        for(x = 0; x < 5; x++)
        {
            if (board[last_board][y][x] != -1)
            {
                sum += board[last_board][y][x];
            }
        }
    }

    printf("sum = %d\n", sum);
    printf("product = %d\n", sum * winner_num);

    return 0;
}