37 lines
703 B
C
37 lines
703 B
C
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define INPUT "inputs/input-day2"
|
||
|
|
||
|
int
|
||
|
main()
|
||
|
{
|
||
|
static char buf[256];
|
||
|
static char cmd[256];
|
||
|
static int operand;
|
||
|
|
||
|
int hpos = 0;
|
||
|
int depth = 0;
|
||
|
|
||
|
FILE *f = fopen(INPUT, "r");
|
||
|
|
||
|
while(fgets(buf, 256, f)) {
|
||
|
sscanf(buf, "%s %d\n", cmd, &operand);
|
||
|
|
||
|
if(strcmp(cmd, "forward") == 0) {
|
||
|
hpos += operand;
|
||
|
} else if(strcmp(cmd, "up") == 0) {
|
||
|
depth -= operand;
|
||
|
} else if(strcmp(cmd, "down") == 0) {
|
||
|
depth += operand;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fclose(f);
|
||
|
|
||
|
printf("depth: %d hpos: %d\n", depth, hpos);
|
||
|
printf("result: %d\n", hpos * depth);
|
||
|
|
||
|
return 0;
|
||
|
}
|