From ae3853b6e8ab02023ccd74baac6dc177b1ee879a Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Tue, 7 Dec 2021 21:34:31 +0300 Subject: 2021, day2: done --- 2021/day2/pilot.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/2021/day2/pilot.c b/2021/day2/pilot.c index 4268cc7..6bc0630 100644 --- a/2021/day2/pilot.c +++ b/2021/day2/pilot.c @@ -22,13 +22,23 @@ int main(int argc, char *argv[]) int scalar; char *way; + int h_pos = 0, depth = 0, aim = 0; while ((read = getline(&buffer, &len, fp)) != -1) { sscanf(buffer, "%s %d", way, &scalar); // printf("READ: <%s>, <%d>\n", way, scalar); - if (strncmp(buffer, "forward", strlen(buffer))) { + if (strncmp(way, "forward", strlen("forward")) == 0) { + // printf("GOING FORWARD\n"); + h_pos += scalar; + depth += aim * scalar; + } else if (strncmp(way, "down", strlen("forward")) == 0) { + aim += scalar; + } else { /* up */ + aim -= scalar; } } + printf("%d\n", depth * h_pos); + return 0; } -- cgit v1.2.3-70-g09d2