Skip to content

Instantly share code, notes, and snippets.

@mrjohannchang
Created March 3, 2013 14:52
Show Gist options
  • Save mrjohannchang/5076388 to your computer and use it in GitHub Desktop.
Save mrjohannchang/5076388 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#define BUF_SIZE (1<<16)
#define LIST_LEN (1024*1024*38)
#define WORD_LEN (16)
#define TIMES (1000*9900)
char buf[BUF_SIZE];
char* p_buf = buf;
void static inline flush() {
write(1, buf, p_buf-buf);
p_buf = buf;
}
void static inline print(char* s) {
while (*s!='\0') *(p_buf++) = *(s++);
if (p_buf-buf>BUF_SIZE-100) flush();
}
int main(int argc, const char *argv[])
{
struct timeval start;
struct timeval now;
gettimeofday(&start, NULL);
char rot[] = {'0', '1', '6', '8', '9'};
char rrot[] = {'0', '1', '9', '8', '6'};
char* sym[] = {"0", "1", "8"};
int idx[] = {4, 0};
char (*list)[2][LIST_LEN][WORD_LEN] = malloc(4L*LIST_LEN*WORD_LEN);
strcpy(list[0][0][0], "1");
strcpy(list[0][0][1], "6");
strcpy(list[0][0][2], "8");
strcpy(list[0][0][3], "9");
strcpy(list[0][1][0], "1\n");
strcpy(list[0][1][1], "9\n");
strcpy(list[0][1][2], "8\n");
strcpy(list[0][1][3], "6\n");
print("0\n1\n8\n");
int b = 0;
for (int i=0;i<idx[b];i++) {
print(list[b][0][i]);
print(list[b][1][i]);
}
for (int i=0;i<idx[b];i++) {
for (int j=0;j<3;j++) {
print(list[b][0][i]);
print(sym[j]);
print(list[b][1][i]);
}
}
int timesup = 0;
for (int l=2;l<20;l++) {
if (timesup) break;
int nb = b^1;
int idxb = idx[b];
int idxnb = 0;
for (int i=0;i<idxb;i++) {
for (int j=0;j<5;j++) {
strcpy(list[nb][0][idxnb], list[b][0][i]);
list[nb][0][idxnb][l-1] = rot[j];
list[nb][1][idxnb][0] = rrot[j];
strcpy(list[nb][1][idxnb]+1, list[b][1][i]);
print(list[nb][0][idxnb]);
print(list[nb][1][idxnb]);
idxnb++;
}
if ((i+1)%10000==0) {
gettimeofday(&now, NULL);
if ((now.tv_sec-start.tv_sec)*1000000+(now.tv_usec-start.tv_usec)>TIMES) {
timesup = 1;
break;
}
}
}
if (timesup) break;
for (int i=0;i<idxnb;i++) {
for (int j=0;j<3;j++) {
print(list[nb][0][i]);
print(sym[j]);
print(list[nb][1][i]);
}
if ((i+1)%10000==0) {
gettimeofday(&now, NULL);
if ((now.tv_sec-start.tv_sec)*1000000+(now.tv_usec-start.tv_usec)>TIMES) {
timesup = 1;
break;
}
}
}
idx[nb] = idxnb;
b ^= 1;
}
flush();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment