inz
adv23d01p1.c (raw)
#include <stdio.h>
#include <stdint.h>
static const struct {
uint64_t match;
uint64_t mask;
int value;
} vals[] = {
{ (uint64_t)'0' << 56, UINT64_MAX << 56, 0 },
{ (uint64_t)'1' << 56, UINT64_MAX << 56, 1 },
{ (uint64_t)'2' << 56, UINT64_MAX << 56, 2 },
{ (uint64_t)'3' << 56, UINT64_MAX << 56, 3 },
{ (uint64_t)'4' << 56, UINT64_MAX << 56, 4 },
{ (uint64_t)'5' << 56, UINT64_MAX << 56, 5 },
{ (uint64_t)'6' << 56, UINT64_MAX << 56, 6 },
{ (uint64_t)'7' << 56, UINT64_MAX << 56, 7 },
{ (uint64_t)'8' << 56, UINT64_MAX << 56, 8 },
{ (uint64_t)'9' << 56, UINT64_MAX << 56, 9 },
{ (uint64_t)'t' << 24 | (uint64_t)'h' << 32 | (uint64_t)'r' << 40 | (uint64_t)'e' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 24, 3 },
{ (uint64_t)'n' << 32 | (uint64_t)'i' << 40 | (uint64_t)'n' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 32, 9 },
{ (uint64_t)'o' << 40 | (uint64_t)'n' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 40, 1 },
{ (uint64_t)'f' << 32 | (uint64_t)'i' << 40 | (uint64_t)'v' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 32, 5 },
{ (uint64_t)'s' << 24 | (uint64_t)'e' << 32 | (uint64_t)'v' << 40 | (uint64_t)'e' << 48 | (uint64_t)'n' << 56, UINT64_MAX << 24, 7 },
{ (uint64_t)'t' << 40 | (uint64_t)'w' << 48 | (uint64_t)'o' << 56, UINT64_MAX << 40, 2 },
{ (uint64_t)'f' << 32 | (uint64_t)'o' << 40 | (uint64_t)'u' << 48 | (uint64_t)'r' << 56, UINT64_MAX << 32, 4 },
{ (uint64_t)'e' << 24 | (uint64_t)'i' << 32 | (uint64_t)'g' << 40 | (uint64_t)'h' << 48 | (uint64_t)'t' << 56, UINT64_MAX << 24, 8 },
{ (uint64_t)'s' << 40 | (uint64_t)'i' << 48 | (uint64_t)'x' << 56, UINT64_MAX << 40, 6 },
};
int findval(uint64_t data) {
size_t l = 0;
size_t h = 1[&vals] - vals;
while (l < h) {
size_t m = (l + h + 1) / 2;
if ((data & vals[m].mask) < vals[m].match)
h = m - 1;
else
l = m;
}
if ((data & vals[l].mask) == vals[l].match)
return vals[l].value;
return -1;
}
int main(int argc, char **argv)
{
int c = 0;
int sum = 0;
int first = -1;
int last = -1;
uint64_t accu = 0;
do {
int cur;
switch (c) {
case '\n':
case EOF:
if (first != -1)
sum += first * 10 + last;
first = -1;
break;
default:
accu = accu >> 8 | (uint64_t)c << 56;
cur = findval(accu);
if (cur != -1) {
if (first == -1)
first = cur;
last = cur;
}
break;
}
} while (c != EOF);
}