C: A traffic light

Move a traffic light through its states.

What to build

An enum used as the state of a simple machine.

Requirements

  • Define an enum with RED, GREEN and YELLOW, in that order.
  • The light starts at the state given, and changes steps times, going from red to green to yellow and back to red.
  • Print the name of the final state in lower case.

Input

One line with a whole number from 0 to 2, the starting state, and the number of steps.

Output

One word.

Constraints

The steps are from 0 to 1000000.

Example

Input:

0 1

Output:

green

Why it works

An enum makes the states readable, while its numeric values make the wrap-around a single use of %.

Examples

Example 1 · Input
0 1
Expected output
green
Example 2 · Input
1 1
Expected output
yellow

Hints

Hint 1

An enum's values are numbers, so % can wrap them around.

Hint 2

A switch or an array of names can turn the value into text.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

typedef enum { RED, GREEN, YELLOW } Light;

int main(void) {
    int start, steps;
    scanf("%d %d", &start, &steps);
    Light now = (start + steps) % 3;
    switch (now) {
    case RED:
        printf("red\n");
        break;
    case GREEN:
        printf("green\n");
        break;
    default:
        printf("yellow\n");
    }
    return 0;
}

Lesson: Constants, enums and macros →