[TASK] Solved Day 12
This commit is contained in:
72
functions/src/day12.ts
Normal file
72
functions/src/day12.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import Day from "./day";
|
||||
|
||||
class Day12 implements Day {
|
||||
|
||||
part1(input: string[]): number | string {
|
||||
|
||||
const caves = this.parseInput(input);
|
||||
return this.countPaths("start", caves, []);
|
||||
}
|
||||
|
||||
|
||||
part2(input: string[]): number | string {
|
||||
|
||||
const caves = this.parseInput(input);
|
||||
return this.countPaths("start", caves, [], true);
|
||||
}
|
||||
|
||||
private parseInput(input: string[]) {
|
||||
const caves = new Map<string, string[]>();
|
||||
for (const line of input) {
|
||||
const splitIndex = line.indexOf("-");
|
||||
const from = line.substr(0, splitIndex);
|
||||
const to = line.substr(splitIndex + 1);
|
||||
|
||||
this.addPath(from, to, caves);
|
||||
this.addPath(to, from, caves);
|
||||
}
|
||||
return caves;
|
||||
}
|
||||
|
||||
addPath(from: string, to: string, caves: Map<string, string[]>) {
|
||||
const existing = caves.get(from);
|
||||
if (existing) {
|
||||
existing.push(to);
|
||||
} else {
|
||||
caves.set(from, [to]);
|
||||
}
|
||||
}
|
||||
|
||||
countPaths(pos: string, caves: Map<string, string[]>, visited: string[], secondSmallVisitAllowed: boolean = false): number {
|
||||
|
||||
// If we reached the end, there is exactly one path
|
||||
if (pos === "end") return 1;
|
||||
|
||||
const destinations = caves.get(pos);
|
||||
if (!destinations) {
|
||||
throw Error(`No paths from ${pos}`);
|
||||
}
|
||||
|
||||
const newVisited = [...visited];
|
||||
if (this.isSmall(pos)) {
|
||||
newVisited.push(pos);
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
for (const destination of destinations) {
|
||||
if (!this.isSmall(destination) || !visited.includes(destination)) {
|
||||
sum += this.countPaths(destination, caves, newVisited, secondSmallVisitAllowed)
|
||||
} else if (secondSmallVisitAllowed && destination !== "start") {
|
||||
sum += this.countPaths(destination, caves, newVisited, false)
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
isSmall(cave: string) {
|
||||
return cave.toLowerCase() === cave;
|
||||
}
|
||||
}
|
||||
|
||||
export default Day12;
|
||||
@@ -13,6 +13,7 @@ import Day8 from "./day8";
|
||||
import Day9 from "./day9";
|
||||
import Day10 from "./day10";
|
||||
import Day11 from "./day11";
|
||||
import Day12 from "./day12";
|
||||
|
||||
|
||||
// // Start writing Firebase Functions
|
||||
@@ -40,6 +41,7 @@ export const day = {
|
||||
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) }),
|
||||
11: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day11(), request, response) }),
|
||||
12: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day12(), request, response) }),
|
||||
|
||||
}
|
||||
|
||||
@@ -56,4 +58,4 @@ function sendResponse(response: Response, part1: string | number, part2: string
|
||||
|
||||
const res: DayResult = { part1, part2 };
|
||||
response.send(res);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user