Skip to content

Commit 18d13f8

Browse files
committed
day 10 part 2
1 parent e3fce69 commit 18d13f8

File tree

1 file changed

+65
-28
lines changed

1 file changed

+65
-28
lines changed

10/solve.js

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,66 @@
11
adventofcode.activate(10);
22

3-
adventofcode.day10_asteroids = [];
4-
53
adventofcode.day10_part1 = function(input) {
64
adventofcode.day10_asteroids = [];
75

86
adventofcode.day10_get_asteroids(input);
97

8+
return adventofcode.day10_max_seeing;
9+
};
10+
11+
adventofcode.day10_part2 = function(input) {
12+
adventofcode.day10_asteroids = [];
13+
14+
adventofcode.day10_get_asteroids(input);
15+
16+
// lets just sort all the y/x fractions of the best asteroid clockwise
17+
// the order is as follows:
18+
// -∞ -> +∞ for positive x, then
19+
// -∞ -> +∞ for negative x
20+
let list = adventofcode.day10_asteroids[adventofcode.day10_best_asteroid].losFractions.sort(function(a, b){
21+
// divisions by 0 suck... hence
22+
if (a[0] === 0)
23+
a[0] = 0.0001;
24+
25+
if (b[0] === 0)
26+
b[0] = 0.0001;
27+
28+
if (a[0] > 0) {
29+
if (b[0] > 0)
30+
return (a[1]/a[0] > b[1]/b[0]);
31+
else
32+
return -1;
33+
} else {
34+
if (b[0] < 0)
35+
return (a[1]/a[0] > b[1]/b[0]);
36+
else
37+
return 1;
38+
}
39+
});
40+
41+
// also we're lucky knowing we found more than 200 asteroids on first sight
42+
// so the laser doesn't have to wrap around and we don't need to calculate
43+
// all newly visible asteroids again
44+
45+
const asteroid_200 = adventofcode.day10_asteroids[list[199][2]];
46+
47+
return ( asteroid_200.x * 100 + asteroid_200.y);
48+
};
49+
50+
adventofcode.day10_get_asteroids = function(input) {
51+
input.split("\n").forEach((data, row) => {
52+
data.split("").forEach((value, col) => {
53+
if (value === '#') {
54+
adventofcode.day10_asteroids.push({
55+
x: col,
56+
y: row,
57+
seeing: 0, // number of other asteroids we see
58+
losFractions: [] // line of sight fractions and id of asteroid as third value
59+
});
60+
}
61+
});
62+
});
63+
1064
adventofcode.day10_asteroids.forEach((asteroid1, nr1) => {
1165
adventofcode.day10_asteroids.forEach((asteroid2, nr2) => {
1266
// don't check against ourself
@@ -29,37 +83,20 @@ adventofcode.day10_part1 = function(input) {
2983
adventofcode.day10_asteroids[nr1].seeing++;
3084
adventofcode.day10_asteroids[nr2].seeing++;
3185

32-
adventofcode.day10_asteroids[nr1].losFractions.push([diff_x, diff_y]);
33-
adventofcode.day10_asteroids[nr2].losFractions.push([-diff_x, -diff_y]);
86+
adventofcode.day10_asteroids[nr1].losFractions.push([diff_x, diff_y, nr2]);
87+
adventofcode.day10_asteroids[nr2].losFractions.push([-diff_x, -diff_y, nr1]);
3488
}
3589
});
3690
});
3791

38-
let max_seeing = 0;
39-
40-
adventofcode.day10_asteroids.forEach(asteroid => {
41-
max_seeing = Math.max(asteroid.seeing, max_seeing);
42-
});
43-
44-
return max_seeing;
45-
};
46-
47-
adventofcode.day10_part2 = function(input) {
48-
return 'maybe tomorrow';
49-
};
92+
adventofcode.day10_max_seeing = 0;
93+
adventofcode.day10_best_asteroid = 0;
5094

51-
adventofcode.day10_get_asteroids = function(input) {
52-
input.split("\n").forEach((data, row) => {
53-
data.split("").forEach((value, col) => {
54-
if (value === '#') {
55-
adventofcode.day10_asteroids.push({
56-
x: col,
57-
y: row,
58-
seeing: 0, // number of other asteroids we see
59-
losFractions: [] // line of sight fractions
60-
});
61-
}
62-
});
95+
adventofcode.day10_asteroids.forEach((asteroid, nr) => {
96+
if (adventofcode.day10_max_seeing < asteroid.seeing) {
97+
adventofcode.day10_max_seeing = asteroid.seeing;
98+
adventofcode.day10_best_asteroid = nr;
99+
}
63100
});
64101
};
65102

0 commit comments

Comments
 (0)