write-ups-challenges-2021-2022/battleship/index.js

19 lines
545 B
JavaScript
Raw Permalink Normal View History

2021-12-02 23:33:26 +00:00
function makeBattleship(message, board) {
return message.split('').map(ch => {
if(board.flat().indexOf(ch) < 0) {
return ch;
} else {
const x = board.filter(row => row.indexOf(ch) >= 0).flat().indexOf(ch)
const y = board.findIndex(row => row.indexOf(ch) >= 0)
return [board[0][x], board[y][0]].join('')
}
}).join('')
}
console.log(makeBattleship(
'IGCTF{IMGOINGDOWNWITHTHESHIP}',
[['Q', 'W', 'E', 'R', 'T'], ['Y', 'U', 'I', 'O', 'P'], ['A', 'S', 'D', 'F', 'G'], ['H', 'K', 'L', 'Z', 'X'], ['C', 'V', 'B', 'N', 'M']]))