코딩테스트 연습 - 크레인 인형뽑기 게임

def solution(board, moves):
    answer = 0
    
    box = []

    for m in moves:
        i = 0
        while i < len(board):
            if board[i][m - 1] != 0:  # if there is a toy in m column
                box.append(board[i][m - 1])
                board[i][m - 1] = 0

                break

            i += 1

        j = len(box) - 1
        if j > 0 and box[j] == box[j-1] and j < len(box):
            answer += 2
            box.pop(j - 1)
            box.pop(j - 1)
    
    return answer

def solution(board, moves):
    answer = 0
    
    box = []

    for m in moves:
        i = 0
        while i < len(board):
            if board[i][m - 1] != 0:  # if there is a toy in m column
                box.append(board[i][m - 1])
                board[i][m - 1] = 0

                break

            i += 1

        if j > 0 and box[j] == box[j-1] and j < len(box):
            answer += 2
            box.pop(-1)  # from the back
            box.pop(-1)  # from the back
    
    return answer