[TASK] Implemented Day 6
This commit is contained in:
35
functions/src/day6.ts
Normal file
35
functions/src/day6.ts
Normal 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;
|
||||
@@ -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) }),
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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[] {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"outDir": "lib",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"target": "es2017"
|
||||
"target": "es2020"
|
||||
},
|
||||
"compileOnSave": true,
|
||||
"include": [
|
||||
|
||||
6
input/day/6-example.http
Normal file
6
input/day/6-example.http
Normal file
@@ -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
|
||||
|
||||
###
|
||||
6
input/day/6.http
Normal file
6
input/day/6.http
Normal file
@@ -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
|
||||
|
||||
###
|
||||
Reference in New Issue
Block a user