File tree Expand file tree Collapse file tree 3 files changed +107
-0
lines changed
Expand file tree Collapse file tree 3 files changed +107
-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+ . split ( '\n' )
6+ . map ( ( a ) => a . split ( ' ' ) . map ( Number ) ) ;
7+
8+ function solution ( input ) {
9+ const n = input [ 0 ] [ 0 ] ;
10+ const arr = input . slice ( 1 ) ;
11+ const count = { '-1' : 0 , 0 : 0 , 1 : 0 } ;
12+
13+ function check ( x , y , size ) {
14+ const first = arr [ x ] [ y ] ;
15+ for ( let i = x ; i < x + size ; i ++ ) {
16+ for ( let j = y ; j < y + size ; j ++ ) {
17+ if ( arr [ i ] [ j ] !== first ) {
18+ return false ;
19+ }
20+ }
21+ }
22+ return true ;
23+ }
24+
25+ function divide ( x , y , size ) {
26+ if ( check ( x , y , size ) ) {
27+ count [ arr [ x ] [ y ] ] ++ ;
28+ } else {
29+ const newSize = size / 3 ;
30+ for ( let i = 0 ; i < 3 ; i ++ ) {
31+ for ( let j = 0 ; j < 3 ; j ++ ) {
32+ divide ( x + i * newSize , y + j * newSize , newSize ) ;
33+ }
34+ }
35+ }
36+ }
37+
38+ divide ( 0 , 0 , n ) ;
39+
40+ return `${ count [ '-1' ] } \n${ count [ '0' ] } \n${ count [ '1' ] } ` ;
41+ }
42+
43+ 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 ( ( a ) => a . split ( ' ' ) . map ( Number ) ) ;
7+
8+ function solution ( input ) {
9+ const n = input [ 0 ] [ 0 ] ;
10+ const arr = input . slice ( 1 ) ;
11+ const visited = Array . from ( { length : n } , ( ) => Array ( n ) . fill ( false ) ) ;
12+
13+ const directions = [
14+ [ 0 , 1 ] ,
15+ [ 1 , 0 ] ,
16+ ] ;
17+
18+ let isReachable = false ;
19+
20+ function dfs ( x , y ) {
21+ if ( x === n - 1 && y === n - 1 ) {
22+ isReachable = true ;
23+ return ;
24+ }
25+
26+ visited [ x ] [ y ] = true ;
27+ const step = arr [ x ] [ y ] ;
28+
29+ for ( const [ dx , dy ] of directions ) {
30+ const nx = x + dx * step ;
31+ const ny = y + dy * step ;
32+
33+ if (
34+ nx >= 0 &&
35+ ny >= 0 &&
36+ nx < n &&
37+ ny < n &&
38+ ! visited [ nx ] [ ny ] &&
39+ arr [ nx ] [ ny ] !== 0
40+ ) {
41+ dfs ( nx , ny ) ;
42+ }
43+ }
44+ }
45+
46+ dfs ( 0 , 0 ) ;
47+
48+ return isReachable ? 'HaruHaru' : 'Hing' ;
49+ }
50+
51+ console . log ( solution ( input ) ) ;
Original file line number Diff line number Diff line change 1+ function solution ( n , a , b ) {
2+ let round = 0 ;
3+
4+ while ( a !== b ) {
5+ a = Math . ceil ( a / 2 ) ;
6+ b = Math . ceil ( b / 2 ) ;
7+ round ++ ;
8+ }
9+
10+ return round ;
11+ }
12+
13+ console . log ( solution ( 8 , 4 , 7 ) ) ;
You can’t perform that action at this time.
0 commit comments