From 95772849e410b530032d8f4b6bd49e01d62ca06f Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Tue, 7 Dec 2021 20:01:39 +0100 Subject: [PATCH] [TASK] Implemented Day 6 --- functions/src/day6.ts | 35 +++++++++++++++++++++++++++++++++++ functions/src/index.ts | 2 ++ functions/src/utils.ts | 6 +++++- functions/tsconfig.json | 2 +- input/day/6-example.http | 6 ++++++ input/day/6.http | 6 ++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 functions/src/day6.ts create mode 100644 input/day/6-example.http create mode 100644 input/day/6.http diff --git a/functions/src/day6.ts b/functions/src/day6.ts new file mode 100644 index 0000000..575f13f --- /dev/null +++ b/functions/src/day6.ts @@ -0,0 +1,35 @@ +import Day from "./day"; +import Utils from "./utils"; + +class Day6 implements Day { + + part1(input: string[]): number | string { + + const initial = input[0].split(",").map(s => parseInt(s)); + return this.countFishAfter(initial, 80).toString(10); + } + + part2(input: string[]): number | string { + + const initial = input[0].split(",").map(s => parseInt(s)); + return this.countFishAfter(initial, 256).toString(10); + } + + countFishAfter(initial: number[], days: number): bigint { + + // We represent the data as the number of glowfish in a specific state per day, then we can just shift that array around: + const fishStates = Array(9).fill(0n); + initial.forEach(f => fishStates[f]++); + + for (let i = 0; i < days; i++) { + const reproducingFish = fishStates.shift() + fishStates[6] += reproducingFish; + fishStates.push(reproducingFish); + } + + return Utils.bigSum(fishStates); + } + +} + +export default Day6; \ No newline at end of file diff --git a/functions/src/index.ts b/functions/src/index.ts index a50ae52..81dc461 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -7,6 +7,7 @@ import Day2 from "./day2"; import Day3 from "./day3"; import Day4 from "./day4"; import Day5 from "./day5"; +import Day6 from "./day6"; // // Start writing Firebase Functions @@ -28,6 +29,7 @@ export const day = { 3: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day3(), request, response) }), 4: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day4(), request, response) }), 5: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day5(), request, response) }), + 6: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day6(), request, response) }), } diff --git a/functions/src/utils.ts b/functions/src/utils.ts index b85f13c..3c81a3f 100644 --- a/functions/src/utils.ts +++ b/functions/src/utils.ts @@ -14,7 +14,11 @@ class Utils { } static sum(values: number[]): number { - return values.reduce((a, b) => a + b, 0); + return values.reduce((a, b) => a + b); + } + + static bigSum(values: bigint[]): bigint { + return values.reduce((a, b) => a + b); } static zeroes(length: number): number[] { diff --git a/functions/tsconfig.json b/functions/tsconfig.json index 7ce05d0..f40bec8 100644 --- a/functions/tsconfig.json +++ b/functions/tsconfig.json @@ -6,7 +6,7 @@ "outDir": "lib", "sourceMap": true, "strict": true, - "target": "es2017" + "target": "es2020" }, "compileOnSave": true, "include": [ diff --git a/input/day/6-example.http b/input/day/6-example.http new file mode 100644 index 0000000..248d7ae --- /dev/null +++ b/input/day/6-example.http @@ -0,0 +1,6 @@ +POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-6 +Content-Type: text/plain + +3,4,3,1,2 + +### diff --git a/input/day/6.http b/input/day/6.http new file mode 100644 index 0000000..b7176b6 --- /dev/null +++ b/input/day/6.http @@ -0,0 +1,6 @@ +POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-6 +Content-Type: text/plain + +3,3,2,1,4,1,1,2,3,1,1,2,1,2,1,1,1,1,1,1,4,1,1,5,2,1,1,2,1,1,1,3,5,1,5,5,1,1,1,1,3,1,1,3,2,1,1,1,1,1,1,4,1,1,1,1,1,1,1,4,1,3,3,1,1,3,1,3,1,2,1,3,1,1,4,1,2,4,4,5,1,1,1,1,1,1,4,1,5,1,1,5,1,1,3,3,1,3,2,5,2,4,1,4,1,2,4,5,1,1,5,1,1,1,4,1,1,5,2,1,1,5,1,1,1,5,1,1,1,1,1,3,1,5,3,2,1,1,2,2,1,2,1,1,5,1,1,4,5,1,4,3,1,1,1,1,1,1,5,1,1,1,5,2,1,1,1,5,1,1,1,4,4,2,1,1,1,1,1,1,1,3,1,1,4,4,1,4,1,1,5,3,1,1,1,5,2,2,4,2,1,1,3,1,5,5,1,1,1,4,1,5,1,1,1,4,3,3,3,1,3,1,5,1,4,2,1,1,5,1,1,1,5,5,1,1,2,1,1,1,3,1,1,1,2,3,1,2,2,3,1,3,1,1,4,1,1,2,1,1,1,1,3,5,1,1,2,1,1,1,4,1,1,1,1,1,2,4,1,1,5,3,1,1,1,2,2,2,1,5,1,3,5,3,1,1,4,1,1,4 + +###