{
"id": "244",
"prompt": "A traffic light on a looping one-lane road: the light runs 180 frames green then 180 red straight off t. Four cars accelerate toward their top speed but obey two proportional limits: never faster than (gap to the car ahead)/24, and while the light is red, never faster than (distance to the stop line)/24 if they are within braking range - so a neat queue compresses at the line and pours through on green. Cars already past the line ignore it. Draw the stop line, the lamp housing with its red or green bulb, and speed-tinted cars.",
"solution_rl": "fn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let track: i32 = w * 8;\n let lx: i32 = w * 2 / 3;\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n let mut i: i32 = 0;\n while i < 4 {\n host::display::state_set(8 + i * 2, i * 400);\n host::display::state_set(9 + i * 2, 12);\n i = i + 1;\n }\n }\n let red: bool = t % 360 >= 180;\n let mut x = [0; 4];\n let mut v = [0; 4];\n let mut i: i32 = 0;\n while i < 4 {\n x[i] = host::display::state_get(8 + i * 2);\n v[i] = host::display::state_get(9 + i * 2);\n i = i + 1;\n }\n i = 0;\n while i < 4 {\n let a: i32 = (i + 1) % 4;\n let mut gap: i32 = x[a] - x[i];\n if gap < 0 { gap = gap + track; }\n gap = gap - 240;\n if gap < 0 { gap = 0; }\n v[i] = v[i] + 1;\n if v[i] > 18 { v[i] = 18; }\n if v[i] > gap / 24 { v[i] = gap / 24; }\n if red {\n let dist: i32 = lx * 8 - x[i];\n if dist > 0 && dist < 560 {\n if v[i] > dist / 24 { v[i] = dist / 24; }\n }\n }\n i = i + 1;\n }\n i = 0;\n while i < 4 {\n x[i] = x[i] + v[i];\n if x[i] >= track { x[i] = x[i] - track; }\n host::display::state_set(8 + i * 2, x[i]);\n host::display::state_set(9 + i * 2, v[i]);\n i = i + 1;\n }\n host::display::clear(0x0c0c10);\n let ry: i32 = h / 2;\n host::display::fill_rect(0, ry - 18, w, 36, 0x242424);\n host::display::fill_rect(lx, ry - 24, 3, 48, 0xd0d0d0);\n host::display::fill_rect(lx - 10, ry - 70, 24, 40, 0x303030);\n if red {\n host::display::fill_rect(lx - 4, ry - 66, 12, 12, 0xdd3030);\n } else {\n host::display::fill_rect(lx - 4, ry - 46, 12, 12, 0x30dd40);\n }\n i = 0;\n while i < 4 {\n let c: i32 = if v[i] >= 12 { 0x60b0ff } else if v[i] >= 4 { 0xddbb30 } else { 0xdd4030 };\n host::display::fill_rect(x[i] / 8, ry - 6, 26, 12, c);\n i = i + 1;\n }\n host::display::draw_string(8, 8, \"QUEUE AT THE RED\", 0x808080, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"traffic",
"queue",
"state"
]
}