aboutsummaryrefslogtreecommitdiff
path: root/03/part1.c
blob: 5e8151bc7ee4ab2b31ff0316d9c8bca9d956c797 (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
#include <stdio.h>
#include <string.h>

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


void getlines(StringVector *lines) {
    while (1) {
        char *line = fgetline(stdin);
        if (line == NULL) {
            break;
        }
        char *nline = str_strip(line);
        free(line);
        StringVector_push_back(lines, nline);
    }
}

int find_max(const char *line) {
    int len = strlen(line);
    int max = 0;
    for (int i = 0; i < len - 1; i++) {
        for (int j = i + 1; j < len; j++) {
            int n = (line[i] - '0') * 10 + line[j] - '0';
            if (n > max) {
                max = n;
            }
        }
    }
    return max;
}

int main() {
    StringVector lines;
    StringVector_init(&lines);
    getlines(&lines);
    String *it = StringVector_begin(&lines);
    int sum = 0;
    for (; it != StringVector_end(&lines); it++) {
        sum += find_max(*it);
    }
    printf("%d\n", sum);
    return 0;
}