-
Notifications
You must be signed in to change notification settings - Fork 0
/
05b.js
45 lines (37 loc) · 1.03 KB
/
05b.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
function solve(input) {
var cleaned = input.split("\n")
.map(x => x.split(" -> ")
.map(y => y.split(",")
.map(z => {
var i = parseInt(z);
return i;
})));
var points = {};
for(var i = 0; i < cleaned.length; i++) {
p1 = cleaned[i][0];
p2 = cleaned[i][1];
var x = p1[0], y = p1[1];
for(var j = 0; true; j++) {
var point = x + "," + y;
if(!points.hasOwnProperty(point)) {
points[point] = 0;
}
points[point] += 1;
if(x == p2[0] && y == p2[1]) {
break;
}
if(p1[0] < p2[0]) {
x++;
} else if(p1[0] > p2[0]) {
x--;
}
if(p1[1] < p2[1]) {
y++;
} else if(p1[1] > p2[1]) {
y--;
}
}
}
return Object.keys(points).filter(x => points[x] >= 2).length;
}
module.exports = solve;