Algorithm/programmers

동영상 재생기 ( Level 1, JavaScript, PCCP 기출문제)

takeU 2025. 1. 4. 15:59
반응형
const timeToSeconds = (time) => {
    const [minutes, seconds] = time.split(':').map(Number);
    return minutes * 60 + seconds;
}

const secondsToTime = (seconds) => {
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = seconds % 60;
    return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
};

const solution = (video_len, pos, op_start, op_end, commands) => {
    const [start, end, len] = [timeToSeconds(op_start), timeToSeconds(op_end), timeToSeconds(video_len)]
    return secondsToTime(commands.reduce((cur, command) => {
        if (start <= cur && cur < end ) cur = end
        const next = cur + (command === 'next' ? 10 : -10)
        if (next < 0) return 0
        if (next > len) return len
        if (start <= next && next < end) return end
        return next
    }, timeToSeconds(pos)));
}