@@ -3,8 +3,76 @@ const fs = require('fs')
3
3
var Rawinput = fs . readFileSync ( "input.txt" ) . toString ( 'utf-8' )
4
4
input = Rawinput . split ( "\r\n" ) ;
5
5
function Run ( ) {
6
-
6
+ var Caves = { } ;
7
+ input . forEach ( function ( line ) {
8
+ var Points = line . split ( "-" ) ;
9
+ var from = Points [ 0 ] ;
10
+ var to = Points [ 1 ] ;
11
+ Caves [ from ] = Caves [ from ] || [ ] ;
12
+ Caves [ from ] . push ( to ) ;
13
+ Caves [ to ] = Caves [ to ] || [ ] ;
14
+ if ( ! Caves [ to ] . includes ( from ) ) {
15
+ Caves [ to ] . push ( from ) ;
16
+ }
17
+ } )
18
+ var DonePaths = [ ] ;
19
+ var AllDone = false ;
20
+ var Paths = GetNext ( Caves , "start" ) ;
21
+ Paths . forEach ( function ( path ) {
22
+ DonePaths = SolvePath ( path , DonePaths , Caves ) ;
23
+ } )
24
+ console . log ( DonePaths . length ) ;
7
25
}
26
+
27
+ var DonePaths = [ ] ;
28
+ function SolvePath ( path , alreadyEnded , Caves ) {
29
+ if ( path . endsWith ( "end" ) && ! DonePaths . includes ( path ) ) {
30
+ DonePaths . push ( path ) ;
31
+ }
32
+ else {
33
+ var next = GetNext ( Caves , path ) ;
34
+ next . forEach ( function ( nextPath ) {
35
+ var Paths = SolvePath ( nextPath , alreadyEnded , Caves ) ;
36
+ } )
37
+ }
38
+ return DonePaths ;
39
+ }
40
+
41
+ function GetNext ( Caves , CurrentPath ) {
42
+ var NextPaths = [ ] ;
43
+ var smallCave = / [ a - z ] (? = , | $ ) / g;
44
+ var CurrentNode = CurrentPath . split ( "," ) . pop ( ) ;
45
+ Caves [ CurrentNode ] . forEach ( function ( node ) {
46
+ var maxAmount = 1 ;
47
+ if ( CurrentPath != "start" ) {
48
+ var FilteredPath = CurrentPath . replace ( "start" , "" )
49
+ var smallAmount = FilteredPath . match ( smallCave )
50
+ if ( smallAmount != null ) {
51
+ smallAmount . forEach ( function ( smalls ) {
52
+ var amount = FilteredPath . match ( new RegExp ( smalls , "g" ) ) . length
53
+ maxAmount = amount < maxAmount ? maxAmount : amount ;
54
+ } )
55
+ }
56
+ }
57
+ if ( node != "start" ) {
58
+ if ( smallCave . test ( node ) ) {
59
+ if ( ! CurrentPath . includes ( node ) ) {
60
+ NextPaths . push ( CurrentPath + "," + node ) ;
61
+ }
62
+ // else if (maxAmount < 2) {
63
+ // NextPaths.push(CurrentPath + "," + node);
64
+ // }
65
+ }
66
+ else {
67
+ NextPaths . push ( CurrentPath + "," + node ) ;
68
+ }
69
+ }
70
+ } )
71
+ return NextPaths ;
72
+ }
73
+
74
+
75
+
8
76
module . exports = Run ;
9
77
//Start timer
10
78
var startTime = new Date ( ) . getTime ( ) ;
0 commit comments