Traffic Light Waiting Time
JavaScript
Medium
3 views
Problem Description
A traffic light has 3 phases: Red, Yellow, Green with fixed durations. You are given current phase (R/Y/G) and how many seconds are left in that phase. Find how many seconds you will wait to see Green (including 0 if already Green).
Input Format
One line: phase rem r y g.
Output Format
One integer wait seconds.
Official Solution
const fs=require('fs');const a=fs.readFileSync(0,'utf8').trim().split(/\\s+/);if(!a[0])process.exit(0);const phase=a[0];const rem=Number(a[1]);const r=Number(a[2]),y=Number(a[3]),g=Number(a[4]);let wait=0;if(phase==='G')wait=0;else if(phase==='Y')wait=rem;else wait=rem+y;process.stdout.write(String(wait));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!