aboutsummaryrefslogtreecommitdiff
path: root/01/part2.c
blob: 211b1bbe3dbc95aabfab6a948bcfd0e4a9fec065 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char buf[1024];

char * readline() {
    int i = 0;
    int c;
    while (1) {
        c = fgetc(stdin);
        if (c == EOF) {
            if (i == 0) {
                return NULL;
            }
            goto end;
        }
        if (c == '\n') {
            goto end;
        }
        buf[i] = c;
        i++;
    }
end:
    buf[i] = '\0';
    return strdup(buf);
}

int main() {
    int dial = 50;
    char *line;
    int count = 0;
    while ((line = readline()) != NULL) {
        int negative = 0;
        if (line[0] == 'L') negative = 1;
        char *end;
        long val = strtol(line+1, &end, 10);
        if (val >= 100) {
            count += val / 100;
            val = val % 100;
        }
        if (negative) val = -val;
        if (end == line+1) continue;
        int olddial = dial;
        dial = dial + val;
        if (dial >= 100) {
            count++;
        }
        if (olddial > 0 && dial <= 0) {
            count++;
        }
        dial = dial % 100;
        if (dial < 0) dial += 100;
    }
    printf("%d\n", count);
    return 0;
}