-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpart-one.js
37 lines (31 loc) · 867 Bytes
/
part-one.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
const { input } = require('./input');
const { InfiniteGrid } = require('./infinite-grid');
const grid = new InfiniteGrid({
load: input,
parseAs: (cell) => {
return { value: parseInt(cell, 10) };
},
});
let count = 0;
// Naive O(n^2) way
for (let [cell_id, cell] of grid) {
let [x, y] = InfiniteGrid.toCoords(cell_id);
let row = grid.getRow(x, y, true);
let col = grid.getCol(x, y, true);
const rowIndex = row.indexOf(cell);
const colIndex = col.indexOf(cell);
let left = row.slice(0, rowIndex);
let right = row.slice(rowIndex + 1);
let top = col.slice(0, colIndex);
let down = col.slice(colIndex + 1);
if (
left.every((v) => v.value < cell.value) ||
right.every((v) => v.value < cell.value) ||
top.every((v) => v.value < cell.value) ||
down.every((v) => v.value < cell.value)
) {
// Cell is visible
count++;
}
}
console.log(count);