From a7c2b7d368522fb2d540503ab603f9b6edfc618c Mon Sep 17 00:00:00 2001 From: Jack Bond-Preston Date: Sat, 11 Dec 2021 03:01:24 +0000 Subject: [PATCH] day 2 Signed-off-by: Jack Bond-Preston --- 2/2.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2/2.c diff --git a/2/2.c b/2/2.c new file mode 100644 index 0000000..1498f1f --- /dev/null +++ b/2/2.c @@ -0,0 +1,51 @@ +#include +#include + +#define PARTB + +int main() { + FILE *f = fopen("input.txt", "r"); + if (!f) return 1; + + char opcode[16]; + int operand = 0, ret = 0; + + int depth = 0, distance = 0, aim = 0; + + int processed = 0; + + while ((ret = fscanf(f, "%s %d", opcode, &operand)) > 0) { + switch (opcode[0]) { + case 'f': //forward + distance += operand; +#ifdef PARTB + depth += aim * operand; +#endif + break; + case 'd': //down +#ifndef PARTB + depth += operand; +#else + aim += operand; +#endif + break; + case 'u': // up +#ifndef PARTB + depth -= operand; +#else + aim -= operand; +#endif + break; + } + + printf("%d\n", aim); + + processed++; + } + + printf("finished processing %d instrs\n", processed); + printf("final depth: %d, distance: %d\n", depth, distance); + printf("mult: %d*%d=%d\n", distance, depth, distance * depth); + + return 0; +} \ No newline at end of file