Day 2: Add C solution part 1
This commit is contained in:
parent
d57d26d9e1
commit
4776f5ca42
1 changed files with 31 additions and 0 deletions
31
02/solution_1.c
Normal file
31
02/solution_1.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
#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, 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':
|
||||
v += n;
|
||||
break;
|
||||
case 'u':
|
||||
v -= n;
|
||||
break;
|
||||
case 'f':
|
||||
h += n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Answer: %u\n", h * v);
|
||||
}
|
Loading…
Reference in a new issue