-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-2.c
33 lines (31 loc) · 852 Bytes
/
02-2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <stdbool.h>
int main (void)
{
FILE * fin;
char command[8];
int64_t depth = 0, horizontal = 0, aim = 0, val;
if ((fin = fopen("02-input.txt", "r"))){
while (fscanf(fin, "%8s %ld", command, &val) != EOF){
if (strcmp(command, "forward") == 0){
horizontal += val;
depth += (val * aim);
}
else if(strcmp(command, "up") == 0){
aim -= val;
}
else if(strcmp(command, "down") == 0){
aim += val;
}
else
assert(false && "Error in parsing step\n");
}
printf("%ld\n", depth * horizontal);
fclose(fin);
}else{
printf("Can't open the file\n");
}
}