From 6596b48113ced669b206d2e1f1e8ba6edcba88a2 Mon Sep 17 00:00:00 2001 From: Orangerot Date: Sat, 17 Dec 2022 01:16:18 +0100 Subject: adv2022 first 8 days --- 2022/day03/main.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2022/day03/main.c (limited to '2022/day03/main.c') diff --git a/2022/day03/main.c b/2022/day03/main.c new file mode 100644 index 0000000..69068fa --- /dev/null +++ b/2022/day03/main.c @@ -0,0 +1,36 @@ +#include +#include +#include + +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); +} -- cgit v1.2.3