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.
 
 
 

43 lines
824 B

#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
unsigned int horizontal = 0;
unsigned int depth = 0;
unsigned int aim = 0;
FILE *fp=fopen(argv[1], "r");
while (!feof (fp)){
char direction[7] = "";
int amount = 0;
fscanf(fp, "%s %d\n", direction, &amount);
if ( strcmp(direction,"forward") == 0 ){
horizontal+=amount;
depth+=(aim*amount);
} else if ( strcmp(direction,"down") == 0 )
aim+=amount;
else if ( strcmp(direction,"up") == 0 )
aim-=amount;
else
printf("Hopefully we don't get here\n");
}
printf("Horizontal: %d\n", horizontal);
printf("Depth: %d\n", depth);
printf("Product: %d\n", horizontal * depth);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}