최대 1 분 소요

part17. 크레인 인형뽑기 게임

js ver 1.0

function solution(board, moves) {
    board = board.map((item, idx) => item.reduce((acc, el, lvl) => {
            if (board[lvl][idx] != 0) {
                acc.push(board[lvl][idx]);
            }
            return acc;
        }, []));

    let temp = [];
    return moves.reduce((rs, el) => {
        const picker = board[el - 1].shift();

        if(picker){
            if (temp[temp.length - 1] == picker) {
                temp.pop();
                rs += 2;
            }else{
                temp.push(picker);
            }
        }

        return rs;
    }, 0);
}


실행결과_js ver 1.0


업데이트: