-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday6.ts
101 lines (82 loc) · 2.94 KB
/
day6.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
import 'core-js';
class Point {
x: number;
y: number;
constructor(x, y) {
this.x = x;
this.y = y;
}
distance(p: Point): number {
return Math.abs(p.x - this.x) + Math.abs(p.y - this.y);
}
}
class Day6 {
public static Part1():number {
let w: number = 20; // 10000
let h: number = 20; // 10000
let grid: number[][] = Array(w).fill(-1).map(x => Array(h).fill(-1));
let points: Point[] = this.getPoints();
// Go through the entire grid and find the closest labeled points
for (let i: number = 0; i < w; i++) {
for (let j: number = 0; j < h; j++) {
// Find the distances from this point to all the labeled points
let distancesToPoint: number[] = points.map((point: Point) => {
return point.distance(new Point(i, j));
});
// Find the shortest distance
let min: number = this.getShortestDistance(distancesToPoint);
// Find all distances that equal the min while handling ties
// and set this point's value to the index of the closest labeled point
if (distancesToPoint.filter(dist => dist === min).length === 1) {
grid[i][j] = distancesToPoint.indexOf(min);
}
}
}
// Go through the grid again, this time looking around the borders
// Add the labels found here to the set of regions to ignore
let ignore: Set<number> = new Set();
for (let i: number = 0; i < w; i++) {
ignore.add(grid[0][i]);
ignore.add(grid[i][0]);
ignore.add(grid[grid[i].length - 1][i]);
ignore.add(grid[i][grid[i].length - 1]);
}
// Calculate the size of each labeled point region
let regionSizes: number[] = Array(points.length).fill(0);
for (let i: number = 0; i < w; i++) {
for (let j:number = 0; j < h; j++) {
if (!ignore.has(grid[i][j])) {
regionSizes[grid[i][j]]++;
}
}
}
// Find the largest region
let max = -1;
regionSizes.forEach(val => {
if (val > max) {
max = val;
}
});
return max;
}
public static getShortestDistance(arr: number[]): number {
let min = Number.MAX_SAFE_INTEGER;
arr.forEach(val => {
if (val < min) {
min = val;
}
});
return min;
}
public static getPoints():Point[] {
let testInput = ["1, 1", "1, 6", "8, 3", "3, 4", "5, 5", "8, 9"];
let points: Point[] = [];
testInput.forEach(point => {
let x = parseInt(point.split(", ")[0]);
let y = parseInt(point.split(", ")[1]);
points.push(new Point(x, y));
});
return points;
}
}
console.log(Day6.Part1());