|
| 1 | +#include <stdint.h> |
| 2 | +#ifdef TEST |
| 3 | +#include <stdio.h> |
| 4 | +#endif |
| 5 | + |
| 6 | +//#pragma bank 3 |
| 7 | +#include "encoded.h" |
| 8 | + |
| 9 | +uint8_t decodeInt(const uint8_t* blob, uint32_t* valueP) { |
| 10 | + if (blob[0] & 0x80 ) { |
| 11 | + *valueP = blob[0] & 0x7F; |
| 12 | + return 1; |
| 13 | + } |
| 14 | + else if (blob[1] & 0x80) { |
| 15 | + *valueP = blob[0] | ((uint32_t)(blob[1]&0x7F)<<7); |
| 16 | + return 2; |
| 17 | + } |
| 18 | + else /* if (blob[2] & 0x80) */ { |
| 19 | + *valueP = blob[0] | ((uint32_t)(blob[1])<<7) | ((uint32_t)(blob[2]&0x7F)<<14); |
| 20 | + return 3; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +void decodeWord(uint8_t start, uint32_t nextFour, char* buffer) { |
| 25 | + buffer[0] = start + 'A'; |
| 26 | + buffer[1] = ((uint8_t)(nextFour >> 15) & 0x1F) + 'A'; |
| 27 | + buffer[2] = ((uint8_t)(nextFour >> 10) & 0x1F) + 'A'; |
| 28 | + buffer[3] = ((uint8_t)(nextFour >> 5) & 0x1F) + 'A'; |
| 29 | + buffer[4] = ((uint8_t)(nextFour) & 0x1F) + 'A'; |
| 30 | + buffer[5] = 0; |
| 31 | +} |
| 32 | + |
| 33 | +void getWord(uint16_t n, char* buffer) { |
| 34 | + uint16_t count = 0; |
| 35 | + uint8_t i; |
| 36 | + for (i = 0 ; i < 26 && n >= words[i+1].wordNumber ; i++) ; |
| 37 | + if (i == 26) { |
| 38 | + *buffer = 0; |
| 39 | + return; |
| 40 | + } |
| 41 | + n -= words[i].wordNumber; |
| 42 | + uint32_t word = 0; |
| 43 | + const uint8_t* blob = wordBlob + words[i].blobOffset; |
| 44 | + uint16_t j; |
| 45 | + for (j=0; j<=n; j++) { |
| 46 | + uint32_t delta; |
| 47 | + blob += decodeInt(blob, &delta); |
| 48 | + word += delta + 1; |
| 49 | + } |
| 50 | + decodeWord(i, word, buffer); |
| 51 | +} |
| 52 | + |
| 53 | +uint8_t filterWord(char* s) { |
| 54 | + for (int i=0; i<5; i++) |
| 55 | + if (s[i] < 'A' || s[i] > 'Z') |
| 56 | + return 0; |
| 57 | + uint8_t i = s[0]-'A'; |
| 58 | + uint32_t w = ((uint32_t)(s[1]-'A') << 15) | ((uint32_t)(s[2]-'A') << 10) | ((uint32_t)(s[3]-'A') << 5) | (uint32_t)(s[4]-'A'); |
| 59 | + uint16_t n = words[i+1].wordNumber - words[i].wordNumber; |
| 60 | + uint32_t match = 0; |
| 61 | + const uint8_t* b = wordBlob + words[i].blobOffset; |
| 62 | + for (uint16_t j=0; j<n; j++) { |
| 63 | + uint32_t delta; |
| 64 | + b += decodeInt(b, &delta); |
| 65 | + match += delta + 1; |
| 66 | + if (match > w) |
| 67 | + return 0; |
| 68 | + else if (match == w) |
| 69 | + return 1; |
| 70 | + } |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +void getSpecialWord(int16_t n, char* buffer) { |
| 75 | + uint16_t w = 0; |
| 76 | + const uint8_t* b = answers; |
| 77 | + while(n>=0) { |
| 78 | + uint8_t c = *b; |
| 79 | + if (c == 0) { |
| 80 | + w += 8; |
| 81 | + } |
| 82 | + else { |
| 83 | + for (uint8_t i = 0 ; i < 8 ; i++) { |
| 84 | + if (c & 1) { |
| 85 | + n--; |
| 86 | + if (n<0) |
| 87 | + break; |
| 88 | + } |
| 89 | + w++; |
| 90 | + c >>= 1; |
| 91 | + } |
| 92 | + } |
| 93 | + b++; |
| 94 | + } |
| 95 | + getWord(w, buffer); |
| 96 | +} |
| 97 | + |
| 98 | +#ifdef TEST |
| 99 | +main() { |
| 100 | + char w[6] = {0}; |
| 101 | + for (int i=0; i<100; i++) { |
| 102 | + getSpecialWord(i, w); |
| 103 | + puts(w); |
| 104 | + } |
| 105 | + printf("%d\n", filterWord("SEREZ")); |
| 106 | + printf("%d\n", filterWord("SERER")); |
| 107 | + printf("%d\n", filterWord("ZYMIC")); |
| 108 | + printf("%d\n", filterWord("BAAED")); |
| 109 | +} |
| 110 | +#endif |
0 commit comments