1
1
adventofcode . activate ( 10 ) ;
2
2
3
- adventofcode . day10_asteroids = [ ] ;
4
-
5
3
adventofcode . day10_part1 = function ( input ) {
6
4
adventofcode . day10_asteroids = [ ] ;
7
5
8
6
adventofcode . day10_get_asteroids ( input ) ;
9
7
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
+
10
64
adventofcode . day10_asteroids . forEach ( ( asteroid1 , nr1 ) => {
11
65
adventofcode . day10_asteroids . forEach ( ( asteroid2 , nr2 ) => {
12
66
// don't check against ourself
@@ -29,37 +83,20 @@ adventofcode.day10_part1 = function(input) {
29
83
adventofcode . day10_asteroids [ nr1 ] . seeing ++ ;
30
84
adventofcode . day10_asteroids [ nr2 ] . seeing ++ ;
31
85
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 ] ) ;
34
88
}
35
89
} ) ;
36
90
} ) ;
37
91
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 ;
50
94
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
+ }
63
100
} ) ;
64
101
} ;
65
102
0 commit comments