aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/01/part2.c
blob: 13aba12e26c06a4d5fe2166503c830cf1fcd8104 (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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

#include "str.h"

char *numbers[] = {
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine"
};

int find_number(const char *line) {
    int matched = 0;
    if (isdigit(*line)) return *line - '0';
    for (int i = 0; i < 10; i++) {
        matched = 1;
        const char *p1 = numbers[i], *p2 = line;
        while (*p1 != '\0' && *p2 != '\0') {
            if (*p1 != *p2) {
                matched = 0;
                break;
            }
            p1++;
            p2++;
        }
        if (matched && *p1 == '\0') return i;
    }
    return -1;
}

int main() {
    FILE* fp = fopen("./input", "r");
    int sum = 0;
    while (1) {
        char *line = str_strip(fgetline(fp));
        if (line == NULL || strlen(line) == 0) {
            break;
        }
        int d1 = -1, d2 = -1;
        while (*line != '\0') {
            int num = -1;
            if ((num = find_number(line)) >= 0) {
                if (d1 == -1) d1 = num;
                d2 = num;
            }
            line++;
        }
        if (d2 == -1) d2 = d1;
        sum += d1 * 10 + d2;
    }
    printf("%d\n", sum);
    return 0;
}