aboutsummaryrefslogtreecommitdiff
path: root/06/part1.c
blob: 9554b96ca74dcfefdd12ec20f805e2474b4342da (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
#include <ctype.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>

#include <algds/vec.h>
#include <algds/str.h>

#define PANIC do { \
    fprintf(stderr, "panic at %s:%d\n", __FILE__, __LINE__); \
    abort(); \
} while (0)

void expect_char(char e) {
    int c = fpeek(stdin);
    if (c != e) {
        PANIC;
    }
    fgetc(stdin);
}

void skip_space() {
    while (1) {
        int c = fpeek(stdin);
        if (c == EOF) return;
        if (c == ' ') {
            fgetc(stdin);
        } else {
            return;
        }
    }
}

bool string2long(const char *s, long *num) {
    char *end;
    errno = 0;
    *num = strtol(s, &end, 10);
    if (end == s) {
        return false;
    }
    if (errno != 0) {
        return false;
    }
    return true;
}

int64_t parse_integer() {
    long ret;
    StrBuilder sb;
    StrBuilder_init(&sb);
    while (1) {
        int c = fpeek(stdin);
        if (isdigit(c)) {
            fgetc(stdin);
            StrBuilder_append_char(&sb, c);
        } else {
            break;
        }
    }
    if (strlen(sb.buf) == 0) {
        PANIC;
    }
    StrBuilder_append_char(&sb, '\0');
    if (!string2long(sb.buf, &ret)) {
        PANIC;
    }
    free(sb.buf);
    return ret;
}

IntVector parse_ints() {
    IntVector ints;
    IntVector_init(&ints);
    while (1) {
        int c = fpeek(stdin);
        if (isdigit(c)) {
            int n = parse_integer();
            IntVector_push_back(&ints, n);
            skip_space();
        } else if (c == '\n' || c == EOF) {
            fgetc(stdin);
            return ints;
        } else {
            PANIC;
        }
    }
    return ints;
}

CharVector parse_syms() {
    CharVector syms;
    CharVector_init(&syms);
    while (1) {
        int c = fpeek(stdin);
        if (c == '+' || c == '*') {
            fgetc(stdin);
            CharVector_push_back(&syms, c);
            skip_space();
        } else if (c == '\n' || c == EOF) {
            return syms;
        } else {
            PANIC;
        }
    }
    return syms;
}

VECTOR_DEF(IntVector)
VECTOR_IMPL(IntVector)

int main() {
    IntVectorVector nums;
    CharVector syms;
    IntVectorVector_init(&nums);
    while (1) {
        skip_space();
        int c = fpeek(stdin);
        if (isdigit(c)) {
            IntVector line = parse_ints();
            IntVectorVector_push_back(&nums, line);
        } else {
            syms = parse_syms();
            break;
        }
    }

    int height = IntVectorVector_len(&nums);
    long ret = 0;
    for (int i = 0; i < CharVector_len(&syms); i++) {
        if (syms.buffer[i] == '+') {
            long r = 0;
            for (int j = 0; j < height; j++) {
                r += nums.buffer[j].buffer[i];
            }
            ret += r;
        } else if (syms.buffer[i] == '*') {
            long r = 1;
            for (int j = 0; j < height; j++) {
                r *= nums.buffer[j].buffer[i];
            }
            ret += r;
        } else {
            PANIC;
        }
    }
    printf("%ld\n", ret);

    for (int j = 0; j < height; j++) {
        IntVector_free(nums.buffer + j);
    }
    IntVectorVector_free(&nums);
    CharVector_free(&syms);
    return 0;
}