Created
February 21, 2022 14:51
-
-
Save arpruss/977a0e30f73a8f58732bc41a0da5264a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void getSpecialWord(uint16_t _n, char* buffer) { | |
static uint16_t w; | |
w = 0; | |
const uint8_t* b = answers; | |
static uint16_t n; | |
n = _n + 1; | |
static uint8_t additionTable[256]; | |
static uint8_t tableReady = 0; | |
static uint8_t c; | |
static uint8_t mask; | |
if (!tableReady) { | |
for (c=255;c;c--) { | |
static uint8_t count; | |
count = 0; | |
for (mask = 1 ; mask ; mask <<= 1) | |
if (c & mask) | |
count++; | |
additionTable[c] = count; | |
} | |
additionTable[0] = 0; | |
tableReady = 1; | |
} | |
for(;;) { | |
c = *b++; | |
uint8_t delta = additionTable[c]; | |
if (n > delta) { | |
n -= delta; | |
w += 8; | |
} | |
else { | |
for (mask = 1 ; mask ; mask <<= 1) { | |
if (c & mask) { | |
n--; | |
if (n == 0) { | |
getWord(w, buffer); | |
return; | |
} | |
} | |
w++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment