inz

adv23d01p1.c (raw)

  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. static const struct {
  5. uint64_t match;
  6. uint64_t mask;
  7. int value;
  8. } vals[] = {
  9. { (uint64_t)'0' << 56, UINT64_MAX << 56, 0 },
  10. { (uint64_t)'1' << 56, UINT64_MAX << 56, 1 },
  11. { (uint64_t)'2' << 56, UINT64_MAX << 56, 2 },
  12. { (uint64_t)'3' << 56, UINT64_MAX << 56, 3 },
  13. { (uint64_t)'4' << 56, UINT64_MAX << 56, 4 },
  14. { (uint64_t)'5' << 56, UINT64_MAX << 56, 5 },
  15. { (uint64_t)'6' << 56, UINT64_MAX << 56, 6 },
  16. { (uint64_t)'7' << 56, UINT64_MAX << 56, 7 },
  17. { (uint64_t)'8' << 56, UINT64_MAX << 56, 8 },
  18. { (uint64_t)'9' << 56, UINT64_MAX << 56, 9 },
  19. { (uint64_t)'t' << 24 | (uint64_t)'h' << 32 | (uint64_t)'r' << 40 | (uint64_t)'e' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 24, 3 },
  20. { (uint64_t)'n' << 32 | (uint64_t)'i' << 40 | (uint64_t)'n' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 32, 9 },
  21. { (uint64_t)'o' << 40 | (uint64_t)'n' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 40, 1 },
  22. { (uint64_t)'f' << 32 | (uint64_t)'i' << 40 | (uint64_t)'v' << 48 | (uint64_t)'e' << 56, UINT64_MAX << 32, 5 },
  23. { (uint64_t)'s' << 24 | (uint64_t)'e' << 32 | (uint64_t)'v' << 40 | (uint64_t)'e' << 48 | (uint64_t)'n' << 56, UINT64_MAX << 24, 7 },
  24. { (uint64_t)'t' << 40 | (uint64_t)'w' << 48 | (uint64_t)'o' << 56, UINT64_MAX << 40, 2 },
  25. { (uint64_t)'f' << 32 | (uint64_t)'o' << 40 | (uint64_t)'u' << 48 | (uint64_t)'r' << 56, UINT64_MAX << 32, 4 },
  26. { (uint64_t)'e' << 24 | (uint64_t)'i' << 32 | (uint64_t)'g' << 40 | (uint64_t)'h' << 48 | (uint64_t)'t' << 56, UINT64_MAX << 24, 8 },
  27. { (uint64_t)'s' << 40 | (uint64_t)'i' << 48 | (uint64_t)'x' << 56, UINT64_MAX << 40, 6 },
  28. };
  29.  
  30. int findval(uint64_t data) {
  31. size_t l = 0;
  32. size_t h = 1[&vals] - vals;
  33.  
  34. while (l < h) {
  35. size_t m = (l + h + 1) / 2;
  36. if ((data & vals[m].mask) < vals[m].match)
  37. h = m - 1;
  38. else
  39. l = m;
  40. }
  41.  
  42. if ((data & vals[l].mask) == vals[l].match)
  43. return vals[l].value;
  44.  
  45. return -1;
  46. }
  47.  
  48. int main(int argc, char **argv)
  49. {
  50. int c = 0;
  51. int sum = 0;
  52. int first = -1;
  53. int last = -1;
  54. uint64_t accu = 0;
  55.  
  56. do {
  57. int cur;
  58. c = fgetc(stdin);
  59.  
  60. switch (c) {
  61. case '\n':
  62. case EOF:
  63. if (first != -1)
  64. sum += first * 10 + last;
  65. first = -1;
  66. break;
  67.  
  68. default:
  69. accu = accu >> 8 | (uint64_t)c << 56;
  70.  
  71. cur = findval(accu);
  72.  
  73. if (cur != -1) {
  74. if (first == -1)
  75. first = cur;
  76. last = cur;
  77. }
  78. break;
  79. }
  80. } while (c != EOF);
  81.  
  82. printf("%d\n", sum);
  83. }
  84.