From 3dd30be057332cd764ee79a26673a775336322eb Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Sun, 15 Jan 2023 10:34:50 +0000 Subject: Puzzle 3: Consolidate 3a/3b programs into `3` with `-p` option Like the later puzzles --- 3a.c | 65 ----------------------------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 3a.c (limited to '3a.c') diff --git a/3a.c b/3a.c deleted file mode 100644 index 676db86..0000000 --- a/3a.c +++ /dev/null @@ -1,65 +0,0 @@ - -#include -#include -#include - - -int -chrcmp(void const * a, void const * b) -{ - return (*(char const *) a) - *((char const *) b); -} - - -int -priority(int c) -{ - if (c >= 'A' && c <= 'Z') - return 27 + c - 'A'; - if (c >= 'a' && c <= 'z') - return 1 + c - 'a'; - fprintf(stderr, "Unexpected priority request: %c\n", c); - return -1; -} - - -int -main() -{ - char buf[BUFSIZ]; - int score = 0; - - while (fgets(buf, sizeof(buf), stdin)) { - int len, i, j; - - len = strlen(buf); - if (len && buf[len - 1] == '\n') - --len; - if (len % 2 == 1) - fprintf(stderr, "Unexpected length: %d\n", len); - - qsort(buf, len / 2, 1, chrcmp); - qsort(buf + len / 2, len / 2, 1, chrcmp); - - i = 0; - j = len / 2; - while (i < len / 2 && j < len) { - if (buf[i] < buf[j]) - ++i; - else if (buf[j] < buf[i]) - ++j; - else - break; - } - if (i >= len / 2 || j >= len) { - fprintf(stderr, "No match found in %s\n", buf); - return -1; - } - score += priority(buf[i]); - } - - printf("Score: %d\n", score); - - return 0; -} - -- cgit v1.2.1