-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.js
88 lines (82 loc) · 2.42 KB
/
day10.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require("fs");
fs.readFile("./day10_input", "utf8", function(err, contents) {
const flatten = arr => {
return arr.reduce(function(flat, toFlatten) {
if (!Array.isArray(toFlatten) && toFlatten.p === ".") return flat;
return flat.concat(
Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten
);
}, []);
};
const input = flatten(
contents.split("\n").map((l, y) =>
l.split("").map((p, x) => {
return { x, y, p };
})
)
);
const visible = ({ asteroid, asteroids }) => {
const jsonAsteroid = JSON.stringify({ x: 0, y: 0, p: "#" });
const relativeAsteroids = asteroids
.map(a => {
return { ...a, x: a.x - asteroid.x, y: a.y - asteroid.y };
})
.filter(ast => jsonAsteroid !== JSON.stringify(ast));
return [
...new Set(
relativeAsteroids.map(
a => Math.round(Math.atan2(a.y, a.x) * 1000000) / 1000000
)
)
].length;
};
const inRange = ({ asteroid, asteroids }) => {
const jsonAsteroid = JSON.stringify({ x: 0, y: 0, p: "#" });
const relativeAsteroids = asteroids
.map(a => {
return { ...a, x: a.x - asteroid.x, y: a.y - asteroid.y };
})
.filter(ast => jsonAsteroid !== JSON.stringify(ast));
const asteroidsMap = new Map();
relativeAsteroids.forEach(a => {
const angle = Math.round(Math.atan2(a.y, a.x) * 1000000) / 1000000;
const angleKey = angle.toString();
if (asteroidsMap.has(angleKey))
asteroidsMap.set(angleKey, [...asteroidsMap.get(angleKey), a]);
else asteroidsMap.set(angleKey, [a]);
});
return { asteroid, asteroidsMap, length: asteroidsMap.size };
};
const distributedAsteroids = input.reduce(
(acc, a) => [
...acc,
{
asteroid: { ...a },
asteroids: [...input]
}
],
[]
);
const distributedMap = distributedAsteroids.map(o => inRange(o));
const { asteroid, asteroidsMap, length } = distributedMap.reduce(function(
prev,
current
) {
return prev.length > current.length ? prev : current;
});
let shoot = true;
let i = 0;
let asteroidShot;
while (shoot) {
asteroidsMap.forEach(l => {
if (l.length > 0) {
asteroidShot = l.splice(0, 1);
i++;
if (i === 200) {
shoot = false;
}
}
});
}
console.log((asteroidShot[0].x + asteroid.x) * 100 + (asteroidShot[0].y + asteroid.y));
});