summaryrefslogtreecommitdiff
path: root/10.c
blob: b79b8a719a2016c23959e2ff9bec284833bcfa8a (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
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_STACK 1024
typedef struct
{
    char data[MAX_STACK];
    int sp;
} stack;

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;
}

static char AnyOpen(int c)
{
    switch(c)
    {
        case '(':
            return 1;
        case '[':
            return 1;
        case '{':
            return 1;
        case '<':
            return 1;
        default:
            return 0;
    }
}

static char AnyClose(int c)
{
    switch(c)
    {
        case ')':
            return 1;
        case ']':
            return 1;
        case '}':
            return 1;
        case '>':
            return 1;
        default:
            return 0;
    }
}

static char Close(int fromopen)
{
    switch(fromopen)
    {
        case '(':
            return ')';
        case '[':
            return ']';
        case '{':
            return '}';
        case '<':
            return '>';
        default:
            return 0;
    }
}

static void Push(stack *s, char c)
{
    if (s->sp == MAX_STACK)
    {
        printf("Stack too big!\n");
        exit(1);
    }

    s->data[s->sp++] = c;
}


static char Pop(stack *s)
{
    if (s->sp == 0)
    {
        printf("Stack too small!\n");
        exit(1);
    }

    return s->data[--s->sp];
}


static int SyntaxError(char *p)
{
    char c = 0;
    int score = 0;
    stack s = {0};

    while((c = *p++))
    {
        if (AnyOpen(c))
        {
            Push(&s, c);
        }

        if (AnyClose(c))
        {
            char close = Close(Pop(&s));

            if (close != c)
            {
                if (c == ')')
                {
                    return 3;
                }

                if (c == ']')
                {
                    return 57;
                }

                if (c == '}')
                {
                    return 1197;
                }

                if (c == '>')
                {
                    return 25137;
                }

                printf("Unexpected character %c\n", c);
            }
        }
    }

    return 0;
}


int main(void)
{
    char buff[0x8000];
    int sum = 0;
    int score = 0;
    int f = 0;

    while(ReadLine(buff, sizeof buff, stdin))
    {
        score = SyntaxError(buff);
        sum += score;
    }

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

    return 0;
}