FunctionRacerProblemsRace

Turns to Match

EasyGridsPython, JavaScript, TypeScript, Java, C++

Turning a grid a quarter turn clockwise makes its first column, read from bottom to top, the new first row, its second column the new second row, and so on, so a grid with R rows and C columns becomes one with C rows and R columns. a and b are grids given as lists of rows. Return the smallest number of clockwise quarter turns, 0, 1, 2 or 3, that turns a into exactly b, or -1 if none of them does. The two grids may have different shapes, and neither may be changed.

Example 1

a [[1,2],[3,4]]
b [[3,1],[4,2]]
returns 1

One clockwise turn reads the first column (1, 3) from the bottom up as the row [3, 1] and the second column (2, 4) as [4, 2], which is b.

Example 2

a [[1,2,3]]
b [[3],[2],[1]]
returns 3

One turn gives [[1], [2], [3]] and two turns give [[3, 2, 1]]; the third turn finally gives b.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems