{
"id": "101",
"prompt": "Sorting-bin drag and drop: a token you grab only when the press starts on it (keep the grab offset so it drags naturally), and three numbered bins along the bottom. While dragging, the bin under the token center highlights; dropping over a bin snaps the token to that bin center and remembers IN BIN n in a state slot, dropping elsewhere leaves the token loose. Show which bin holds it.",
"solution_rl": "fn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n if host::display::state_get(6) == 0 {\n host::display::state_set(6, 1);\n host::display::state_set(0, w / 2 - 20);\n host::display::state_set(1, 60);\n }\n let down: i32 = host::display::pointer_down();\n let prev: i32 = host::display::state_get(5);\n let px: i32 = host::display::pointer_x();\n let py: i32 = host::display::pointer_y();\n let mut x: i32 = host::display::state_get(0);\n let mut y: i32 = host::display::state_get(1);\n let mut drag: i32 = host::display::state_get(2);\n if down == 1 && prev == 0 && px >= x && px < x + 40 && py >= y && py < y + 40 {\n drag = 1;\n host::display::state_set(3, px - x);\n host::display::state_set(4, py - y);\n }\n if drag == 1 && down == 1 {\n x = px - host::display::state_get(3);\n y = py - host::display::state_get(4);\n }\n let bw: i32 = w / 3;\n let bys: i32 = h - 100;\n let mut overbin: i32 = -1;\n if y + 20 >= bys {\n overbin = (x + 20) / bw;\n if overbin > 2 { overbin = 2; }\n }\n if drag == 1 && down == 0 {\n drag = 0;\n if overbin >= 0 {\n x = overbin * bw + bw / 2 - 20;\n y = bys + 30;\n host::display::state_set(7, overbin + 1);\n }\n }\n host::display::state_set(0, x);\n host::display::state_set(1, y);\n host::display::state_set(2, drag);\n host::display::state_set(5, down);\n host::display::clear(0x101010);\n let mut i: i32 = 0;\n while i < 3 {\n let mut bc: i32 = 0x303030;\n if drag == 1 && i == overbin { bc = 0x00aa66; }\n host::display::fill_rect(i * bw + 6, bys, bw - 12, 90, bc);\n host::display::fill_rect(i * bw + 9, bys + 3, bw - 18, 84, 0x101010);\n host::display::draw_number(i * bw + bw / 2 - 3, bys + 70, i + 1, 0x808080, 1);\n i = i + 1;\n }\n let stored: i32 = host::display::state_get(7);\n if stored > 0 && drag == 0 {\n host::display::draw_string(8, 8, \"IN BIN\", 0x00ff88, 1);\n host::display::draw_number(60, 8, stored, 0x00ff88, 1);\n } else {\n host::display::draw_string(8, 8, \"DRAG THE TOKEN INTO A BIN\", 0x808080, 1);\n }\n host::display::fill_rect(x, y, 40, 40, 0xffaa00);\n host::display::present();\n}\n",
"tags": [
"ui",
"pointer",
"state",
"drag-drop"
]
}