My solutions to the advent of code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.3 KiB

3 years ago
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int countLines(char filename[]){
FILE *fp;
fp=fopen(filename, "r");
long int lines =0;
if ( fp == NULL ) {
return -1;
}
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
fclose(fp); // closing file
return lines;
}
void countPositionBits(int linesize, int noOfLines, char *lines[noOfLines], unsigned int digitCount[]){
for ( int i = 0; i < noOfLines; i++){
for ( int j = 0; j < linesize; j++ ){
if ( lines[i][j] == '1' )
digitCount[j]++;
}
}
}
char* filterO2List(unsigned int linesize, unsigned int noOfLines, char **lines, unsigned int iteration){
if ( noOfLines == 1 )
return lines[0];
unsigned int digitCount[linesize];
memset( &digitCount, 0, linesize*sizeof(unsigned int) );
countPositionBits(linesize, noOfLines, lines, digitCount);
char bitKeep = ( digitCount[iteration] >= ((double)noOfLines) / 2 ) ? '1' : '0';
unsigned int newLinesIndex=0;
//Not sure why +4 here but any less and it buffer overflows
char **newLines = (char**)malloc((digitCount[iteration]+4) * sizeof(char*));
for ( unsigned int i=0; i < noOfLines; i++ ){
if (lines[i][iteration] == bitKeep)
newLines[newLinesIndex++] = lines[i];
}
return filterO2List( linesize, newLinesIndex, newLines, ++iteration );
}
char* filterCO2List(unsigned int linesize, unsigned int noOfLines, char **lines, unsigned int iteration){
if ( noOfLines == 1 )
return lines[0];
unsigned int digitCount[linesize];
memset( &digitCount, 0, linesize*sizeof(unsigned int) );
countPositionBits(linesize, noOfLines, lines, digitCount);
char bitKeep = ( digitCount[iteration] >= ((double)noOfLines) / 2 ) ? '0' : '1';
unsigned int newLinesIndex=0;
//Not sure why +4 here but any less and it buffer overflows
char **newLines = (char**)malloc((digitCount[iteration]+4) * sizeof(char*));
for ( unsigned int i=0; i < noOfLines; i++ ){
if (lines[i][iteration] == bitKeep)
newLines[newLinesIndex++] = lines[i];
}
return filterCO2List( linesize, newLinesIndex, newLines, ++iteration );
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
unsigned int gamma = 0;
unsigned int epsilon = 0;
unsigned int lineLength = 0;
unsigned int noOfLines = countLines( argv[1] );
unsigned int iteration=0;
// Read the values into an array
FILE *fp=fopen(argv[1], "r");
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
rewind(fp);
unsigned int digitCount[lineLength];
memset( &digitCount, 0, lineLength*sizeof(int) );
// Make a big array to hold the lines
char **lines = (char**)malloc(noOfLines * sizeof(char*));
// Loop through the file and populate the array
for ( unsigned int i = 0; !feof(fp); i++){
char *line = (char*)malloc(lineLength);
fscanf(fp, "%s\n", line);
lines[i] = line;
}
char *o2Rating = filterO2List( lineLength, noOfLines, lines, 0 );
char *co2Rating = filterCO2List( lineLength, noOfLines, lines, 0 );
int o2RatingDec = (int)strtol(o2Rating, NULL, 2);
int co2RatingDec = (int)strtol(co2Rating, NULL, 2);
printf("O2 Rating: %s -> %i\n", o2Rating, o2RatingDec);
printf("CO2 Rating: %s -> %i\n", co2Rating, co2RatingDec);
printf("Answer: %i\n", o2RatingDec * co2RatingDec);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}