{
"id": "278",
"prompt": "Race two fetches: fire GETs at two different https URLs in the same init latch, then poll both handles every frame. The first handle whose ready() hits 1 takes a WINNER tag on its lane - decided once, in a state slot. Each lane shows a PENDING / READY / ERROR lamp and its upstream status code once ready; write one lane(y, handle, win, me) helper so both rows share the drawing code.",
"solution_rl": "fn lane(y: i32, h: i32, win: i32, me: i32) {\n let r: i32 = host::http::ready(h);\n if r == 1 {\n host::display::fill_rect(40, y, 12, 12, 0x22bb44);\n host::display::draw_string(64, y + 2, \"READY\", 0x22bb44, 1);\n host::display::draw_number(130, y, host::http::status(h), 0x66ddff, 2);\n } else if r < 0 {\n host::display::fill_rect(40, y, 12, 12, 0xbb2222);\n host::display::draw_string(64, y + 2, \"ERROR\", 0xbb2222, 1);\n } else {\n host::display::fill_rect(40, y, 12, 12, 0xaa7700);\n host::display::draw_string(64, y + 2, \"PENDING\", 0xaa7700, 1);\n }\n if win == me {\n host::display::draw_string(300, y + 2, \"WINNER\", 0xffffff, 2);\n }\n}\n\nfn frame(t: i32) {\n host::display::clear(0x0b0d11);\n if host::display::state_get(0) == 0 {\n host::display::state_set(1, host::http::get(\"https://example.com/\", 20));\n host::display::state_set(2, host::http::get(\"https://www.rust-lang.org/\", 26));\n host::display::state_set(0, 1);\n }\n let ha: i32 = host::display::state_get(1);\n let hb: i32 = host::display::state_get(2);\n if host::display::state_get(3) == 0 {\n if host::http::ready(ha) == 1 {\n host::display::state_set(3, 1);\n } else if host::http::ready(hb) == 1 {\n host::display::state_set(3, 2);\n }\n }\n let win: i32 = host::display::state_get(3);\n host::display::draw_string(140, 60, \"FETCH RACE\", 0xffffff, 2);\n host::display::draw_string(40, 140, \"LANE A\", 0x888888, 1);\n lane(170, ha, win, 1);\n host::display::draw_string(40, 260, \"LANE B\", 0x888888, 1);\n lane(290, hb, win, 2);\n if win == 0 {\n host::display::draw_string(40, 400, \"RACING...\", 0xaa7700, 2);\n }\n host::display::present();\n}\n",
"tags": [
"http",
"concurrency",
"functions"
]
}