From 32f00ea017f987c08ee719aed18a33ee774b4c8e Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Thu, 2 Dec 2021 22:04:51 +0100 Subject: [PATCH] [TASK] Day 2 Part 2 --- functions/src/day2.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/functions/src/day2.ts b/functions/src/day2.ts index 3c32137..99a0597 100644 --- a/functions/src/day2.ts +++ b/functions/src/day2.ts @@ -18,14 +18,27 @@ class Day2 { static part2(input: string[]): number { - - // TODO implement - return 0; + let depth = 0; + let aim = 0; + let x = 0; + for (const entry of input) { + + if (entry.startsWith("up")) { + aim -= this.readUnits(entry); + } else if (entry.startsWith("down")) { + aim += this.readUnits(entry); + } else if (entry.startsWith("forward")) { + const movement = this.readUnits(entry); + depth += aim * movement; + x += movement; + } + } + return x * depth; } private static readUnits(input: string): number { - return parseInt(input.substr(input.indexOf(' ') + 1)); + return parseInt(input.substr(input.indexOf(" ") + 1)); } } -export default Day2; \ No newline at end of file +export default Day2;