First Winning Move in a Gravity Drop Game on an Unbounded Board
Company: Jane Street
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Two players take turns dropping pieces into the columns of a board that has no edges. Columns are identified by any integer (positive, zero or negative), and every column is unbounded upward. Gravity applies: a piece dropped into a column lands on the lowest empty cell of that column, so the first piece in a column occupies row `0`, the next one row `1`, and so on.
Player 1 makes the first move and the players then alternate, so move `i` (0-based) belongs to player 1 when `i` is even and to player 2 when `i` is odd. A player wins when the piece just placed completes a line of at least `k` of that player's pieces in consecutive cells, horizontally, vertically or along either diagonal.
Given the columns in the order the pieces are dropped and the win length `k`, return the 0-based index of the first move that wins. If no move wins, return `-1`.
### Function Signature
```python
def first_winning_move(moves: list[int], k: int) -> int:
```
### Rules
- Cell `(c, r)` is column `c`, row `r`. A line is a set of `k` cells `(c + j*dc, r + j*dr)` for `j = 0, 1, ..., k-1`, where the direction `(dc, dr)` is `(1, 0)`, `(0, 1)`, `(1, 1)` or `(1, -1)`, and every one of those cells holds a piece of the same player.
- Rows below `0` do not exist. Columns with different numbers are different columns however far apart they are, and nothing wraps around.
- A line longer than `k` also wins: any `k` consecutive cells of it form a line.
- The game ends at the first winning move. Moves after it in the list are ignored.
### Constraints
- `1 <= len(moves) <= 10^5`
- `-10^9 <= moves[i] <= 10^9`
- `2 <= k <= 20`
- The result is `-1` or an index from `0` to `len(moves) - 1`.
### Examples
#### Example 1
```text
Input: moves = [0,1,0,1,0,1,0], k = 4
Output: 6
```
Player 1 drops into column 0 on moves 0, 2, 4 and 6, filling rows 0 to 3 of that column. Player 2's three pieces in column 1 never reach four.
#### Example 2
```text
Input: moves = [-1,0,0,1,5,1,1], k = 3
Output: 6
```
Player 1's pieces land at `(-1, 0)`, `(0, 1)`, `(5, 0)` and finally `(1, 2)`, on top of player 2's two pieces in column 1. That last piece completes the diagonal `(-1, 0)`, `(0, 1)`, `(1, 2)`. Before move 6 neither player has three in a line: player 2 holds `(0, 0)`, `(1, 0)` and `(1, 1)`.
#### Example 3
```text
Input: moves = [0,1000000000,-1000000000,0], k = 2
Output: -1
```
Player 1 holds `(0, 0)` and `(-1000000000, 0)`, and player 2 holds `(1000000000, 0)` and `(0, 1)`. No two pieces of the same player are in adjacent cells.
Overview: Simulate a two-player game in which pieces drop into integer-numbered columns of a board without edges and stack from the bottom, and find the first move that completes a line of k pieces horizontally, vertically or diagonally. It tests modeling an unbounded board and checking every line direction correctly.
Two players take turns dropping pieces into the columns of a board that has no edges. Columns are identified by any integer (positive, zero or negative), and every column is unbounded upward. Gravity applies: a piece dropped into a column lands on the lowest empty cell of that column, so the first piece in a column occupies row `0`, the next one row `1`, and so on.
Player 1 makes the first move and the players then alternate, so move `i` (0-based) belongs to player 1 when `i` is even and to player 2 when `i` is odd. A player wins when the piece just placed completes a line of at least `k` of that player's pieces in consecutive cells, horizontally, vertically or along either diagonal.
Given `moves`, the columns in the order the pieces are dropped, and the win length `k`, return the 0-based index of the first move that wins. If no move wins, return `-1`.
### Rules
- Cell `(c, r)` is column `c`, row `r`. A line is a set of `k` cells `(c + j*dc, r + j*dr)` for `j = 0, 1, ..., k-1`, where the direction `(dc, dr)` is `(1, 0)`, `(0, 1)`, `(1, 1)` or `(1, -1)`, and every one of those cells holds a piece of the same player.
- Rows below `0` do not exist. Columns with different numbers are different columns however far apart they are, and nothing wraps around.
- A line longer than `k` also wins: any `k` consecutive cells of it form a line.
- The game ends at the first winning move. Moves after it in the list are ignored.
### Example 1
```text
Input: moves = [0,1,0,1,0,1,0], k = 4
Output: 6
```
Player 1 drops into column 0 on moves 0, 2, 4 and 6, filling rows 0 to 3 of that column. Player 2's three pieces in column 1 never reach four.
### Example 2
```text
Input: moves = [-1,0,0,1,5,1,1], k = 3
Output: 6
```
Player 1's pieces land at `(-1, 0)`, `(0, 1)`, `(5, 0)` and finally `(1, 2)`, on top of player 2's two pieces in column 1. That last piece completes the diagonal `(-1, 0)`, `(0, 1)`, `(1, 2)`. Before move 6 neither player has three in a line: player 2 holds `(0, 0)`, `(1, 0)` and `(1, 1)`.
### Constraints
- `1 <= len(moves) <= 10^5`
- `-10^9 <= moves[i] <= 10^9`
- `2 <= k <= 20`
- The result is `-1` or an index from `0` to `len(moves) - 1`.
Every column id and every returned index fits in a signed 32-bit integer.
Constraints
- 1 <= len(moves) <= 10^5
- -10^9 <= moves[i] <= 10^9
- 2 <= k <= 20
- The result is -1 or an index from 0 to len(moves) - 1.
Examples
Input: ([0], 2)
Expected Output: -1
Explanation: A single piece cannot form a line because k is at least 2.
Input: ([0, 1, 0], 2)
Expected Output: 2
Explanation: Player 1 stacks (0, 0) and (0, 1), the smallest vertical win with k = 2.
Hints
- A win is defined through the piece just placed, so look at the lines that pass through that one cell.
- Column ids can be negative and up to 2 * 10^9 apart, so store only the cells that hold pieces.
- Because of gravity, the row of a new piece equals the number of pieces already in its column.