백준-Node.js-1284-집 주소

송철진·2023년 2월 11일
0

백준-Node.js

목록 보기
15/69

풀이

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

const solution = (input) => {
  for(let i = 0; i<input.length; i++){
    for(let j = 0; j<input[i].length; j++){
      input[i][j] === '1' ? input[i][j] = 2 
      : input[i][j] === '0' ? input[i][j] = 4 
      : input[i][j] = 3  
    }
    input[i] = input[i].reduce((a,b) => a+b, 0) + input[i].length + 1
  }
  
  return input.join('\n')
}

console.log(solution(input))

각 라인의 호수판 너비는;
2(양쪽 끝) + input[i].reduce((a,b) => a+b, 0)(각 숫자의 너비의 합) + input[i].length - 1(자간 너비의 합)
이므로 간단히 하면 다음과 같다.

input[i] = input[i].reduce((a,b) => a+b, 0) + input[i].length + 1
profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글