[TASK] Initial commit: basic firebase setup and part 1 almost solved (of-by-one)
This commit is contained in:
13
functions/src/day1.ts
Normal file
13
functions/src/day1.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
class Day1 {
|
||||
static part1(input: string[]): number {
|
||||
|
||||
let res = 0;
|
||||
for (let i = 1; i < input.length; i++) {
|
||||
if (input[i] > input[i-1]) res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export default Day1;
|
||||
35
functions/src/index.ts
Normal file
35
functions/src/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import * as functions from "firebase-functions";
|
||||
import Utils from "./utils";
|
||||
import Day1 from "./day1";
|
||||
import {Response} from "firebase-functions";
|
||||
|
||||
|
||||
// // Start writing Firebase Functions
|
||||
// // https://firebase.google.com/docs/functions/typescript
|
||||
//
|
||||
// export const helloWorld = functions.https.onRequest((request, response) => {
|
||||
// functions.logger.info("Hello logs!", {structuredData: true});
|
||||
// response.send("Hello from Firebase!");
|
||||
// });
|
||||
|
||||
interface DayResult {
|
||||
part1: any;
|
||||
part2: any;
|
||||
}
|
||||
|
||||
export const day = {
|
||||
1: functions.region('europe-west1').https.onRequest((request, response) => {
|
||||
|
||||
const input = Utils.parseInput(request);
|
||||
const part1 = Day1.part1(input);
|
||||
|
||||
sendResponse(response, part1, 0);
|
||||
|
||||
}),
|
||||
}
|
||||
|
||||
function sendResponse(response: Response, part1: any, part2: any) {
|
||||
|
||||
const res: DayResult = { part1, part2 };
|
||||
response.send(res);
|
||||
}
|
||||
19
functions/src/utils.ts
Normal file
19
functions/src/utils.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import {Request} from "firebase-functions/lib/common/providers/https";
|
||||
|
||||
class Utils {
|
||||
static parseInput(request: Request): string[] {
|
||||
|
||||
const body = request.body;
|
||||
if (typeof body === 'string') {
|
||||
return body.split('\n');
|
||||
} else if (body.constructor === Array) {
|
||||
return body;
|
||||
} else {
|
||||
throw Error("Invalid request");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default Utils;
|
||||
Reference in New Issue
Block a user