File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-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 ( ( el ) => el . split ( ' ' ) ) ;
7+
8+ function solution ( input ) {
9+ const [ N , M ] = input [ 0 ] . map ( Number ) ;
10+ const prevChessBoard = input . slice ( 1 ) . map ( ( el ) => el [ 0 ] . split ( '' ) ) ;
11+
12+ function checkChessBoard ( chessBoard ) {
13+ let whiteFirst = 0 ;
14+ let blackFirst = 0 ;
15+ for ( let i = 0 ; i < 8 ; i ++ ) {
16+ for ( let j = 0 ; j < 8 ; j ++ ) {
17+ const currentChessBlock = chessBoard [ i ] [ j ] ;
18+ if ( ( i + j ) % 2 === 0 ) {
19+ if ( currentChessBlock === 'W' ) {
20+ blackFirst ++ ;
21+ } else {
22+ whiteFirst ++ ;
23+ }
24+ } else {
25+ if ( currentChessBlock === 'B' ) {
26+ blackFirst ++ ;
27+ } else {
28+ whiteFirst ++ ;
29+ }
30+ }
31+ }
32+ }
33+ return Math . min ( whiteFirst , blackFirst ) ;
34+ }
35+
36+ let minRepaint = Infinity ;
37+
38+ for ( let i = 0 ; i <= N - 8 ; i ++ ) {
39+ for ( let j = 0 ; j <= M - 8 ; j ++ ) {
40+ const standardChessBoard = prevChessBoard
41+ . slice ( i , i + 8 )
42+ . map ( ( row ) => row . slice ( j , j + 8 ) ) ;
43+ const repaint = checkChessBoard ( standardChessBoard ) ;
44+ minRepaint = Math . min ( minRepaint , repaint ) ;
45+ }
46+ }
47+
48+ return minRepaint ;
49+ }
50+
51+ console . log ( solution ( input ) ) ;
You can’t perform that action at this time.
0 commit comments