94 lines
2.0 KiB
C
94 lines
2.0 KiB
C
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#define BUFFER_SIZE 64
|
||
|
|
#define GROUP_SIZE 3
|
||
|
|
|
||
|
|
#define score(ch) (ch < 'a' ? ch - 'A' + 27 : ch - 'a' + 1)
|
||
|
|
|
||
|
|
char firstMatch(char*, char*, int);
|
||
|
|
void cp(char*, char*);
|
||
|
|
char badge(char**, char*);
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
char buf[BUFFER_SIZE], *p, *q, c;
|
||
|
|
char *prev[GROUP_SIZE - 1];
|
||
|
|
for (int i = 0; i < GROUP_SIZE - 1; i++) {
|
||
|
|
prev[i] = (char*)malloc(BUFFER_SIZE);
|
||
|
|
}
|
||
|
|
int l = 0, sum = 0, sum2 = 0, gruppen = 0;
|
||
|
|
p = buf;
|
||
|
|
while ((c = getchar()) != EOF) {
|
||
|
|
*p++ = c;
|
||
|
|
l++;
|
||
|
|
if (c == '\n') {
|
||
|
|
p = buf;
|
||
|
|
q = p + l/2;
|
||
|
|
|
||
|
|
c = firstMatch(p, q, l/2);
|
||
|
|
sum += score(c);
|
||
|
|
|
||
|
|
if (gruppen != GROUP_SIZE - 1) {
|
||
|
|
cp(p, prev[gruppen]);
|
||
|
|
} else {
|
||
|
|
c = badge(prev, p);
|
||
|
|
sum2 += score(c);
|
||
|
|
}
|
||
|
|
|
||
|
|
gruppen == GROUP_SIZE - 1 ? gruppen = 0 : gruppen++;
|
||
|
|
|
||
|
|
memset(buf, 0, BUFFER_SIZE);
|
||
|
|
p = buf;
|
||
|
|
l = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < GROUP_SIZE - 1; i++) {
|
||
|
|
free(prev[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
printf("%i\n", sum);
|
||
|
|
printf("%i\n", sum2);
|
||
|
|
}
|
||
|
|
|
||
|
|
char firstMatch(char *p, char *q, int l) {
|
||
|
|
for (int i = 0; i < l; i++) {
|
||
|
|
for (int j = 0; j < l; j++) {
|
||
|
|
if (p[i] == q[j]) return p[i];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
void cp(char* p, char* q) {
|
||
|
|
while((*q++ = *p++) != '\n') {}
|
||
|
|
}
|
||
|
|
|
||
|
|
char badge(char** prev, char* p) {
|
||
|
|
// This could be done more efficiently
|
||
|
|
int match;
|
||
|
|
char *q;
|
||
|
|
p--;
|
||
|
|
while(*(++p) != '\n') {
|
||
|
|
for (int i = 0; i < GROUP_SIZE - 1; i++) {
|
||
|
|
// Iterate every string to find a match
|
||
|
|
match = 0;
|
||
|
|
q = prev[i] - 1;
|
||
|
|
while(*(++q) != '\n') {
|
||
|
|
if (*q == *p) {
|
||
|
|
match = 1;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!match) {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (match) {
|
||
|
|
return *p;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|