-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5question1.ts
More file actions
131 lines (98 loc) · 3.01 KB
/
day5question1.ts
File metadata and controls
131 lines (98 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { readFileSync } from "fs";
class Point {
private _x: number;
public get x(): number {
return this._x;
}
protected set x(value: number) {
this._x = value;
}
private _y: number;
public get y(): number {
return this._y;
}
protected set y(value: number) {
this._y = value;
}
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
public encode() {
return `${this.x},${this.y}`;
}
}
class Line {
protected startingPoint: Point;
protected endingPoint: Point;
constructor(startingPoint: Point, endingPoint: Point) {
this.startingPoint = startingPoint;
this.endingPoint = endingPoint;
}
public isVerticalOrHorizontal() {
return (
this.startingPoint.y === this.endingPoint.y ||
this.startingPoint.x === this.endingPoint.x
);
}
public getPointsOnLine(): Point[] {
if (!this.isVerticalOrHorizontal()) {
throw new Error("Not implemented for diagonal lines");
}
const pointsOnLine: Point[] = [];
if (this.startingPoint.x === this.endingPoint.x) {
let [smallerPoint, biggerPoint] = this.orderBySize(
this.startingPoint.y,
this.endingPoint.y
);
for (let i = smallerPoint; i <= biggerPoint; i++) {
pointsOnLine.push(new Point(this.startingPoint.x, i));
}
} else if (this.startingPoint.y === this.endingPoint.y) {
let [smallerPoint, biggerPoint] = this.orderBySize(
this.startingPoint.x,
this.endingPoint.x
);
for (let i = smallerPoint; i <= biggerPoint; i++) {
pointsOnLine.push(new Point(i, this.startingPoint.y));
}
}
return pointsOnLine;
}
private orderBySize(coordinate1, coordinate2) {
return coordinate1 < coordinate2
? [coordinate1, coordinate2]
: [coordinate2, coordinate1];
}
}
let inputs: string[];
const rawData = readFileSync("./day5inputs.txt", "utf8");
inputs = rawData.split("\r\n");
let lines: Line[] = [];
for (let input of inputs) {
let [rawStartingPoint, rawEndingPoint] = input.split(" -> ");
let [rawStartingPointXValue, rawStartingPointyValue] =
rawStartingPoint.split(",");
let [rawEndingPointXValue, rawEndingPointyValue] = rawEndingPoint.split(",");
let startingPoint = new Point(
Number(rawStartingPointXValue),
Number(rawStartingPointyValue)
);
let endingPoint = new Point(
Number(rawEndingPointXValue),
Number(rawEndingPointyValue)
);
lines.push(new Line(startingPoint, endingPoint));
}
let pointMap: Map<string, number> = new Map();
for (let line of lines.filter((l) => l.isVerticalOrHorizontal())) {
for (let point of line.getPointsOnLine()) {
let encodedPoint = point.encode();
pointMap.set(
encodedPoint,
pointMap.has(encodedPoint) ? pointMap.get(encodedPoint) + 1 : 1
);
}
}
const result = Array.from(pointMap.values()).filter((v) => v >= 2).length;
console.log(result);