Last active
August 29, 2015 13:59
-
-
Save calebrob6/10676574 to your computer and use it in GitHub Desktop.
Does stuff
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
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include <malloc.h> | |
int main(int argc, char* argv[]) | |
{ | |
char *name = "enwik8.zip"; | |
FILE *file; | |
unsigned char *buffer; | |
unsigned long fileLen; | |
file=fopen(name,"rb"); | |
if(!file){ | |
fprintf(stderr,"Unable to open file %s",name); | |
return 0; | |
} | |
fseek(file,0,SEEK_END); | |
fileLen=ftell(file); | |
fseek(file,0,SEEK_SET); | |
buffer=(unsigned char *)malloc(fileLen); | |
if(!buffer){ | |
fprintf(stderr, "Memory error!"); | |
fclose(file); | |
return 0; | |
} | |
fread(buffer,fileLen,1,file); | |
fclose(file); | |
printf("Length of %s is %d\n",name,fileLen); | |
printf("sizeof(char) is %d\n",sizeof(char)); | |
printf("sizeof(buffer) is %d\n",sizeof(buffer)); | |
printf("Number of 1's in file is: %d\n",371868110); | |
srand((time(NULL) & 0xFFFF)); | |
int r = rand(); | |
long n = fileLen*8; | |
long randomFill = n/20; | |
int numIterations = 10000000; | |
unsigned char* board = (unsigned char*) malloc(n); | |
unsigned char* board2 = (unsigned char*) malloc(n); | |
if(!board) printf("Something happened...\n"); | |
long i,j; | |
//clear board | |
printf("Clearing the board...\n"); | |
for(i=0;i<n;i++){ | |
board[i]=0; | |
} | |
printf("Random fill...\n"); | |
for(i=0;i<randomFill;i++){ | |
board[i] = rand()%2; | |
board2[i] = board[i]; | |
} | |
/* | |
printf("Smart fill...\n"); | |
for(i=0;i<fileLen;i++){ | |
board[i*8 ] = (buffer[i] & 0X80) >> 7; | |
board[i*8+1] = (buffer[i] & 0X40) >> 6; | |
board[i*8+2] = (buffer[i] & 0X20) >> 5; | |
board[i*8+3] = (buffer[i] & 0X10) >> 4; | |
board[i*8+4] = (buffer[i] & 0X08) >> 3; | |
board[i*8+5] = (buffer[i] & 0X04) >> 2; | |
board[i*8+6] = (buffer[i] & 0X02) >> 1; | |
board[i*8+7] = (buffer[i] & 0X01) >> 0; | |
//Check for sameness | |
//printf("%X\n",buffer[i]); | |
//printf("%X %X %X %X %X %X %X %X\n",board[i*8],board[i*8+1],board[i*8+2],board[i*8+3],board[i*8+4],board[i*8+5],board[i*8+6],board[i*8+7]); | |
} | |
*/ | |
unsigned char firstVal = 0; | |
unsigned char endVal = 0; | |
unsigned char prevVal = 0; | |
unsigned char tempVal = 0; | |
long counter = 0; | |
for(j=0;j<n;j++){ | |
if(board[j]==1) counter++; | |
} | |
printf("Initial number of 1's: %d\n",counter); | |
for(i=0;i<numIterations;i++){ | |
unsigned char tempChar = 0; | |
int match = 1; | |
for(j=0;j<fileLen;j++){ | |
tempChar = 0X00; | |
tempChar |= (board[j*8] & 0X01) << 7; | |
tempChar |= (board[j*8+1] & 0X01) << 6; | |
tempChar |= (board[j*8+2] & 0X01) << 5; | |
tempChar |= (board[j*8+3] & 0X01) << 4; | |
tempChar |= (board[j*8+4] & 0X01) << 3; | |
tempChar |= (board[j*8+5] & 0X01) << 2; | |
tempChar |= (board[j*8+6] & 0X01) << 1; | |
tempChar |= (board[j*8+7] & 0X01) << 0; | |
//printf("%X\n",buffer[j]); | |
//printf("%X\n",tempChar); | |
//printf("%X %X %X %X %X %X %X %X\n",board[j*8],board[j*8+1],board[j*8+2],board[j*8+3],board[j*8+4],board[j*8+5],board[j*8+6],board[j*8+7]); | |
if(tempChar != buffer[j]){ | |
//printf("Mismatch\n"); | |
match = 0; | |
break; | |
} | |
} | |
if(match==1){ | |
printf("Configuration found!\n"); | |
char filename[50]; | |
sprintf(filename,"%dresults.txt",time(NULL)); | |
FILE *f = fopen(filename, "w"); | |
if (f == NULL) | |
{ | |
printf("Error opening file!\n"); | |
exit(1); | |
} | |
fprintf(f,"Current iteration: %d\n",i); | |
fprintf(f,"Initial configuration size: %d\n",randomFill); | |
for(j=0;j<randomFill;j++){ | |
fprintf(f,"%d ",board2[j]); | |
} | |
fprintf(f,"\n"); | |
fclose(f); | |
break; | |
} | |
printf("Iteration: %d/%d\n",i,numIterations); | |
//board initial state | |
firstVal = board[0]; | |
endVal = board[n-1]; | |
//do first iteration | |
prevVal = board[0]; | |
board[0] = endVal^(board[0]|(~board[1])); | |
for(j=1;j<n-1;j++){ //do all 1...n-1 iterations | |
tempVal = board[j]; | |
board[j] = prevVal^(board[j]|(~board[j+1])); | |
prevVal = tempVal; | |
} | |
//do last iteration | |
board[n-1] = prevVal^(board[n-1]|(~firstVal)); | |
} | |
counter = 0; | |
for(j=0;j<n;j++){ | |
//printf("%d ",board[j]); | |
if(board[j]==1) counter++; | |
} | |
printf("Final number of 1's: %d\n",counter); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment