2021-12-10 11:01:51 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-12-10 14:37:41 -08:00
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
2021-12-10 11:01:51 -08:00
|
|
|
|
|
|
|
#include "input.h"
|
|
|
|
|
2021-12-10 14:37:41 -08:00
|
|
|
static void solution(long p2) {
|
|
|
|
size_t sz = 0;
|
|
|
|
char **input = get_lines("input/2021/02", &sz);
|
|
|
|
long h = 0, d = 0, a = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sz; ++i) {
|
|
|
|
size_t sp_sz = 0;
|
|
|
|
char **sp = split(input[i], &sp_sz, " ");
|
|
|
|
if (sp == NULL) {
|
|
|
|
printf("Failed to split\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
const char *errstr;
|
|
|
|
long n = 0;
|
|
|
|
n = (long)strtonum(sp[1], LONG_MIN, LONG_MAX, &errstr);
|
|
|
|
if (errstr) {
|
|
|
|
printf("Failed to convert %s to long\n", sp[i]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p2) {
|
|
|
|
if (strcmp("forward", sp[0]) == 0) {
|
|
|
|
h += n;
|
|
|
|
} else if (strcmp("down", sp[0]) == 0) {
|
|
|
|
d += n;
|
|
|
|
} else {
|
|
|
|
d -= n;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcmp("forward", sp[0]) == 0) {
|
|
|
|
h += n;
|
|
|
|
d += a * n;
|
|
|
|
} else if (strcmp("down", sp[0]) == 0) {
|
|
|
|
a += n;
|
|
|
|
} else {
|
|
|
|
a -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(sp);
|
|
|
|
}
|
|
|
|
printf("%ld\n", h * d);
|
|
|
|
del_lines(input);
|
|
|
|
}
|
|
|
|
|
2021-12-10 11:01:51 -08:00
|
|
|
void advent2021day02(void) {
|
2021-12-10 14:37:41 -08:00
|
|
|
solution(0);
|
|
|
|
solution(1);
|
2021-12-10 11:01:51 -08:00
|
|
|
}
|