advent-of-code/2022/day03/main.c
2022-12-17 01:16:18 +01:00

37 lines
734 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char getWrong(char *line)
{
char *middle = line + strlen(line)/2;
char *a = line;
char *b = middle;
for (char *a = line; a < middle; a++)
{
for (char *b = middle; *b != '\n'; b++)
{
if ( *a == *b ) return *b;
}
}
}
int main()
{
char *line = NULL;
size_t len;
int prios = 0;
while (getline(&line, &len, stdin) >= 0)
{
char wrong = getWrong(line);
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);
}