-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpart-two.js
64 lines (51 loc) · 1.61 KB
/
part-two.js
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
const fs = require('fs');
const path = require('path');
const distance = require('manhattan');
let raw_input = fs.readFileSync(path.resolve(__dirname, './input.txt'), 'utf8');
// Last filter is to remove any empty lines
let input = raw_input.split('\n').filter(n => n);
let coordinates = input.map(p => p.split(',').map(n => +n));
let flattened = coordinates.flat();
let largest = Math.max(...flattened);
let grid = Array(largest + 2)
.fill()
.map(n => {
return Array(largest + 2).fill(-1);
});
// Loop through grid and mark spots that are coordinates
coordinates.forEach((coord, index) => {
let [i, j] = coord;
grid[i][j] = 'C' + index;
});
const MIN_DIST = 10000;
let min_dist_count = 0;
for (let i = 0; i < grid.length; i++) {
let row = grid[i];
for (let j = 0; j < grid.length; j++) {
let cell = row[j];
if (typeof cell !== 'string') {
// Not a coord, measure it
let lengths = coordinates.map((coord, index) => {
return distance(coord, [i, j]);
});
let sum = lengths.reduce((a, b) => a + b);
if (sum < MIN_DIST) {
grid[i][j] = 'close';
min_dist_count++;
}
}
}
}
// Loop through coords, and find ones that are surrounded by "found" areas
coordinates.forEach(coord => {
let [i, j] = coord;
let t = grid[i - 1][j];
let r = grid[i][j + 1];
let b = grid[i + 1][j];
let l = grid[i][j - 1];
const C = 'close';
if (t === C && r === C && b === C && l === C) {
min_dist_count++;
}
});
console.log(min_dist_count);