
const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : 'Wiki\\input.txt';
const inputs = fs
.readFileSync(path)
.toString()
.trim()
.split('\n')
.map((it) => it.split(' '))
.flat()
.filter((it) => it)
.slice(1)
.map((it) =>
Array(...it)
.reverse()
.join('')
)
.map(Number)
.sort((a, b) => a - b)
.join('\n');
console.log(inputs);
⏰ 소요한 시간 : -
지독한 메서드체이닝 .. .
fs.readFileSync(path)로 파일의 내용을 읽고,
toString()으로 파일의 내용을 문자열로 변환한 뒤,
trim() 으로 양 끝 공백을 제거하고,
split('\n')으로 줄바꿈 기준으로 배열로 변환하고,
map((it) => it.split(' '))으로 각 줄을 공백을 기준으로 나눈 후,
flat()으로 중첩 배열을 평탄화하며,
filter((it) => it)으로 빈 요소를 제거하고,
slice(1)로 첫 번째 요소를 제거한 뒤,
map((it) => Array(...it).reverse().join(''))으로 각 문자열을 배열로 만든 후 뒤집어 다시 문자열로 변환하고,
map(Number)로 각 문자열을 숫자로 변환하고,
sort((a, b) => a - b)로 숫자를 오름차순으로 정렬한 후,
join('\n')으로 각 숫자를 줄바꿈 문자를 포함해 하나의 문자열로 만든다.