{
"id": "241",
"prompt": "A rope of six nodes hanging from a fixed anchor at the top: every frame gravity tugs each free node down a few pixels, then three relaxation passes enforce a 30px link length between neighbors (position-based dynamics - measure each link with manhattan distance, split the correction between the two ends, re-pin node 0 to the anchor each pass). Grabbing within 26px of the tail node drags it with the pointer while held. Node positions live in state slots; draw the links with draw_line and highlight the tail.",
"solution_rl": "fn iabs(v: i32) -> i32 {\n if v < 0 { -v } else { v }\n}\n\nfn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let ax: i32 = w / 2;\n let ay: i32 = 60;\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n let mut i: i32 = 0;\n while i < 6 {\n host::display::state_set(8 + i * 2, ax);\n host::display::state_set(9 + i * 2, ay + i * 30);\n i = i + 1;\n }\n }\n let mut nx = [0; 6];\n let mut ny = [0; 6];\n let mut i: i32 = 0;\n while i < 6 {\n nx[i] = host::display::state_get(8 + i * 2);\n ny[i] = host::display::state_get(9 + i * 2);\n i = i + 1;\n }\n i = 1;\n while i < 6 {\n ny[i] = ny[i] + 3;\n i = i + 1;\n }\n let down: i32 = host::display::pointer_down();\n let px: i32 = host::display::pointer_x();\n let py: i32 = host::display::pointer_y();\n let mut grab: i32 = host::display::state_get(1);\n if down == 1 {\n if grab == 0 && iabs(px - nx[5]) < 26 && iabs(py - ny[5]) < 26 {\n grab = 1;\n }\n if grab == 1 {\n nx[5] = px;\n ny[5] = py;\n }\n } else {\n grab = 0;\n }\n host::display::state_set(1, grab);\n let mut pass: i32 = 0;\n while pass < 3 {\n nx[0] = ax;\n ny[0] = ay;\n i = 1;\n while i < 6 {\n let dx: i32 = nx[i] - nx[i - 1];\n let dy: i32 = ny[i] - ny[i - 1];\n let len: i32 = iabs(dx) + iabs(dy);\n if len > 0 {\n let ex: i32 = dx * (len - 30) / len;\n let ey: i32 = dy * (len - 30) / len;\n if i == 1 {\n nx[i] = nx[i] - ex;\n ny[i] = ny[i] - ey;\n } else {\n nx[i] = nx[i] - ex / 2;\n ny[i] = ny[i] - ey / 2;\n nx[i - 1] = nx[i - 1] + ex / 2;\n ny[i - 1] = ny[i - 1] + ey / 2;\n }\n }\n i = i + 1;\n }\n pass = pass + 1;\n }\n i = 0;\n while i < 6 {\n host::display::state_set(8 + i * 2, nx[i]);\n host::display::state_set(9 + i * 2, ny[i]);\n i = i + 1;\n }\n host::display::clear(0x101014);\n host::display::fill_rect(ax - 20, ay - 12, 40, 12, 0x505860);\n i = 1;\n while i < 6 {\n host::display::draw_line(nx[i - 1], ny[i - 1], nx[i], ny[i], 0x909090);\n i = i + 1;\n }\n i = 0;\n while i < 6 {\n let c: i32 = if i == 5 { 0xffb030 } else { 0xd0d0d0 };\n host::display::fill_rect(nx[i] - 4, ny[i] - 4, 8, 8, c);\n i = i + 1;\n }\n host::display::draw_string(8, 8, \"DRAG THE TAIL\", 0x606060, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"rope",
"pointer",
"state"
]
}