[TASK] Finished Day 13 + some style fixes to make sure eslint is happy enough to deploy to firebase functions

This commit is contained in:
2021-12-13 23:16:06 +01:00
parent 33a8ce77e4
commit 888f40d341
7 changed files with 1134 additions and 5 deletions

View File

@@ -112,7 +112,7 @@ class OctopusGrid {
toString(): string {
let lines = [];
const lines = [];
for (let y = 0; y < this.maxY; y++) {
let line = "";
for (let x = 0; x < this.maxX; x++) {

View File

@@ -28,7 +28,7 @@ class Day12 implements Day {
return caves;
}
addPath(from: string, to: string, caves: Map<string, string[]>) {
addPath(from: string, to: string, caves: Map<string, string[]>): void {
const existing = caves.get(from);
if (existing) {
existing.push(to);
@@ -37,7 +37,7 @@ class Day12 implements Day {
}
}
countPaths(pos: string, caves: Map<string, string[]>, visited: string[], secondSmallVisitAllowed: boolean = false): number {
countPaths(pos: string, caves: Map<string, string[]>, visited: string[], secondSmallVisitAllowed = false): number {
// If we reached the end, there is exactly one path
if (pos === "end") return 1;
@@ -64,7 +64,7 @@ class Day12 implements Day {
return sum;
}
isSmall(cave: string) {
isSmall(cave: string): boolean {
return cave.toLowerCase() === cave;
}
}

100
functions/src/day13.ts Normal file
View File

@@ -0,0 +1,100 @@
import Day from "./day";
import Vector2 from "./vector2";
class Day13 implements Day {
part1(input: string[]): number | string {
const {dots, folds} = this.parseInput(input);
const foldedDots = dots.map(d => folds[0].apply(d));
return new Set(foldedDots.map(d => d.key())).size;
}
part2(input: string[]): number | string {
const {dots, folds} = this.parseInput(input);
let foldedDots = [...dots];
for (const fold of folds) {
foldedDots = foldedDots.map(d => fold.apply(d));
}
return this.renderDots(dots);
}
private parseInput(input: string[]): { dots: Vector2[], folds: Fold[] } {
const dots = input.filter(s => s.trim() !== "" && !s.startsWith("fold"))
.map(s => Vector2.parse(s));
const folds = input.filter(s => s.startsWith("fold"))
.map(s => Fold.parseInstruction(s));
return {dots, folds};
}
renderDots(dots: Vector2[]): string {
const minX = Math.min(...dots.map(d => d.x));
const maxX = Math.max(...dots.map(d => d.x));
const minY = Math.min(...dots.map(d => d.y));
const maxY = Math.max(...dots.map(d => d.y));
const lines = [];
for (let y = minY; y <= maxY; y++) {
let line = "";
for (let x = minX; x <= maxX; x++) {
line += dots.some(d => d.x === x && d.y === y) ? "#" : ".";
}
lines.push(line);
}
return lines.join("\n");
}
}
class Fold {
axis: "x" | "y";
foldPosition: number;
constructor(axis: "x" | "y", position: number) {
this.axis = axis;
this.foldPosition = position;
}
static parseInstruction(line: string): Fold {
if (line.startsWith("fold along y=")) {
return new Fold("y", parseInt(line.substr(13)));
} else if (line.startsWith("fold along x=")) {
return new Fold("x", parseInt(line.substr(13)));
} else {
throw Error(`Invalid fold instruction: "${line}"`);
}
}
apply(pos: Vector2) {
if (this.axis === "y") {
return new Vector2(pos.x, this.foldAlong(pos.y));
} else {
return new Vector2(this.foldAlong(pos.x), pos.y);
}
}
private foldAlong(pos: number): number {
if (pos < this.foldPosition) return pos;
if (pos === this.foldPosition) console.log("Excuse me WTF?");
const distanceToFold = pos - this.foldPosition - 1;
return this.foldPosition - distanceToFold - 1;
}
}
export default Day13;

View File

@@ -14,6 +14,7 @@ import Day9 from "./day9";
import Day10 from "./day10";
import Day11 from "./day11";
import Day12 from "./day12";
import Day13 from "./day13";
// // Start writing Firebase Functions
@@ -42,6 +43,7 @@ export const day = {
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) }),
13: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day13(), request, response) }),
}

26
input/day/13-example.http Normal file
View File

@@ -0,0 +1,26 @@
POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-13
Content-Type: text/plain
6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5
###

1001
input/day/13.http Normal file

File diff suppressed because it is too large Load Diff