{
"id": "175",
"prompt": "A stopwatch with a lap board: tapping the left half of the screen toggles run/stop, tapping the right half while running records a lap into the next of 4 lap slots (state slots - locals reset each frame). Show elapsed as seconds.centiseconds (centis = (frames % 60) * 100 / 60) and list the recorded laps as rows.",
"solution_rl": "fn pad2(x: i32, y: i32, v: i32, rgb: i32, sc: i32) {\n if v < 10 {\n host::display::draw_number(x, y, 0, rgb, sc);\n host::display::draw_number(x + sc * 8, y, v, rgb, sc);\n } else {\n host::display::draw_number(x, y, v, rgb, sc);\n }\n}\n\nfn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let down: i32 = host::display::pointer_down();\n let prev: i32 = host::display::state_get(2);\n let mut run: i32 = host::display::state_get(0);\n let mut f: i32 = host::display::state_get(1);\n let mut laps: i32 = host::display::state_get(3);\n if down == 1 && prev == 0 {\n if host::display::pointer_x() < w / 2 {\n run = 1 - run;\n } else {\n if run == 1 && laps < 4 {\n host::display::state_set(10 + laps, f);\n laps = laps + 1;\n }\n }\n }\n if run == 1 { f = f + 1; }\n host::display::state_set(0, run);\n host::display::state_set(1, f);\n host::display::state_set(2, down);\n host::display::state_set(3, laps);\n host::display::clear(0x000000);\n host::display::draw_string(8, 8, \"STOPWATCH\", 0x808080, 1);\n let col: i32 = if run == 1 { 0x00ff80 } else { 0xffffff };\n pad2(8, 28, f / 60, col, 4);\n host::display::fill_rect(74, 52, 4, 4, col);\n pad2(84, 28, (f % 60) * 100 / 60, col, 4);\n let mut i: i32 = 0;\n while i < laps {\n let lf: i32 = host::display::state_get(10 + i);\n let y: i32 = 90 + i * 22;\n host::display::draw_string(8, y, \"LAP\", 0x808080, 1);\n host::display::draw_number(40, y, i + 1, 0x808080, 1);\n pad2(70, y, lf / 60, 0xc0c0c0, 2);\n pad2(110, y, (lf % 60) * 100 / 60, 0xc0c0c0, 2);\n i = i + 1;\n }\n host::display::draw_string(8, h - 16, \"LEFT TAP RUN RIGHT TAP LAP\", 0x606060, 1);\n host::display::present();\n}\n",
"tags": [
"time",
"state",
"pointer",
"edge-detect"
]
}