Created
December 5, 2021 07:25
-
-
Save shaun-jacks/7deb02ec12a24931c53be173e2af2c95 to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 5 Solution - Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from 'fs' | |
import * as path from 'path' | |
class Input { | |
buffer: Buffer | undefined; | |
parsed: number[][][] | undefined | |
readBuffer() { | |
this.buffer = fs.readFileSync(path.join(process.cwd(), 'day5', 'input', 'input.txt')) | |
} | |
parseBuffer() { | |
this.parsed = this.buffer?.toString().split("\n").filter(line => line !== "").map(element => | |
element.split(" -> ").map(pair => pair.split(",").map(num => parseInt(num, 10))) | |
) | |
} | |
filterStraightLines() { | |
return this.parsed?.filter(line => line[0][0] === line[1][0] || line[0][1] === line[1][1]) | |
} | |
} | |
class Point { | |
x = 0; | |
y = 0; | |
constructor(x: number, y: number) { | |
this.x = x | |
this.y = y | |
} | |
} | |
class Line { | |
start: Point = new Point(0, 0); | |
end: Point = new Point(0, 0); | |
points: Point[] = []; | |
constructor(p1: Point, p2: Point) { | |
this.start = p1; | |
this.end = p2; | |
if (this.start.y === this.end.y || this.start.x === this.end.x) { | |
this.initStraightPoints(); | |
} else { | |
this.initDiagonalPoints(); | |
} | |
} | |
initStraightPoints() { | |
this.points.push(this.start); | |
if (this.start.y === this.end.y) { | |
let tmp = this.start.x; | |
while (tmp !== this.end.x) { | |
if (this.start.x > this.end.x) { | |
tmp -= 1 | |
} else { | |
tmp += 1 | |
} | |
this.points.push(new Point(tmp, this.start.y)); | |
} | |
} | |
if (this.start.x === this.end.x) { | |
let tmp = this.start.y; | |
while (tmp !== this.end.y) { | |
if (this.start.y > this.end.y) { | |
tmp -= 1; | |
} else { | |
tmp += 1 | |
} | |
this.points.push(new Point(this.start.x, tmp)); | |
} | |
} | |
} | |
initDiagonalPoints() { | |
this.points.push(this.start); | |
let tmp = new Point(this.start.x, this.start.y); | |
// right direction | |
if (this.start.x < this.end.x) { | |
// down | |
if (this.start.y < this.end.y) { | |
while(tmp.x !== this.end.x && tmp.y !== this.end.y) { | |
tmp.x += 1; | |
tmp.y += 1; | |
this.points.push(new Point(tmp.x, tmp.y)) | |
} | |
// up | |
} else { | |
while(tmp.x !== this.end.x && tmp.y !== this.end.y) { | |
tmp.x += 1; | |
tmp.y -= 1; | |
this.points.push(new Point(tmp.x, tmp.y)) | |
} | |
} | |
// left direction | |
} else { | |
// down | |
if (this.start.y < this.end.y) { | |
while(tmp.x !== this.end.x && tmp.y !== this.end.y) { | |
tmp.x -= 1; | |
tmp.y += 1; | |
this.points.push(new Point(tmp.x, tmp.y)) | |
} | |
// up | |
} else { | |
while(tmp.x !== this.end.x && tmp.y !== this.end.y) { | |
tmp.x -= 1; | |
tmp.y -= 1; | |
this.points.push(new Point(tmp.x, tmp.y)) | |
} | |
} | |
} | |
} | |
} | |
class Graph { | |
xMax = 0; | |
yMax = 0; | |
lines: Line[] = []; | |
grid: number[][] = []; | |
constructor(lines: Line[]) { | |
this.lines = lines; | |
} | |
init() { | |
this.lines.forEach(line => { | |
this.xMax = Math.max(this.xMax, line.start.x, line.end.x); | |
this.yMax = Math.max(this.yMax, line.start.y, line.end.y); | |
}) | |
this.grid = Array.from(Array(this.yMax + 1), () => Array.from(Array(this.xMax + 1), () => 0)); | |
} | |
drawLines() { | |
this.lines.forEach(line => { | |
for (const point of line.points) { | |
this.grid[point.y][point.x] += 1 | |
} | |
}) | |
} | |
getOverlappingPoints() { | |
let count = 0; | |
for (let i = 0; i < this.grid.length; i++) { | |
for (let j = 0; j < this.grid[0].length; j++) { | |
if (this.grid[i][j] > 1) { | |
count += 1; | |
} | |
} | |
} | |
return count; | |
} | |
} | |
function main() { | |
const input = new Input(); | |
input.readBuffer() | |
input.parseBuffer() | |
const parsedP1Lines = input.filterStraightLines(); | |
const p1Lines: Line[] = [] | |
parsedP1Lines?.forEach(line => { | |
p1Lines.push(new Line( | |
new Point(line[0][0], line[0][1]), | |
new Point(line[1][0], line[1][1]) | |
)) | |
}) | |
const p1Graph = new Graph(p1Lines); | |
p1Graph.init() | |
p1Graph.drawLines(); | |
console.log("Part 1 Answer: ", p1Graph.getOverlappingPoints()) | |
const parsedP2Lines = input.parsed | |
const p2Lines: Line[] = [] | |
parsedP2Lines?.forEach(line => { | |
p2Lines.push(new Line( | |
new Point(line[0][0], line[0][1]), | |
new Point(line[1][0], line[1][1]) | |
)) | |
}) | |
const p2Graph = new Graph(p2Lines) | |
p2Graph.init(); | |
p2Graph.drawLines(); | |
console.log("Part 2 Answer: ", p2Graph.getOverlappingPoints()) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment