[TASK] Implemented Day 6

This commit is contained in:
2021-12-07 20:01:39 +01:00
parent fed31ccb0e
commit 95772849e4
6 changed files with 55 additions and 2 deletions

35
functions/src/day6.ts Normal file
View File

@@ -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;

View File

@@ -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) }),
}

View File

@@ -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[] {

View File

@@ -6,7 +6,7 @@
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
"target": "es2020"
},
"compileOnSave": true,
"include": [