1
1
import ISolution from "./ISolution.ts" ;
2
- import routine from "https://deno.land/x/routine/mod.ts" ;
3
2
4
3
export default class S2406 implements ISolution {
5
4
firstPart ( input : string ) : ( number | string ) | Promise < number | string > {
@@ -14,7 +13,7 @@ export default class S2406 implements ISolution {
14
13
}
15
14
}
16
15
}
17
- const poss : { x : number , y : number } [ ] = [ ] ;
16
+ const poss : { x : number , y : number } [ ] = [ ] ;
18
17
poss . push ( { x : currentPos . x , y : currentPos . y } ) ;
19
18
walk: while ( true ) {
20
19
switch ( direction ) {
@@ -69,11 +68,10 @@ export default class S2406 implements ISolution {
69
68
}
70
69
return poss . filter ( ( pos , index ) => poss . findIndex ( p => p . x === pos . x && p . y === pos . y ) === index ) . length ;
71
70
}
72
- async secondPart ( input : string ) : Promise < string | number > {
71
+ secondPart ( input : string ) {
73
72
const coords = input . split ( "\n" ) . map ( line => line . split ( "" ) ) ;
74
- function tryWith ( coords : string [ ] [ ] ) : number {
75
- const poss : { x : number , y : number } [ ] = [ ] ;
76
- const turn : { x : number , y : number , t : string } [ ] = [ ] ;
73
+ function tryWith ( coords : string [ ] [ ] ) : [ number , { x : number , y : number } [ ] ] {
74
+ const turn : { x : number , y : number , t : string } [ ] = [ ] ;
77
75
let direction = "up" ;
78
76
const currentPos = { x : 0 , y : 0 } ;
79
77
for ( const y in coords ) {
@@ -84,10 +82,11 @@ export default class S2406 implements ISolution {
84
82
}
85
83
}
86
84
}
85
+ const poss : { x : number , y : number } [ ] = [ ] ;
87
86
poss . push ( { x : currentPos . x , y : currentPos . y } ) ;
88
87
walk: while ( true ) {
89
88
if ( turn . some ( ( t , i ) => turn . findIndex ( tt => tt . x === t . x && tt . y === t . y && tt . t === t . t ) !== i ) ) {
90
- return 1 ;
89
+ return [ 1 , poss ] ;
91
90
}
92
91
switch ( direction ) {
93
92
case "up" :
@@ -99,6 +98,7 @@ export default class S2406 implements ISolution {
99
98
turn . push ( { x : currentPos . x , y : currentPos . y , t : "right" } ) ;
100
99
} else {
101
100
currentPos . y -- ;
101
+ poss . push ( { x : currentPos . x , y : currentPos . y } ) ;
102
102
}
103
103
break ;
104
104
case "right" :
@@ -110,6 +110,7 @@ export default class S2406 implements ISolution {
110
110
turn . push ( { x : currentPos . x , y : currentPos . y , t : "down" } ) ;
111
111
} else {
112
112
currentPos . x ++ ;
113
+ poss . push ( { x : currentPos . x , y : currentPos . y } ) ;
113
114
}
114
115
break ;
115
116
case "down" :
@@ -121,6 +122,7 @@ export default class S2406 implements ISolution {
121
122
turn . push ( { x : currentPos . x , y : currentPos . y , t : "left" } ) ;
122
123
} else {
123
124
currentPos . y ++ ;
125
+ poss . push ( { x : currentPos . x , y : currentPos . y } ) ;
124
126
}
125
127
break ;
126
128
case "left" :
@@ -132,23 +134,23 @@ export default class S2406 implements ISolution {
132
134
turn . push ( { x : currentPos . x , y : currentPos . y , t : "up" } ) ;
133
135
} else {
134
136
currentPos . x -- ;
137
+ poss . push ( { x : currentPos . x , y : currentPos . y } ) ;
135
138
}
136
139
break ;
137
140
138
141
}
139
142
}
140
- return 0 ;
143
+ return [ 0 , poss . filter ( ( pos , index ) => poss . findIndex ( p => p . x === pos . x && p . y === pos . y ) === index ) ] ;
141
144
}
145
+ const [ _ , fields ] = tryWith ( coords ) ;
142
146
const routines = [ ] ;
143
- for ( const y in coords ) {
144
- for ( const x in coords [ y ] ) {
145
- if ( coords [ y ] [ x ] === "." ) {
146
- coords [ y ] [ x ] = "#" ;
147
- routines . push ( routine ( tryWith , coords ) ) ;
148
- coords [ y ] [ x ] = "." ;
149
- }
147
+ for ( const { x, y } of fields ) {
148
+ if ( coords [ y ] [ x ] === "." ) {
149
+ coords [ y ] [ x ] = "#" ;
150
+ routines . push ( tryWith ( coords . copyWithin ( 0 , 0 ) ) ) ;
151
+ coords [ y ] [ x ] = "." ;
150
152
}
151
153
}
152
- return await Promise . all ( routines ) . then ( res => res . reduce ( ( a , b ) => a + b , 0 ) ) ;
154
+ return routines . reduce ( ( a , b ) => a + b [ 0 ] , 0 ) ;
153
155
}
154
156
}
0 commit comments