2021-12-04 22:19:25 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-12-07 18:39:07 -05:00
|
|
|
#define INPUT "../inputs/input-day2"
|
2021-12-04 22:19:25 -05:00
|
|
|
|
|
|
|
int
|
2021-12-04 22:23:00 -05:00
|
|
|
first()
|
2021-12-04 22:19:25 -05:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2021-12-04 22:23:00 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
second()
|
|
|
|
{
|
|
|
|
static char buf[256];
|
|
|
|
static char cmd[256];
|
|
|
|
static int operand;
|
|
|
|
|
|
|
|
int hpos = 0;
|
|
|
|
int depth = 0;
|
|
|
|
int aim = 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;
|
|
|
|
depth += (aim * operand);
|
|
|
|
} else if(strcmp(cmd, "up") == 0) {
|
|
|
|
aim -= operand;
|
|
|
|
} else if(strcmp(cmd, "down") == 0) {
|
|
|
|
aim += operand;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
printf("aim: %d depth: %d hpos: %d\n", aim, depth, hpos);
|
|
|
|
printf("result: %d\n", hpos * depth);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
first();
|
|
|
|
second();
|
2021-12-04 22:19:25 -05:00
|
|
|
return 0;
|
|
|
|
}
|