백준-Node.js-1100, 하얀 칸

송철진·2023년 2월 18일
0

백준-Node.js

목록 보기
29/69

풀이

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim()
                .split('\n').map(el => el.split(''))

const solution = input => {
  let count = 0;
  for(let i = 0; i < input.length; i++){
    if(i % 2 === 0){
      for(let j = 0; j < input[i].length; j+=2){
        if(input[i][j] === 'F') count++
      }  
    }else{
      for(let j = 1; j < input[i].length; j+=2){
        if(input[i][j] === 'F') count++
      }  
    }   
  }
  return count
}

console.log( solution(input) )

입력값의 각 라인의 index를 i, 라인의 각 칸의 index를 j 라고 한다.
ij가 0, 2, 4, 6 일 때와 1, 3, 5, 7 일 때 순회하여
값이 'F'면 count를 증가시키고
최종 count를 반환한다.

profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글