File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed
Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ const input = require ( 'fs' )
2+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
3+ . toString ( )
4+ . trim ( ) ;
5+
6+ function solution ( input ) {
7+ let size = input * 4 - 3 ;
8+ let arr = Array . from ( { length : size } , ( ) => Array ( size ) . fill ( ' ' ) ) ;
9+
10+ function star ( x , y , size ) {
11+ if ( size === 1 ) {
12+ arr [ x ] [ y ] = '*' ;
13+ return ;
14+ }
15+
16+ for ( let i = 0 ; i < size ; i ++ ) {
17+ arr [ x ] [ y + i ] = '*' ;
18+ arr [ x + i ] [ y ] = '*' ;
19+ arr [ x + size - 1 ] [ y + i ] = '*' ;
20+ arr [ x + i ] [ y + size - 1 ] = '*' ;
21+ }
22+ star ( x + 2 , y + 2 , size - 4 ) ;
23+ }
24+ star ( 0 , 0 , size ) ;
25+ return arr . map ( ( row ) => row . join ( '' ) ) . join ( '\n' ) ;
26+ }
27+
28+ console . log ( solution ( input ) ) ;
Original file line number Diff line number Diff line change 1+ const input = require ( 'fs' )
2+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
3+ . toString ( )
4+ . trim ( )
5+ . split ( '\n' )
6+ . map ( Number ) ;
7+
8+ function solution ( input ) {
9+ const result = [ ] ;
10+ for ( let i = 0 ; i < input . length ; i ++ ) {
11+ let n = 3 ** input [ i ] ;
12+ let str = '-' ;
13+ let arr = str . repeat ( n ) ;
14+
15+ function three ( arr ) {
16+ if ( arr . length === 1 ) {
17+ return str ;
18+ }
19+
20+ let size = arr . length / 3 ;
21+ const forward = three ( arr . slice ( 0 , size ) ) ;
22+ const backward = three ( arr . slice ( size * 2 ) ) ;
23+
24+ return forward + ' ' . repeat ( size ) + backward ;
25+ }
26+ result . push ( three ( arr ) ) ;
27+ }
28+ return result . join ( '\n' ) ;
29+ }
30+
31+ console . log ( solution ( input ) ) ;
You can’t perform that action at this time.
0 commit comments