In this challenge you will receive as input a list of binary lists. The list represents a game board with each element representing a location on the board. The list at each location represents the tiles on the board, with a 0
being a white tile (□) and a 1
being a black tile (■). Each place can have any number of tiles and the order they appear in the list indicates how they are stacked, with the first element being the tile on the top of the stack and the last being on the bottom of the stack.
For example here's a input list and a graphical representation of the game board:
[[0,0,1],[],[1,1],[1],[],[1,0]]
□
□ ■ ■
■ ■ ■ □
-+-+-+-+-+-
On a turn of this game the player can move by choosing any one place on the board and picking up all the tiles from that place, without disrupting their order. Then the player must choose a direction, either left or right. Then starting from the place they chose to pick from, until they have no tiles left in their hand they repeatedly choose to either:
- Drop the tile at the bottom of the held stack at their current place.
- Move to the adjacent place in their chosen direction.
The player is not permitted drop any tiles out of bounds of the game board.
Here's an example turn:
□ □
■ ■ ■ ■ ■
■ □ □ □ ■
-+-+-+-+-+-
Pick up two tiles from the second place from the left and move to the right:
□
■
□
□
■ ■ ■ ■
■ □ □ ■
-+-+-+-+-+-
Drop 1 tile from the bottom back where it was picked up and move to the right:
□
■
□
■ ■ ■ ■
■ □ □ □ ■
-+-+-+-+-+-
Drop no tiles and move to the right
□
■
□
■ ■ ■ ■
■ □ □ □ ■
-+-+-+-+-+-
Drop two tiles and end.
□
■ ■ □ ■ ■
■ □ □ ■ □ ■
-+-+-+-+-+-
With that the goal of this game is simple. Using as few moves as possible arrange the board so that in every place the top tile is black.
Here is an example game:
□ □ □ □ □
■ ■ ■ ■ ■
-+-+-+-+-
■ □
□ □ □ □
■ ■ ■ ■
-+-+-+-+-
■ □
□ □ □
□ ■ ■ ■ ■
-+-+-+-+-
■ □
□ □ □
□ ■ ■ ■ ■
-+-+-+-+-
□
□ ■
□ □
□ ■ ■ ■ ■
-+-+-+-+-
■
□
□
□
□
□ ■ ■ ■ ■
-+-+-+-+-
Your task is take the starting board as input and determine the number of moves required to win.
You can assume that the board is at least 2 places wide (the outer list contains at least two lists) and that there are enough black tiles to cover every space on the board (i.e. it is possible to solve)
There are two arbitrary choices I have made. You may swap either, both or neither for the opposite choice:
0
is white. You may instead assume0
is black and1
is white.- The first element is the tile on the top. You may instead assume the first element is the tile on the bottom and the last element is the tile on the top.
You may also use suitable substitutions for 0
and 1
such as True
and False
, 1
and -1
, etc.
This is code-golf so the goal is to minimize the size of your source code as measured in bytes.
Brute force is fine, but I encourage you to try golf faster algorithms. Good golfs under self imposed constraints on time are always worth an upvote.
Test cases
[[0,1],[0,1]] -> 3
[[0,1],[0,1],[0,1]] -> 3
[[1,1,1],[],[]] -> 1
[[1,0],[1],[1,1]] -> 0
[[0,1,0,1,0,1],[],[]] -> 2
[[0,1],[0,1]] -> [[0],[1,0,1]] -> [[1,0],[1,0]]
2 turns? \$\endgroup\$