[TASK] Solved Day 12

This commit is contained in:
2021-12-13 21:43:07 +01:00
parent ed88c69cea
commit 33a8ce77e4
4 changed files with 157 additions and 1 deletions

72
functions/src/day12.ts Normal file
View 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;

View File

@@ -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);
}
}

52
input/day/12-example.http Normal file
View File

@@ -0,0 +1,52 @@
POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-12
Content-Type: text/plain
start-A
start-b
A-c
A-b
b-d
A-end
b-end
###
POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-12
Content-Type: text/plain
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc
###
POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-12
Content-Type: text/plain
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW
###

30
input/day/12.http Normal file
View File

@@ -0,0 +1,30 @@
POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-12
Content-Type: text/plain
start-kc
pd-NV
start-zw
UI-pd
HK-end
UI-kc
pd-ih
ih-end
start-UI
kc-zw
end-ks
MF-mq
HK-zw
LF-ks
HK-kc
ih-HK
kc-pd
ks-pd
MF-pd
UI-zw
ih-NV
ks-HK
MF-kc
zw-NV
NV-ks
###