← All challenges

Max Coins with One Turn

Grid / Path (Advanced) ★★☆☆

Maximum coins collected from A1 to F6, moving only right at first and only down after one allowed turn — once you turn down, you cannot turn back.

A1:F6 is a 6×6 grid of coin piles. A collector starts at A1, moves through the grid, and ends at F6. The catch: the collector can only move right first, and once they make a single right-to-down turn, they can no longer turn back — every remaining move must be down.

So a valid trajectory is: zero or more rights, exactly one downturn, zero or more downs. The collector picks up the coins in every cell they pass through. Output the maximum total coins on any valid trajectory.

There are only 6 + 6 − 1 = 11 such L-shaped trajectories on a 6×6 grid, but they fan out across very different sums. The formula encodes the once-allowed turn as a boolean in the recursion: before turning, the call has both branches; after turning, only the down branch is legal.

Input

Range A1:F6

ABCDEF
1 305812015
2 6234242129
3 122629389
4 2112063018
5 222430142329
6 219291199
Hint

Branching recursion with a boolean `turned` argument. Before the turn, the next move can be either right (still not turned) or down (now turned). After the turn, only down is legal.

Solution
=LET(
  tbl, A1:F6,
  ros, ROWS(tbl),
  cols, COLUMNS(tbl),
  fx, LAMBDA(me, ro, co, turned,
    IF(OR(ro>ros, co>cols),
      0,
      LET(
        cur, INDEX(tbl, ro, co),
        IF(AND(ro=ros, co=cols),
          cur,
          cur + IF(turned,
            me(me, ro+1, co, TRUE),
            MAX(me(me, ro, co+1, FALSE), me(me, ro+1, co, TRUE))
          )
        )
      )
    )
  ),
  fx(fx, 1, 1, FALSE)
)