Reject rounded sums of wide binary strings

YA
16m ago
+298-1

Problem and behavior

The five original Wide Register Arithmetic (add-binary) cases use binary strings of at most four bits. A JavaScript solution that adds parseInt(a, 2) and parseInt(b, 2) therefore passes 5/5, even though it rounds 2^53 + 1 to 2^53. This input is valid under the problem's 10,000-character constraint.

Approach

Append five cases to cover precision loss, fixed-width truncation and the maximum input length:

  • wide value plus one: 2^53 + 1, the case accepted in the issue, rejects addition through Number.
  • wide odd value plus zero: preserves 2^53 + 1 unchanged, rejecting a solution that converts an already rounded Number to BigInt.
  • wide all ones plus one: 128 ones plus one requires a 129-bit result, rejecting both 64-bit and unsigned 128-bit truncation. The modeled wrong implementation uses 64 bits; a comment explains the stronger case.
  • two wide values with multi-bit carry: unequal 64-bit and 56-bit addends require a 65-bit exact result, rejecting conversion of only the shorter operand through Number.
  • maximum length all ones plus one: 10,000 ones plus one requires a 10,001-bit result, rejecting the reported Python solution that recurses for each character and exceeds the default recursion limit.

The arithmetic regression in tests/browser/compiler-explorer.test.js runs five wrong implementations by name and looks up the case each must fail by label. The reported parseInt solution is the only Number-based addition variant; the equivalent binary-literal conversion is omitted. A separate regression rejects preserving only the longer operand exactly, which passes all eight earlier cases, and confirms that the new addends both exceed 53 bits and differ in length. The same wide case must also reject an adder that writes 0 when a column sums to 3, so later fixture edits cannot silently remove that coverage. Bit-by-bit carry and direct BigInt addition match every expected value.

Repeatable tests also run correct solutions through the generated C, C++ and Java harnesses in tests/browser/compiler-explorer.test.js and the Python driver in tests/browser/python-driver.test.js. Each test reads the served judge, requires a result wider than 64 bits, checks that every case ran without error, and compares every returned string with its expected value.

A named regression in tests/browser/python-driver.test.js requires the recursive candidate to pass the first nine cases and return RecursionError on the maximum-length case, looked up by label. The correct Python candidate now uses iterative per-digit carry, following the problem's clarification against converting whole operands to integers.

The cases are appended so existing example indexes remain unchanged. Their labels join the contract checks in tests/web/contract.rs; web/judges/wide-register-arithmetic.json is regenerated, and the add-binary digest in tests/golden/task-posed.json is updated.

Validation

  • OpenJDK and javac 21.0.13.1 installed and used locally.
  • Focused add-binary regressions and language-driver tests: 11 passed, 0 skipped. Each language's correct solution passes all ten judge cases; the reported Number solution passes 6/10.
  • Before adding the maximum-length case, the new recursion regression fails on its missing label. With the case, the recursive candidate passes 9/10 and returns RecursionError on the pinned maximum-length input.
  • ./scripts/test.sh: passed locally (exit 0). Rust: 3,066 passed, 29 ignored. Browser Node suite: 2,349 passed, 0 failed, 1,443 skipped. Git hooks: 498 checks passed.

Local coverage limits: the gate skipped three server/browser checks, Chromium/Playwright and ffmpeg-dependent tests, desktop clippy/tests (missing WebKitGTK development package), one Python authoring probe outside a virtual environment, cargo-audit, cargo-deny and actionlint. The Python driver used CPython; real-browser/Pyodide behavior and live external compiler services were not tested locally. These local results do not establish remote CI success.

Close #848


Summary by cubic

Adds add-binary cases that reject solutions relying on parseInt/Number precision by requiring exact sums of binary strings wider than 2^53. Also checks exact sums through the C, C++, Java, and Python harnesses.

  • Covers 2^53 + 1, an already-roundable odd value, 128 ones plus one (129-bit result), and two wide addends with a multi-bit carry so rounding only the shorter operand is rejected.
  • Adds a maximum-length all-ones plus one case that rejects per-character Python recursion exceeding its stack limit on valid 10,000-character inputs.
  • Shares a carry-loop helper between the correct reference and the column-of-three wrong adder; both stay pinned to the multi-bit carry case so fixture edits can't remove coverage.
  • Appends cases so existing indexes stay stable and updates the browser regression, contract checks, judge data, and the add-binary golden digest.
  • Verifies exact wide sums through C, C++, Java, and Python harnesses, not just string arithmetic in the browser regression.

Closes #848.