[CLEANUP] Code cleanup to reduce the amount of duplicate code each day

This commit is contained in:
2021-12-05 11:25:07 +01:00
parent 039c36f09a
commit 8b3963fa6a
6 changed files with 40 additions and 40 deletions

7
functions/src/day.ts Normal file
View File

@@ -0,0 +1,7 @@
interface Day {
part1(input: string[]): number;
part2(input: string[]): number;
}
export default Day;

View File

@@ -1,6 +1,7 @@
import Day from "./day";
class Day1 { class Day1 implements Day {
static part1(input: string[]): number { part1(input: string[]): number {
const values = input.map(s => parseInt(s)); const values = input.map(s => parseInt(s));
let res = 0; let res = 0;
@@ -10,7 +11,7 @@ class Day1 {
return res; return res;
} }
static part2(input: string[]): number { part2(input: string[]): number {
const values = input.map(s => parseInt(s)); const values = input.map(s => parseInt(s));
let res = 0; let res = 0;

View File

@@ -1,22 +1,23 @@
import Utils from "./utils"; import Utils from "./utils";
import Day from "./day";
class Day2 { class Day2 implements Day {
static part1(input: string[]): number { part1(input: string[]): number {
const forwardMoves = input.filter(s => s.startsWith("forward")) const forwardMoves = input.filter(s => s.startsWith("forward"))
.map(s => this.readUnits(s)); .map(s => Day2.readUnits(s));
const finalForwardPosition = Utils.sum(forwardMoves); const finalForwardPosition = Utils.sum(forwardMoves);
const depthMovesUp = input.filter(s => s.startsWith("up")) const depthMovesUp = input.filter(s => s.startsWith("up"))
.map(s => this.readUnits(s)); .map(s => Day2.readUnits(s));
const depthMovesDown = input.filter(s => s.startsWith("down")) const depthMovesDown = input.filter(s => s.startsWith("down"))
.map(s => this.readUnits(s)); .map(s => Day2.readUnits(s));
const finalDepthPosition = Utils.sum(depthMovesDown) - Utils.sum(depthMovesUp); const finalDepthPosition = Utils.sum(depthMovesDown) - Utils.sum(depthMovesUp);
return finalForwardPosition * finalDepthPosition; return finalForwardPosition * finalDepthPosition;
} }
static part2(input: string[]): number { part2(input: string[]): number {
let depth = 0; let depth = 0;
let aim = 0; let aim = 0;
@@ -24,11 +25,11 @@ class Day2 {
for (const entry of input) { for (const entry of input) {
if (entry.startsWith("up")) { if (entry.startsWith("up")) {
aim -= this.readUnits(entry); aim -= Day2.readUnits(entry);
} else if (entry.startsWith("down")) { } else if (entry.startsWith("down")) {
aim += this.readUnits(entry); aim += Day2.readUnits(entry);
} else if (entry.startsWith("forward")) { } else if (entry.startsWith("forward")) {
const movement = this.readUnits(entry); const movement = Day2.readUnits(entry);
depth += aim * movement; depth += aim * movement;
x += movement; x += movement;
} }

View File

@@ -1,12 +1,13 @@
import Utils from "./utils"; import Utils from "./utils";
import Day from "./day";
class Day3 { class Day3 implements Day {
public static part1(input: string[]): number { public part1(input: string[]): number {
let sums = Utils.zeroes(input[0].length); const sums = Utils.zeroes(input[0].length);
for (let value of input) { for (const value of input) {
for (let i = 0; i < value.length; i++) { for (let i = 0; i < value.length; i++) {
if (value[i] === "1") { if (value[i] === "1") {
sums[i] += 1; sums[i] += 1;
@@ -20,9 +21,9 @@ class Day3 {
return gamma * epsilon; return gamma * epsilon;
} }
public static part2(input: string[]): number { public part2(input: string[]): number {
return this.findLifeSupportRating(input, "oxygen") * this.findLifeSupportRating(input, "CO2"); return Day3.findLifeSupportRating(input, "oxygen") * Day3.findLifeSupportRating(input, "CO2");
} }
static findLifeSupportRating(input: string[], type: "oxygen" | "CO2"): number { static findLifeSupportRating(input: string[], type: "oxygen" | "CO2"): number {

View File

@@ -1,9 +1,10 @@
import * as functions from "firebase-functions"; import * as functions from "firebase-functions";
import Utils from "./utils"; import Utils from "./utils";
import Day1 from "./day1"; import Day1 from "./day1";
import {Response} from "firebase-functions"; import {Request, Response} from "firebase-functions";
import Day2 from "./day2"; import Day2 from "./day2";
import Day3 from "./day3"; import Day3 from "./day3";
import Day from "./day";
// // Start writing Firebase Functions // // Start writing Firebase Functions
@@ -20,30 +21,19 @@ interface DayResult {
} }
export const day = { export const day = {
1: functions.region("europe-west1").https.onRequest((request, response) => { 1: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day1(), request, response) }),
2: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day2(), request, response) }),
3: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day3(), request, response) }),
const input = Utils.parseInput(request); }
const part1 = Day1.part1(input);
const part2 = Day1.part2(input);
sendResponse(response, part1, part2);
}), function processDay(day: Day, request: Request, response: Response) {
2: functions.region("europe-west1").https.onRequest((request, response) => {
const input = Utils.parseInput(request); const input = Utils.parseInput(request);
const part1 = Day2.part1(input); const part1 = day.part1(input);
const part2 = Day2.part2(input); const part2 = day.part2(input);
sendResponse(response, part1, part2); sendResponse(response, part1, part2);
}),
3: functions.region("europe-west1").https.onRequest((request, response) => {
const input = Utils.parseInput(request);
const part1 = Day3.part1(input);
const part2 = Day3.part2(input);
sendResponse(response, part1, part2);
}),
} }
function sendResponse(response: Response, part1: string | number, part2: string | number) { function sendResponse(response: Response, part1: string | number, part2: string | number) {

View File

@@ -19,7 +19,7 @@ class Utils {
static zeroes(length: number): number[] { static zeroes(length: number): number[] {
let res = []; const res = [];
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
res.push(0); res.push(0);
} }