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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#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
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;
}
|