{
"id": "254",
"prompt": "Two big equal-mass balls bouncing around one box in 2-D. Walls reflect them; when their centers pass within the sum of radii (squared-distance check), swap BOTH velocity components between the balls - the equal-mass elastic shortcut - and push the pair apart along the dominant axis so they cannot stick together. Keep all eight quantities (x, y, vx, vy each) in state slots in 1/8-px fixed point. Print the sum of |v| components as ENERGY: swaps and reflections only permute and negate components, so the number should never change.",
"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 if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n host::display::state_set(8, 100 * 8);\n host::display::state_set(9, 100 * 8);\n host::display::state_set(10, 22);\n host::display::state_set(11, 14);\n host::display::state_set(12, 300 * 8);\n host::display::state_set(13, 260 * 8);\n host::display::state_set(14, -16);\n host::display::state_set(15, 10);\n }\n let mut ax: i32 = host::display::state_get(8);\n let mut ay: i32 = host::display::state_get(9);\n let mut avx: i32 = host::display::state_get(10);\n let mut avy: i32 = host::display::state_get(11);\n let mut bx: i32 = host::display::state_get(12);\n let mut by: i32 = host::display::state_get(13);\n let mut bvx: i32 = host::display::state_get(14);\n let mut bvy: i32 = host::display::state_get(15);\n ax = ax + avx;\n ay = ay + avy;\n bx = bx + bvx;\n by = by + bvy;\n if ax < 18 * 8 { ax = 18 * 8; avx = -avx; }\n if ax > (w - 18) * 8 { ax = (w - 18) * 8; avx = -avx; }\n if ay < 18 * 8 { ay = 18 * 8; avy = -avy; }\n if ay > (h - 18) * 8 { ay = (h - 18) * 8; avy = -avy; }\n if bx < 18 * 8 { bx = 18 * 8; bvx = -bvx; }\n if bx > (w - 18) * 8 { bx = (w - 18) * 8; bvx = -bvx; }\n if by < 18 * 8 { by = 18 * 8; bvy = -bvy; }\n if by > (h - 18) * 8 { by = (h - 18) * 8; bvy = -bvy; }\n let dx: i32 = bx / 8 - ax / 8;\n let dy: i32 = by / 8 - ay / 8;\n if dx * dx + dy * dy < 36 * 36 {\n let tx: i32 = avx;\n avx = bvx;\n bvx = tx;\n let ty: i32 = avy;\n avy = bvy;\n bvy = ty;\n if iabs(dx) > iabs(dy) {\n if dx > 0 { bx = ax + 36 * 8; } else { bx = ax - 36 * 8; }\n } else {\n if dy > 0 { by = ay + 36 * 8; } else { by = ay - 36 * 8; }\n }\n }\n host::display::state_set(8, ax);\n host::display::state_set(9, ay);\n host::display::state_set(10, avx);\n host::display::state_set(11, avy);\n host::display::state_set(12, bx);\n host::display::state_set(13, by);\n host::display::state_set(14, bvx);\n host::display::state_set(15, bvy);\n host::display::clear(0x0e1012);\n host::display::fill_rect(ax / 8 - 18, ay / 8 - 18, 36, 36, 0xe0a030);\n host::display::fill_rect(bx / 8 - 18, by / 8 - 18, 36, 36, 0x4090e0);\n let e: i32 = iabs(avx) + iabs(avy) + iabs(bvx) + iabs(bvy);\n host::display::draw_string(8, 8, \"ENERGY\", 0x707070, 1);\n host::display::draw_number(70, 8, e, 0xffffff, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"collision",
"state",
"conservation"
]
}