-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5question2.ts
More file actions
166 lines (128 loc) · 3.89 KB
/
day5question2.ts
File metadata and controls
166 lines (128 loc) · 3.89 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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 canDetermineIntermediatePoints() {
return this.isHorizontal() || this.isVertical() || this.isDiagonal();
}
public getPointsOnLine(): Point[] {
if (!this.canDetermineIntermediatePoints()) {
throw new Error("Not implemented for this type of line");
}
const pointsOnLine: Point[] = [];
if (this.isVertical()) {
const yPolarity = Math.sign(this.endingPoint.y - this.startingPoint.y);
for (
let i = 0;
i <= Math.abs(this.endingPoint.y - this.startingPoint.y);
i++
) {
pointsOnLine.push(
new Point(this.startingPoint.x, this.startingPoint.y + i * yPolarity)
);
}
} else if (this.isHorizontal()) {
const xPolarity = Math.sign(this.endingPoint.x - this.startingPoint.x);
for (
let i = 0;
i <= Math.abs(this.endingPoint.x - this.startingPoint.x);
i++
) {
pointsOnLine.push(
new Point(this.startingPoint.x + i * xPolarity, this.startingPoint.y)
);
}
} else if (this.isDiagonal()) {
const xPolarity = Math.sign(this.endingPoint.x - this.startingPoint.x);
const yPolarity = Math.sign(this.endingPoint.y - this.startingPoint.y);
for (
let i = 0;
i <= Math.abs(this.endingPoint.x - this.startingPoint.x);
i++
) {
pointsOnLine.push(
new Point(
this.startingPoint.x + i * xPolarity,
this.startingPoint.y + i * yPolarity
)
);
}
}
return pointsOnLine;
}
private isVertical() {
return this.startingPoint.x === this.endingPoint.x;
}
private isHorizontal() {
return this.startingPoint.y === this.endingPoint.y;
}
private isDiagonal() {
return (
Math.abs(this.endingPoint.x - this.startingPoint.x) ===
Math.abs(this.endingPoint.y - this.startingPoint.y)
);
}
}
let inputs: string[];
const rawData = readFileSync("./day5inputs.txt", "utf8");
inputs = rawData.split("\r\n");
let lines = buildLines(inputs);
let pointMap: Map<string, number> = new Map();
for (let line of lines.filter((l) => l.canDetermineIntermediatePoints())) {
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);
function buildLines(inputs: string[]) {
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));
}
return lines;
}