Traffic Light Simulation

Traffic Light Simulation

Hard PHP PHP Control Flow 23 views
Explanation Complexity

Problem Statement

Colors cycle: Red -> Green -> Yellow -> Red. Given start color and t steps, print final color.

Input Format

One line: startColor t.

Output Format

One word color.

Example

Red 4
Green

Constraints

0

Input / Output Format

Input Format
One line: startColor t.
Output Format
One word color.
Constraints
0

Examples

Input:
Red 4
Output:
Green

Example Solution (Public)

PHP
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$start,$t]=preg_split('/\\s+/', $inputText, 2);
$t=intval($t);
$colors=['Red','Green','Yellow'];
$idx=array_search($start,$colors,true);
if($idx===false) $idx=0;
$idx=($idx + ($t%3))%3;
echo $colors[$idx];
?>

Official Solution Code

<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$start,$t]=preg_split('/\\s+/', $inputText, 2);
$t=intval($t);
$colors=['Red','Green','Yellow'];
$idx=array_search($start,$colors,true);
if($idx===false) $idx=0;
$idx=($idx + ($t%3))%3;
echo $colors[$idx];
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.