summaryrefslogtreecommitdiff
path: root/2022/day03/main2.c
blob: 17daa38193f29c5712844e0c5b7c7ef4f5db17dd (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 <stdlib.h>
#include <string.h>

char getWrong(char *line, char *line2, char *line3)
{

    for (char *a = line; *a != '\n'; a++)
    {
        for (char *b = line2; *b != '\n'; b++)
        {
            for (char *c = line3; *c != '\n'; c++)
            {
                if ( *a == *b && *a == *c ) return *b;
            }
        }
    }
}

int main()
{
    char *line = NULL;
    char *line2 = NULL;
    char *line3 = NULL;
    size_t len;
    size_t len2;
    size_t len3;
    int prios = 0;
    
    while (getline(&line, &len, stdin) >= 0)
    {
        getline(&line2, &len2, stdin);
        getline(&line3, &len3, stdin);

        char wrong = getWrong(line, line2, line3);
        printf("%c", wrong);
        int prio;
        if ( wrong >= 97 && wrong <= 122 ) prio = wrong - 96;
        else prio = wrong - 64 + 26;
        printf("%d", prio);
        prios += prio;
    }
    printf("\n%d\n", prios);
}