[TASK] Finished Day 1
This commit is contained in:
@@ -9,7 +9,7 @@ module.exports = {
|
||||
"plugin:import/errors",
|
||||
"plugin:import/warnings",
|
||||
"plugin:import/typescript",
|
||||
"google",
|
||||
// "google",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
],
|
||||
parser: "@typescript-eslint/parser",
|
||||
|
||||
@@ -9,6 +9,20 @@ class Day1 {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static part2(input: string[]): number {
|
||||
|
||||
const values = input.map(s => parseInt(s));
|
||||
let res = 0;
|
||||
let previousSum = Math.max(...values) * 3;
|
||||
for (let i = 2; i < values.length; i++) {
|
||||
const sum = values[i - 2] + values[i - 1] + values[i];
|
||||
if (sum > previousSum) res++;
|
||||
previousSum = sum;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export default Day1;
|
||||
@@ -13,22 +13,23 @@ import {Response} from "firebase-functions";
|
||||
// });
|
||||
|
||||
interface DayResult {
|
||||
part1: any;
|
||||
part2: any;
|
||||
part1: string | number;
|
||||
part2: string | number;
|
||||
}
|
||||
|
||||
export const day = {
|
||||
1: functions.region('europe-west1').https.onRequest((request, response) => {
|
||||
1: functions.region("europe-west1").https.onRequest((request, response) => {
|
||||
|
||||
const input = Utils.parseInput(request);
|
||||
const part1 = Day1.part1(input);
|
||||
const part2 = Day1.part2(input);
|
||||
|
||||
sendResponse(response, part1, 0);
|
||||
sendResponse(response, part1, part2);
|
||||
|
||||
}),
|
||||
}
|
||||
|
||||
function sendResponse(response: Response, part1: any, part2: any) {
|
||||
function sendResponse(response: Response, part1: string | number, part2: string | number) {
|
||||
|
||||
const res: DayResult = { part1, part2 };
|
||||
response.send(res);
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
import {Request} from "firebase-functions/lib/common/providers/https";
|
||||
import { Request } from "firebase-functions";
|
||||
|
||||
class Utils {
|
||||
static parseInput(request: Request): string[] {
|
||||
|
||||
const body = request.body;
|
||||
if (typeof body === 'string') {
|
||||
return body.split('\n');
|
||||
if (typeof body === "string") {
|
||||
return body.split("\n");
|
||||
} else if (body.constructor === Array) {
|
||||
return body;
|
||||
} else {
|
||||
throw Error("Invalid request");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user