-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13question2.ts
More file actions
141 lines (106 loc) · 2.95 KB
/
day13question2.ts
File metadata and controls
141 lines (106 loc) · 2.95 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
import { readFileSync, writeFileSync } from "fs";
class Point {
private _x: number;
public get x(): number {
return this._x;
}
public set x(value: number) {
this._x = value;
}
private _y: number;
public get y(): number {
return this._y;
}
public set y(value: number) {
this._y = value;
}
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
class Paper {
private _points: Point[];
public get points(): Point[] {
return this._points;
}
protected set points(value: Point[]) {
this._points = value;
}
constructor(points: Point[]) {
this.points = points;
}
public fold(direction: string, position: number) {
const newPoints = [];
switch (direction) {
case "x": {
for (let point of this.points) {
if (point.x > position) {
let reflectedXPosition = position - Math.abs(position - point.x);
let existingReflectedPoint = this.points.find(
(p) => p.x === reflectedXPosition && p.y === point.y
);
if (existingReflectedPoint === undefined) {
newPoints.push(new Point(reflectedXPosition, point.y));
}
} else {
newPoints.push(point);
}
}
break;
}
case "y": {
for (let point of this.points) {
if (point.y > position) {
let reflectedYPosition = position - Math.abs(position - point.y);
let existingReflectedPoint = this.points.find(
(p) => p.x === point.x && p.y === reflectedYPosition
);
if (existingReflectedPoint === undefined) {
newPoints.push(new Point(point.x, reflectedYPosition));
}
} else {
newPoints.push(point);
}
}
break;
}
default: {
throw new Error("Not implemented.");
}
}
this.points = newPoints;
}
}
let inputs: string[];
const rawData = readFileSync("./day13inputs.txt", "utf8");
inputs = rawData.split("\r\n");
let points = [];
let i = 0;
while (inputs[i] !== "") {
let [x, y] = inputs[i].split(",");
points.push(new Point(Number(x), Number(y)));
i++;
}
const paper = new Paper(points);
i++;
for (; i < inputs.length; i++) {
let [foldDirection, foldPosition] = inputs[i].split(" along ")[1].split("=");
paper.fold(foldDirection, Number(foldPosition));
}
visualize(paper.points);
function visualize(points: Point[]) {
let asciiChart = "";
for (let i = 0; i < 1200; i++) {
for (let j = 0; j < 1200; j++) {
let point = points.find((p) => p.x === j && p.y === i);
if (point === undefined) {
asciiChart += " ";
} else {
asciiChart += "*";
}
}
asciiChart += "\n";
}
writeFileSync("./day13output.txt", asciiChart);
}