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.
42 lines
776 B
42 lines
776 B
3 years ago
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
int main( int argc, char *argv[] ){
|
||
|
if( argc == 2 ) {
|
||
|
|
||
|
unsigned int horizontal = 0;
|
||
|
unsigned int depth = 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;
|
||
|
else if ( strcmp(direction,"down") == 0 )
|
||
|
depth+=amount;
|
||
|
else if ( strcmp(direction,"up") == 0 )
|
||
|
depth-=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;
|
||
|
}
|
||
|
}
|