[TASK] Solved Day 10
This commit is contained in:
87
functions/src/day10.ts
Normal file
87
functions/src/day10.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import Day from "./day";
|
||||
import Utils from "./utils";
|
||||
|
||||
const MISSING_CHARACTER_SCORES: {[key: string]: number} = {
|
||||
")": 1,
|
||||
"]": 2,
|
||||
"}": 3,
|
||||
">": 4,
|
||||
}
|
||||
|
||||
class Day10 implements Day {
|
||||
|
||||
part1(input: string[]): number | string {
|
||||
|
||||
return Utils.sum(
|
||||
input.map(s => this.checkSyntax(s))
|
||||
.map(r => r.error === "corrupt" ? this.getCorruptCharacterScore(r.got) : 0)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
part2(input: string[]): number | string {
|
||||
|
||||
const incompletenessScores = input
|
||||
.map(s => this.checkSyntax(s))
|
||||
.filter(r => r.error === "incomplete")
|
||||
.map(r => r.error === "incomplete" ? this.calculateIncompletenessScore(r.missing) : 0);
|
||||
|
||||
incompletenessScores.sort((a, b) => a - b);
|
||||
return incompletenessScores[Math.floor(incompletenessScores.length / 2)];
|
||||
}
|
||||
|
||||
calculateIncompletenessScore(missing: string): number {
|
||||
|
||||
let score = 0;
|
||||
for (const missingElement of missing) {
|
||||
score *= 5;
|
||||
score += MISSING_CHARACTER_SCORES[missingElement];
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
getCorruptCharacterScore(char: string): number {
|
||||
switch (char) {
|
||||
case ")": return 3;
|
||||
case "]": return 57;
|
||||
case "}": return 1197;
|
||||
case ">": return 25137;
|
||||
default: throw Error(`Can't find points for character '${char}'`);
|
||||
}
|
||||
}
|
||||
|
||||
checkSyntax(line: string): { error: "corrupt", expected: string, got: string } | { error: "incomplete", missing: string } | { error: "none" } {
|
||||
|
||||
const stack = [];
|
||||
for (const char of line) {
|
||||
|
||||
switch (char) {
|
||||
case "(": stack.push(")"); break;
|
||||
case "[": stack.push("]"); break;
|
||||
case "{": stack.push("}"); break;
|
||||
case "<": stack.push(">"); break;
|
||||
default: {
|
||||
const expected = stack[stack.length - 1];
|
||||
if (expected === char) {
|
||||
stack.pop();
|
||||
} else {
|
||||
return {error: "corrupt", expected, got: char};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (stack.length === 0) {
|
||||
return { error: "none" }
|
||||
} else {
|
||||
let missing = "";
|
||||
while(stack.length > 0) {
|
||||
missing += stack.pop();
|
||||
}
|
||||
return { error: "incomplete", missing }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Day10;
|
||||
@@ -11,6 +11,7 @@ import Day6 from "./day6";
|
||||
import Day7 from "./day7";
|
||||
import Day8 from "./day8";
|
||||
import Day9 from "./day9";
|
||||
import Day10 from "./day10";
|
||||
|
||||
|
||||
// // Start writing Firebase Functions
|
||||
@@ -36,6 +37,7 @@ export const day = {
|
||||
7: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day7(), request, response) }),
|
||||
8: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day8(), request, response) }),
|
||||
9: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day9(), request, response) }),
|
||||
10: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day10(), request, response) }),
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user