11const distance = require ( 'manhattan' ) ;
22const input = require ( './input' ) ;
3+ // const input = require('./sample-input2');
34const cloned_input = JSON . parse ( JSON . stringify ( input ) ) ;
45
5- xs = cloned_input . map ( p => p [ 0 ] ) ;
6- ys = cloned_input . map ( p => p [ 1 ] ) ;
7- zs = cloned_input . map ( p => p [ 2 ] ) ;
6+ xs = cloned_input . map ( p => p . pos [ 0 ] ) ;
7+ ys = cloned_input . map ( p => p . pos [ 1 ] ) ;
8+ zs = cloned_input . map ( p => p . pos [ 2 ] ) ;
89
910const sortNum = ( a , b ) => {
1011 if ( a < b ) return - 1 ;
@@ -23,16 +24,42 @@ const min_y = ys[0];
2324const max_y = ys [ ys . length - 1 ] ;
2425
2526const min_z = zs [ 0 ] ;
26- const max_z = zs [ zs . length - 1 ] ;
27+ const max_z = zs [ zs . length - 1 ] ;
2728
28- let largest = cloned_input [ cloned_input . length - 1 ] ;
29+ let best_coord = {
30+ coord : [ null , null , null ] ,
31+ inRangeOf : - 1
32+ } ;
33+
34+ console . log ( `Running ${ ( ( max_x - min_x ) * ( max_y - min_y ) * ( max_z - min_z ) ) . toLocaleString ( ) } ` )
35+
36+
37+
38+ for ( let x = min_x ; x < max_x ; x ++ ) {
39+ for ( let y = min_y ; y < max_y ; y ++ ) {
40+ for ( let z = min_z ; z < max_z ; z ++ ) {
41+ let coord = [ x , y , z ] ;
42+
43+ let inRangeOf = 0 ;
44+ input . forEach ( bot => {
45+ let d = distance ( coord , bot . pos ) ;
46+ if ( d <= bot . r ) {
47+ inRangeOf ++ ;
48+ }
49+ } ) ;
50+
51+
52+ if ( inRangeOf > best_coord . inRangeOf ) {
53+ console . log ( `New best [${ x } , ${ y } , ${ z } ], in range of ${ inRangeOf } ` ) ;
2954
30- let in_range = 0 ;
31- for ( let i = 0 ; i < input . length - 1 ; i ++ ) {
32- let bot = input [ i ] ;
33- if ( distance ( bot . pos , largest . pos ) <= largest . r ) {
34- in_range ++ ;
55+ best_coord . coord [ 0 ] = x ;
56+ best_coord . coord [ 1 ] = y ;
57+ best_coord . coord [ 2 ] = z ;
58+ best_coord . inRangeOf = inRangeOf ;
59+ }
60+ }
3561 }
3662}
3763
38- console . log ( in_range ) ;
64+ console . log ( best_coord ) ;
65+ console . log ( distance ( best_coord . coord , [ 0 , 0 , 0 ] ) )
0 commit comments