Skip to content

browserbase/coding-exercises-traffic-light

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Traffic Light

Your task is to write a program that controls a traffic light at the intersection of Browser Way (BW) and Stagehand Street (SS). Your program owns the intersection's state—deciding when each street turns green, yellow, or red—and controls the light over UDP, sending color changes on port 10000:

┌───────────────────┐                         ┌───────────────────┐
│     Your Code     │        port 10000       │  Traffic Light    │
│                   │      ────────────►      │                   │
│      1 ──► 2      │       BW-R, SS-G...     │         R         │
│      ▲     │      │                         │       +---+       │
│      │     ▼      │                         │     R |   | R     │
│      4 ◄── 3      │                         │       +---+       │
│                   │                         │         R         │
└───────────────────┘                         └───────────────────┘

At any moment, one street has a green or yellow light while the other holds red. The active street stays green for ten seconds, changes to yellow for five, then yields to the cross street. Four states, looping forever. The state machine looks like this:

        State 1      State 2      State 3      State 4
         ┌───┐        ┌───┐        ┌───┐        ┌───┐
         │ R │        │ R │        │ · │        │ · │
   BW:   │ · │        │ · │        │ · │        │ Y │
         │ · │        │ · │        │ G │        │ · │
         └───┘        └───┘        └───┘        └───┘
         ┌───┐        ┌───┐        ┌───┐        ┌───┐
         │ · │        │ · │        │ R │        │ R │
   SS:   │ · │        │ Y │        │ · │        │ · │
         │ G │        │ · │        │ · │        │ · │
         └───┘        └───┘        └───┘        └───┘
          ── 10s ─►   ── 5s ─►   ── 10s ─►   ── 5s ─► ↺

The traffic light's top and bottom faces show Browser Way's color; its left and right faces show Stagehand Street's. Browser Way runs north–south, Stagehand Street east–west:

         BW
         │
    SS ──┼── SS
         │
         BW

Running the Traffic Light

The traffic light runs as a separate process:

% python light_server.py 10000

Your program should send color changes to port 10000 over UDP. You can set a street's color by sending one of six messages: BW-R, BW-Y, BW-G, SS-R, SS-Y, SS-G, where BW and SS are short for "Browser Way" and "Stagehand Street" respectively, and R, Y, G are short for "Red", "Yellow", and "Green".

Each message controls a single street. To flip BW to green and SS to red at the same time, send two separate messages.

The light has no safety logic—it will happily show green in both directions. Don't let it.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages