@@ -2,8 +2,97 @@ const fs = require('fs')
2
2
3
3
var Rawinput = fs . readFileSync ( "input.txt" ) . toString ( 'utf-8' )
4
4
input = Rawinput . split ( "\r\n" ) ;
5
+ for ( let i = 0 ; i < input . length ; i ++ ) {
6
+ const element = input [ i ] ;
7
+ input [ i ] = element . split ( "" ) . map ( Number ) ;
8
+ }
5
9
function Run ( ) {
6
-
10
+ var Basins = [ ] ;
11
+ input . forEach ( ( row , x ) => {
12
+ row . forEach ( ( col , y ) => {
13
+ if ( IsLowest ( x , y ) != 0 ) {
14
+ Basins . push ( GetBasin ( x , y ) ) ;
15
+ }
16
+ } ) ;
17
+ } ) ;
18
+ Basins = Basins . sort ( function ( a , b ) { return b - a } )
19
+ console . log ( Basins [ 0 ] * Basins [ 1 ] * Basins [ 2 ] ) ;
20
+ }
21
+ function IsLowest ( x , y ) {
22
+ var Input = input [ x ] [ y ] ;
23
+ var Adjacents = [
24
+ x == 0 ? 99 : input [ x - 1 ] [ y ] ,
25
+ x == input . length - 1 ? 99 : input [ x + 1 ] [ y ] ,
26
+ y == 0 ? 99 : input [ x ] [ y - 1 ] ,
27
+ y == input [ x ] . length - 1 ? 99 : input [ x ] [ y + 1 ]
28
+ ]
29
+ if ( Input < Adjacents . sort ( ) [ 0 ] ) {
30
+ return 1 + Input ;
31
+ }
32
+ return 0 ;
33
+ }
34
+ function GetBasin ( x , y ) {
35
+ var Points = [ { x : x , y : y } ] ;
36
+ var StartingPoint = input [ x ] [ y ] ;
37
+ var OnlyNines = false ;
38
+ while ( ! OnlyNines ) {
39
+ var OnlyNines = true ;
40
+ Points . forEach ( point => {
41
+ //Check Up
42
+ if ( point . y != 0 && input [ point . x ] [ point . y - 1 ] != 9 ) {
43
+ var AlreadyIn = false
44
+ Points . forEach ( point2 => {
45
+ if ( point2 . x == point . x && point2 . y == point . y - 1 ) {
46
+ AlreadyIn = true ;
47
+ }
48
+ } ) ;
49
+ if ( ! AlreadyIn ) {
50
+ OnlyNines = false ;
51
+ Points . push ( { x : point . x , y : point . y - 1 } ) ;
52
+ }
53
+ }
54
+ //Check Down
55
+ if ( point . y + 1 != input [ 0 ] . length && input [ point . x ] [ point . y + 1 ] != 9 && ! Points . includes ( { x : point . x , y : point . y + 1 } ) ) {
56
+ var AlreadyIn = false
57
+ Points . forEach ( point2 => {
58
+ if ( point2 . x == point . x && point2 . y == point . y + 1 ) {
59
+ AlreadyIn = true ;
60
+ }
61
+ } ) ;
62
+ if ( ! AlreadyIn ) {
63
+ OnlyNines = false ;
64
+ Points . push ( { x : point . x , y : point . y + 1 } ) ;
65
+ }
66
+ }
67
+ //Check Left
68
+ if ( point . x != 0 && input [ point . x - 1 ] [ point . y ] != 9 && ! Points . includes ( { x : point . x + 1 , y : point . y } ) ) {
69
+ var AlreadyIn = false
70
+ Points . forEach ( point2 => {
71
+ if ( point2 . x == point . x - 1 && point2 . y == point . y ) {
72
+ AlreadyIn = true ;
73
+ }
74
+ } ) ;
75
+ if ( ! AlreadyIn ) {
76
+ OnlyNines = false ;
77
+ Points . push ( { x : point . x - 1 , y : point . y } ) ;
78
+ }
79
+ }
80
+ //Check Right
81
+ if ( point . x + 1 != input . length && input [ point . x + 1 ] [ point . y ] != 9 && ! Points . includes ( { x : point . x + 1 , y : point . y } ) ) {
82
+ var AlreadyIn = false
83
+ Points . forEach ( point2 => {
84
+ if ( point2 . x == point . x + 1 && point2 . y == point . y ) {
85
+ AlreadyIn = true ;
86
+ }
87
+ } ) ;
88
+ if ( ! AlreadyIn ) {
89
+ OnlyNines = false ;
90
+ Points . push ( { x : point . x + 1 , y : point . y } ) ;
91
+ }
92
+ }
93
+ } )
94
+ }
95
+ return Points . length ;
7
96
}
8
97
module . exports = Run ;
9
98
//Start timer
0 commit comments