Name:
Cudder
!cXCudderUE
2016-11-07 11:36
Write a function that fills a 64-byte array with the following sequence:
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63
...but the challenge is that it must be less than 64 bytes of machine instructions in an existing architecture of your choice.
Name:
Anonymous
2016-11-07 16:44
#include <stdio.h>
int ones[64];
int sevens[64];
int seq[64];
void cudder() {
int onesaccum = 0;
int nextones = 1;
int sevensaccum = 0;
int nextsevens = 0;
for (int i = 0; i < 32; i++) {
if (++onesaccum == nextones) {
ones[i]++;
nextones++;
onesaccum = 0;
}
if (sevensaccum > 0)
sevens[i]++;
else if (sevensaccum < 0)
sevens[i]--;
if (sevensaccum == 0)
nextsevens += 2;
if (sevensaccum++ == nextsevens)
sevensaccum = -nextsevens;
}
seq[0] = 0;
seq[63-0] = 63-seq[0];
for (int i = 1; i < 32; i++) {
seq[i] = seq[i-1] + ones[i-1] * 1 + sevens[i-1] * 7;
seq[63-i] = 63 - seq[i];
}
}
int main (int argc, char *argv[]) {
cudder();
printf("seq:\n");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
printf("%d, ", seq[i*8 + j]);
}
printf("\n");
}
printf("ones:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
printf("%d, ", ones[i*8 + j]);
}
printf("\n");
}
printf("sevens:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
printf("%d, ", sevens[i*8 + j]);
}
printf("\n");
}
}
Output:
seq:
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63,
ones:
1, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0,
sevens:
0, 1, 1, -1, -1, 0, 1, 1,
1, 1, -1, -1, -1, -1, 0, 1,
1, 1, 1, 1, 1, -1, -1, -1,
-1, -1, -1, 0, 1, 1, 1, 1,