← All challenges

Jump Game with All Hops

Grid / Path (Advanced) ★★★☆

Reachability on a 5×5 grid where each cell's value is the MAXIMUM jump distance — any shorter hop (1..max) is also legal. Right or down only.

A1:E5 is a 5×5 grid. The robot starts at A1. Each cell’s value is the maximum jump distance from that cell — 3 means the robot may hop 1, 2, or 3 cells right, or 1, 2, or 3 cells down. A 0 means the robot is stuck (unless already at the goal).

Return TRUE if any sequence of valid jumps lands on E5 (bottom-right), FALSE otherwise.

This is the more permissive cousin of the exact-jump variant. The formula trick is the inner REDUCE: at each cell, fold over all hop lengths 1..cur and OR the recursive reachability results, in each direction. (Side note: the exact-jump formula on this same grid returns FALSE — the smaller hops matter here.)

Input

Range A1:E5

ABCDE
1 13121
2 22032
3 10212
4 23102
5 23130
Hint

Like the exact-jump version, but at each cell try every hop length from 1 to cur. Use REDUCE over SEQUENCE(cur,,1) to OR together the results across all hop lengths in each direction.

Solution
=LET(
  tbl, A1:E5,
  ros, ROWS(tbl),
  cols, COLUMNS(tbl),
  fx, LAMBDA(me, ro, co,
    LET(
      cur, INDEX(tbl, ro, co),
      IF(OR(ro>ros, co>cols), FALSE,
      IF(cur=0, FALSE,
      IF(AND(ro=ros, co=cols), TRUE,
        LET(
          down, REDUCE(FALSE, SEQUENCE(cur,,1), LAMBDA(s, c, OR(s, me(me, ro+c, co)))),
          right, REDUCE(FALSE, SEQUENCE(cur,,1), LAMBDA(s, c, OR(s, me(me, ro, co+c)))),
          OR(down, right)
        )
      )))
    )
  ),
  fx(fx, 1, 1)
)