-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpart-one.js
49 lines (41 loc) · 925 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
38
39
40
41
42
43
44
45
46
47
48
49
const { input } = require('./input');
const { InfiniteGrid } = require('./infinite-grid');
const grid = new InfiniteGrid();
function* range(from, to) {
let step = to > from ? 1 : -1;
let steps = Math.abs(from - to) + 1;
let value = from;
while (steps--) {
yield value;
value += step;
}
}
for (let { from, to } of input) {
let [from_x, from_y] = from;
let [to_x, to_y] = to;
// For now, only consider horizontal and vertical lines
if (!(from_x === to_x || from_y === to_y)) {
continue;
}
if (from_x === to_x) {
let x = from_x;
for (let y of range(from_y, to_y)) {
let count = grid.get(x, y);
grid.set(x, y, count + 1);
}
}
if (from_y === to_y) {
let y = from_y;
for (let x of range(from_x, to_x)) {
let count = grid.get(x, y);
grid.set(x, y, count + 1);
}
}
}
let two_cells = 0;
for (let [id, value] of grid) {
if (value >= 2) {
two_cells++;
}
}
console.log(two_cells);