{
"id": "243",
"prompt": "Make me a phantom traffic jam: five cars on a circular one-lane road (positions wrap at the screen edge), each accelerating by 1 toward its cruise speed but braking hard by 4 whenever the wrapped gap to the car ahead falls under 50px. While I hold the pointer, car 0 slams to a stop - release and watch the stop-and-go wave ripple backwards around the loop. Cars keep their cyclic order so car i always follows car (i+1)%5; color each car green, yellow or red by its current speed.",
"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 if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n let mut i: i32 = 0;\n while i < 5 {\n host::display::state_set(8 + i * 2, i * (track / 5));\n host::display::state_set(9 + i * 2, 16);\n i = i + 1;\n }\n }\n let mut x = [0; 5];\n let mut v = [0; 5];\n let mut i: i32 = 0;\n while i < 5 {\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 let brake: bool = host::display::pointer_down() == 1;\n i = 0;\n while i < 5 {\n let a: i32 = (i + 1) % 5;\n let mut gap: i32 = x[a] - x[i];\n if gap < 0 { gap = gap + track; }\n gap = gap - 224;\n if i == 0 && brake {\n v[i] = 0;\n } else if gap < 400 {\n v[i] = v[i] - 4;\n } else if v[i] < 20 {\n v[i] = v[i] + 1;\n }\n if v[i] < 0 { v[i] = 0; }\n if gap < 40 { v[i] = 0; }\n i = i + 1;\n }\n i = 0;\n while i < 5 {\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(0x101010);\n let ry: i32 = h / 2;\n host::display::fill_rect(0, ry - 20, w, 40, 0x282828);\n i = 0;\n while i < 5 {\n let c: i32 = if v[i] >= 16 { 0x40cc50 } else if v[i] >= 8 { 0xddbb30 } else { 0xdd4030 };\n host::display::fill_rect(x[i] / 8, ry - 6, 24, 12, c);\n i = i + 1;\n }\n host::display::draw_string(8, 8, \"HOLD = BRAKE CAR 0\", 0x808080, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"traffic",
"state",
"pointer"
]
}