Day 2: Add C solution part 2
This commit is contained in:
parent
4776f5ca42
commit
da09365608
1 changed files with 32 additions and 0 deletions
32
02/solution_2.c
Normal file
32
02/solution_2.c
Normal 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);
|
||||
}
|
Loading…
Reference in a new issue