개인용 복습공간

프로그래머스 - 크레인 인형뽑기 게임 본문

programmers

프로그래머스 - 크레인 인형뽑기 게임

taehwanis 2023. 10. 25. 12:56
board를 변환 후 shift()로 뽑아서 동일한 인형이면 pop() 시키기
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);
}

Comments