aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

b.c (604B) [raw]


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 
      5 int main(void) {
      6     int depth = 0;
      7     int pos = 0;
      8     int aim = 0;
      9     int val;
     10     char comm[256];
     11     while(scanf("%255s %d\n", comm, &val) != EOF) {
     12         if (!strcmp(comm, "forward")) {
     13             pos += val;
     14             depth += val * aim;
     15         } else if (!strcmp(comm, "up")) {
     16             aim -= val;
     17         } else if (!strcmp(comm, "down")) {
     18             aim += val;
     19         } else {
     20             fprintf(stderr, "bad command: %s\n", comm);
     21             exit(1);
     22         }
     23     }
     24     printf("%d\n", pos * depth);
     25     return 0;
     26 }