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.
 
 
 

53 lines
869 B

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
// This will hold the number of each count
unsigned int counts[8] = {0};
// 4 output values, each with a maximum length of 9 (8 + \0)
while (!feof (fp)){
char output[4][9] = {0};
fscanf(fp, "%*[^|]| %s %s %s %s\n", output[0], output[1], output[2], output[3]);
for ( unsigned int i = 0; i < 4; i++ ){
counts[strlen(output[i])]++;
}
}
fclose(fp);
unsigned int sum=0;
// 1s have a count of 2
sum+=counts[2];
// 4s have a count of 4
sum+=counts[4];
// 7s have a count of 3
sum+=counts[3];
// 8s have a count of 7
sum+=counts[7];
printf("Answer: %i\n", sum);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}