aoc23/01/main.c

64 lines
1.7 KiB
C

#include <stdio.h>
#define MAX_LENGTH 52
int convert(char* c) {
switch (*c) {
case 'z':
return (c[1] == 'e' && c[2] == 'r' && c[3] == 'o') ? 0 : -1;
case 'o':
return (c[1] == 'n' && c[2] == 'e') ? 1 : -1;
case 't':
if (c[1] == 'w' && c[2] == 'o')
return 2;
else if (c[1] == 'h' && c[2] == 'r' && c[3] == 'e' && c[4] == 'e')
return 3;
else
return -1;
case 'f':
if (c[1] == 'o' && c[2] == 'u' && c[3] == 'r')
return 4;
else if (c[1] == 'i' && c[2] == 'v' && c[3] == 'e')
return 5;
else
return -1;
case 's':
if (c[1] == 'i' && c[2] == 'x')
return 6;
else if (c[1] == 'e' && c[2] == 'v' && c[3] == 'e' && c[4] == 'n')
return 7;
else
return -1;
case 'e':
return (c[1] == 'i' && c[2] == 'g' && c[3] == 'h' && c[4] == 't') ? 8 : -1;
case 'n':
return (c[1] == 'i' && c[2] == 'n' && c[3] == 'e') ? 9 : -1;
}
return -1;
}
int main(void) {
int num1, num2, sum, word_num;
char buf[MAX_LENGTH];
num1 = 0;
num2 = 0;
sum = 0;
while (fgets(buf, MAX_LENGTH, stdin)) {
for (char* c = buf; *c != '\n'; c++) {
if (*c >= '0' && *c <= '9') {
num1 = num1 == 0 ? *c - '0' : num1;
num2 = *c - '0';
} else if ((word_num = convert(c)) >= 0) {
num1 = num1 == 0 ? word_num : num1;
num2 = word_num;
}
}
sum += num1*10 + num2;
num1 = 0;
}
printf("%d\n", sum);
return 0;
}