FunctionRacerProblemsRace

Four in a Row

MediumGridsPython, JavaScript, TypeScript, Java, C++

Two players take turns dropping discs into an upright board with rows rows and cols columns; player 1 moves first. drops[i] is the column (counted from 0, left to right) chosen on move number i + 1, and the disc falls to the lowest empty cell of that column. A player wins as soon as four of their discs lie next to each other in a line: along a row, along a column, or along either diagonal. Return [player, move]: the winning player (1 or 2) and the number of the move that completed the line. Moves after the winning one are ignored. If nobody gets four in a line, return [0, 0]. No disc is ever dropped into a full column.

Example 1

rows 6
cols 7
drops [3,3,4,4,5,5,6]
returns [1,7]

Player 1 drops into columns 3, 4, 5 and 6 on moves 1, 3, 5 and 7, filling four neighbouring cells of the bottom row.

Example 2

rows 4
cols 4
drops [0,1,0,1,0,1,2,1]
returns [2,8]

Player 2 drops into column 1 on moves 2, 4, 6 and 8 and stacks four discs in that column. Player 1 only ever has three in a line.

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