2
1
Fork 0

Day 2: Add C solution part 2

This commit is contained in:
Mia Herkt 2021-12-02 11:14:39 +01:00
parent 4776f5ca42
commit da09365608
Signed by: mia
GPG key ID: 72E154B8622EC191

32
02/solution_2.c Normal file
View file

@ -0,0 +1,32 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
int main(void) {
FILE *fp = fopen("input.txt", "r");
char buf[16] = {0}, *np;
unsigned n, aim = 0, h = 0, v = 0;
while (fgets(buf, 16, fp) != NULL) {
np = strchr(buf, ' ');
assert(np != NULL);
np++;
sscanf(np, "%u", &n);
switch (buf[0]) {
case 'd':
aim += n;
break;
case 'u':
aim -= n;
break;
case 'f':
h += n;
v += aim * n;
break;
}
}
printf("Answer: %u\n", h * v);
}