-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3.ts
118 lines (98 loc) · 2.94 KB
/
Day3.ts
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
import * as fs from 'fs';
class Solution {
private arr: number[][];
private size = 1000;
private rectangles: Rectangle[];
constructor(private lines: string[]) {
this.rectangles = this.lines.map(x => this.parse(x));
}
private contains(x: number, y: number, rectangle: Rectangle): boolean {
const point = rectangle.point;
if (x < point.x) {
return false;
}
if (y < point.y) {
return false;
}
if (x > point.x + rectangle.width - 1) {
return false;
}
if (y > point.y + rectangle.height - 1) {
return false;
}
return true;
}
private noOverlap(rectangle: Rectangle, arr: number[][]) {
let point = rectangle.point;
for (let column = point.y; column < point.y + rectangle.height; column++) {
for (let row = point.x; row < point.x + rectangle.width; row++) {
if (arr[row][column] !== 1) {
return false;
}
}
}
return true;
}
private parse(input: string): Rectangle {
const parts = input.split(/[\s:#]+/).filter(x => x.length != 0);
const coord = parts[2].split(',');
const point: Point = { x: +coord[0], y: +coord[1] };
const size = parts[3].split('x');
return {
id: +parts[0],
point: point,
width: +size[0],
height: +size[1]
};
}
part1(): number {
this.arr = [];
for (let row = 0; row < this.size; row++) {
this.arr[row] = [];
}
for (let row = 0; row < this.size; row++) {
for (let column = 0; column < this.size; column++) {
for (let rectangle of this.rectangles) {
const contains = this.contains(row, column, rectangle);
if (contains) {
if (this.arr[row][column]) {
this.arr[row][column]++;
}
else {
this.arr[row][column] = 1;
}
}
}
}
}
let result = 0;
for (let row = 0; row < this.size; row++) {
for (let column = 0; column < this.size; column++) {
if (this.arr[row][column] > 1) {
result++;
}
}
}
return result;
}
part2(): number {
for (let rectangle of this.rectangles) {
if (this.noOverlap(rectangle, this.arr)) {
return rectangle.id;
}
}
}
}
class Rectangle {
id: number;
point: Point;
width: number;
height: number;
}
class Point {
x: number;
y: number;
}
const sln = new Solution(fs.readFileSync('./Day3.txt').toString().split("\n"));
console.log(sln.part1());
console.log(sln.part2());