aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2022/03/2.c
blob: f91ea46c24ecaf4b879ae88c2131e45d7342d461 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define BUFSZ 1024

char buf[BUFSZ + 1] = {0};

int table[53] = {0};

int decode(char c) {
    if (c >= 'A' && c <= 'Z') {
        return 26 + c - 'A' + 1;
    }
    if (c >='a' && c <= 'z') {
        return c - 'a' + 1;
    }
    return -1;
}

int process(char *buf, int len, int n) {
    if (n == 1) memset(table, 0, 53 * sizeof(int));
    for (int i = 0; i < len; i++) {
        int code = decode(buf[i]);
        if (table[code] == n - 1) {
            if (n == 3) return code; 
            table[code] = n;
        }
    }
    return 0;
}


int main() {
    FILE *fp = fopen("input", "r");
    int sum = 0;
    if (fp == NULL) return -1;
    int line = 0;
    while (fgets(buf, BUFSZ, fp)) {
        if (strlen(buf) <= 1) continue;
        int len = strlen(buf) - 1;
        sum += process(buf, len, line % 3 + 1);
        line++;
    }
    printf("%d\n", sum);
}