[TASK] Finished Day5
This commit is contained in:
@@ -8,7 +8,7 @@ class Day4 implements Day {
|
||||
const { drawnNumbers, bingoCards } = this.parseInput(input);
|
||||
|
||||
// We are done as soon as we encounter the first bingo
|
||||
return this.playBingo(drawnNumbers, bingoCards, _ => ({ isDone: true }) ).score;
|
||||
return this.playBingo(drawnNumbers, bingoCards, () => ({ isDone: true }) ).score;
|
||||
}
|
||||
|
||||
part2(input: string[]): number | string {
|
||||
|
||||
93
functions/src/day5.ts
Normal file
93
functions/src/day5.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import Day from "./day";
|
||||
import Vector2 from "./vector2";
|
||||
|
||||
class Day5 implements Day {
|
||||
|
||||
part1(input: string[]): number | string {
|
||||
|
||||
const vents = Day5.parseInput(input)
|
||||
// For now, we should only consider purely horizontal or purely vertical vents:
|
||||
.filter(v => v.start.x === v.end.x || v.start.y === v.end.y);
|
||||
|
||||
return Day5.countVentOverlaps(Day5.markVents(vents));
|
||||
}
|
||||
|
||||
part2(input: string[]): number | string {
|
||||
|
||||
const vents = Day5.parseInput(input)
|
||||
return Day5.countVentOverlaps(Day5.markVents(vents));
|
||||
}
|
||||
|
||||
private static markVents(vents: Vent[]): Map<string, number> {
|
||||
const grid = new Map<string, number>();
|
||||
vents.forEach(v => Day5.markVent(v, grid));
|
||||
return grid;
|
||||
}
|
||||
|
||||
private static markVent(vent: Vent, grid: Map<string, number>) {
|
||||
|
||||
// Must be a diagonal line
|
||||
const steps = Math.max(Math.abs(vent.end.x - vent.start.x), Math.abs(vent.end.y - vent.start.y));
|
||||
const stepX = Day5.determineStep(vent.start.x, vent.end.x);
|
||||
const stepY = Day5.determineStep(vent.start.y, vent.end.y);
|
||||
|
||||
for (let i = 0; i <= steps; i++) {
|
||||
Day5.addVentPos(new Vector2(vent.start.x + stepX * i, vent.start.y + stepY * i), grid);
|
||||
}
|
||||
}
|
||||
|
||||
private static determineStep(start: number, end: number): number {
|
||||
return start === end ? 0 : (start < end) ? 1 : -1;
|
||||
}
|
||||
|
||||
private static countVentOverlaps(grid: Map<string, number>): number {
|
||||
|
||||
let count = 0;
|
||||
for (const ventCount of grid.values()) {
|
||||
if (ventCount >= 2) count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
private static addVentPos(pos: Vector2, grid: Map<string, number>) {
|
||||
|
||||
const key = pos.key();
|
||||
const existing = grid.get(key);
|
||||
if (!existing) {
|
||||
grid.set(key, 1);
|
||||
} else {
|
||||
grid.set(key, existing + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private static parseInput(input: string[]): Vent[] {
|
||||
|
||||
const res: Vent[] = [];
|
||||
for (const line of input) {
|
||||
const startEnd = line.split(/\s*->\s*/);
|
||||
res.push({
|
||||
start: Vector2.parse(startEnd[0]),
|
||||
end: Vector2.parse(startEnd[1])
|
||||
});
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Vent {
|
||||
|
||||
readonly start: Vector2;
|
||||
readonly end: Vector2;
|
||||
|
||||
constructor(start: Vector2, end: Vector2) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default Day5;
|
||||
@@ -6,6 +6,7 @@ import Day1 from "./day1";
|
||||
import Day2 from "./day2";
|
||||
import Day3 from "./day3";
|
||||
import Day4 from "./day4";
|
||||
import Day5 from "./day5";
|
||||
|
||||
|
||||
// // Start writing Firebase Functions
|
||||
@@ -26,6 +27,7 @@ export const day = {
|
||||
2: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day2(), request, response) }),
|
||||
3: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day3(), request, response) }),
|
||||
4: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day4(), request, response) }),
|
||||
5: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day5(), request, response) }),
|
||||
|
||||
}
|
||||
|
||||
|
||||
23
functions/src/vector2.ts
Normal file
23
functions/src/vector2.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
class Vector2 {
|
||||
readonly x: number;
|
||||
readonly y: number;
|
||||
|
||||
constructor(x: number, y: number) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
static parse(input: string): Vector2 {
|
||||
const splitIndex = input.indexOf(",");
|
||||
const xString = input.substr(0, splitIndex);
|
||||
const yString = input.substr(splitIndex + 1);
|
||||
return new Vector2(parseInt(xString), parseInt(yString));
|
||||
}
|
||||
|
||||
key(): string {
|
||||
return `${this.x},${this.y}`;
|
||||
}
|
||||
}
|
||||
|
||||
export default Vector2;
|
||||
Reference in New Issue
Block a user