본문 바로가기

백준

백준2980 - 도로와 신호등

https://www.acmicpc.net/problem/2980

 

2980번: 도로와 신호등

문제 상근이는 트럭을 가지고 긴 일직선 도로를 운전하고 있다. 도로에는 신호등이 설치되어 있다. 상근이는 각 신호등에 대해서 빨간 불이 지속되는 시간과 초록 불이 지속되는 시간을 미리 구

www.acmicpc.net

합성함수를 이용하여 풀어보았다. 신호등에 도착했을 때 신호등의 기본 반복시간(빨간불 + 초록불)으로 나누어서 빨간불의 대기 시간이면 현재 시간에서 빨간불 시간을 빼서 시간에 더하고 출발을 하면된다

//1. 합성함수를 이용하자
const readline = require('readline')

let N = 0, L = 0, cnt = 0
const input = []

const rl = readline.createInterface({
    output: process.stdout,
    input: process.stdin
})

const pipe = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value)

function parseValue(input) {
    return input.map(x => {
        return x.split(' ').map(y => {
            return parseInt(y)
        })
    })
}

function RGB_sort(input) {
    return input.sort((a, b) => {
        return a[0] - b[0]
    })
}

function time_solve(input) {
    let where = 0
    let time = 0

    while(where < L) {
        time++
        where++

        if(input.length !== 0 && input[0][0] === where) {
            const check = time % (input[0][1] + input[0][2])

            if(check <= input[0][1]) {
                time += (input[0][1] - check)
            }

            input.shift()
        }
    }

    return time
}

rl.on('line', (line) => {
    if(N === 0 && L === 0) {
        [N, L] = line.split(' ').map(x => { return parseInt(x) })
        cnt = N
    }
    else {
        input.push(line)
        cnt--
    }
    if(cnt === 0) {
        const res = pipe(
            x => parseValue(x),
            x => RGB_sort(x),
            x => time_solve(x)
        )(input)
        console.log(res)
        rl.close()   
    }
}).on('close', () => {
    process.exit()
})

module.exports = {

}

'백준' 카테고리의 다른 글

백준12904 - A와 B  (0) 2021.01.25
백준2685 - 님비합  (0) 2020.08.25
백준14562 - 태권왕  (0) 2020.08.06
백준4470 - 줄번호  (0) 2020.08.06
백준17249 - 태보태보 총난타  (0) 2020.08.06