summaryrefslogtreecommitdiff
path: root/3b.c
diff options
context:
space:
mode:
Diffstat (limited to '3b.c')
-rw-r--r--3b.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/3b.c b/3b.c
new file mode 100644
index 0000000..84455e1
--- /dev/null
+++ b/3b.c
@@ -0,0 +1,87 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+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
+min(int a, int b)
+{
+ return a < b ? a : b;
+}
+
+
+int
+min3(int a, int b, int c)
+{
+ return min(min(a, b), c);
+}
+
+
+int
+main()
+{
+ char buf[3][BUFSIZ];
+ int score = 0;
+
+ while (fgets(buf[0], sizeof(buf[0]), stdin)
+ && fgets(buf[1], sizeof(buf[1]), stdin)
+ && fgets(buf[2], sizeof(buf[2]), stdin))
+ {
+ int n, i, j, k;
+
+ for (n = 0; n < 3; ++n) {
+ int len = strlen(buf[n]);
+ if (len && buf[n][len - 1] == '\n') {
+ --len;
+ buf[n][len] = '\0';
+ }
+ if (len % 2 == 1)
+ fprintf(stderr, "Unexpected length: %d\n", len);
+
+ qsort(buf[n], len, 1, chrcmp);
+ }
+
+ i = 0; j = 0; k = 0;
+ while (buf[0][i] != '\0' && buf[1][j] != '\0' && buf[2][k] != '\0') {
+ int n = min3(buf[0][i], buf[1][j], buf[2][k]);
+ if (buf[0][i] == n && buf[1][j] == n && buf[2][k] == n)
+ break;
+ if (buf[0][i] == n)
+ ++i;
+ if (buf[1][j] == n)
+ ++j;
+ if (buf[2][k] == n)
+ ++k;
+ }
+ if (buf[0][i] == '\0' || buf[1][j] == '\0' || buf[2][k] == '\0') {
+ fprintf(stderr, "No match found in %s/%s/%s\n", buf[0], buf[1], buf[2]);
+ return -1;
+ }
+ score += priority(buf[0][i]);
+ }
+
+ printf("Score: %d\n", score);
+
+ return 0;
+}
+