In the game of sudoku, many players like to "pencil in" possible numbers that can go in each square:
The above row can be represented as an array:
[[1,2,9], [6], [5], [7], [1,2,9], [1,2,9], [3], [1,2,4], [8]]
Now, notice that there is only 1 place where a 4
can go. This effectively lets us simplify the above list to:
[[1,2,9], [6], [5], [7], [1,2,9], [1,2,9], [3], [4], [8]]
The goal of this challenge is to take a list of possible numbers in a permutation, and deduce which possibilities can be eliminated.
As another example, lets say you have the following array of possibilities:
[[0,1,3], [0,2,3], [1,2], [1,2]]
The last two places must be filled with 1 and 2. Therefore, we can remove those possibilities from the first two elements in the array:
[[0,3], [0,3], [1,2], [1,2]]
As another example:
[[0,1,2,3], [0,2], [0,2], [0,2]]
Its impossible to construct a permutation from the above possibilities, as there's only 1 location for both 1
and 3
, and you would want to return an empty array.
You need to input a list of possibilities and output the remaining possibilities after the maximum number of possibilities have been eliminated.
- If a particular array is impossible, you either need to return an empty array, or an array where one of the subarrays is empty.
- You may assume that the array will be well-formed, and have at least 1 element.
- Given an array of size
N
, you can assume the numbers in the subarray will always be in the range[0:N)
, and thatN <= 10
- You may not assume that every number from
0
toN-1
will be present - You may assume that numbers within a single subarray are unique.
- If a subarray contains only a single possibility, you can either represent the possibility in an array or by itself.
[[1],[2],[0]]
,[1,2,0]
,[[1,2],0,[1,2]]
are all valid. - You may accept the array either in a reasonable string format or in list/array format.
- Subarrays can be in any order.
- Instead of dealing with ragged arrays, you can pad empty places with
-1
.
Test cases
[[0]] -> [[0]]
[[1],[0]] -> [[1],[0]]
[[1],[1]] -> []
[[1],[0,1]] -> [[1],[0]]
[[0,1,2],[1,2],[1,2]] -> [[0],[1,2],[1,2]]
[[0,1],[1,2],[0,2]] -> [[0,1],[1,2],[0,2]]
[[2,1],[1,2],[1,2]] -> []
[[0,3],[2,1],[3,0],[3,2]] -> [[0,3],[1],[0,3],[2]]
[[0,1],[0,1],[2,3],[2,3,0]] -> [[0,1],[0,1],[2,3],[2,3]]
[[0,1],[0,3],[3,2],[0]] -> [[1],[3],[2],[0]]
[[3,5,2],[0,2,4],[4,0],[0,1,3,5],[2,1],[2,4]] -> [[3,5],[0,2,4],[4,0],[3,5],[1],[2,4]]
[[6,9,8,4],[4,5],[5,3,6],[3,8,6,1,4],[3,1,9,6],[3,7,0,2,4,5],[9,5,6,8],[6,5,8,1,3,7],[8],[8,0,6,2,5,6,3]] -> [[6,9,4],[4,5],[5,3,6],[3,6,1,4],[3,1,9,6],[0,2],[9,5,6],[7],[8],[0,2]]
[[3,5,0],[5,7],[5,1,2],[1,3,0],[5,3],[5,0],[5,3,7,8,0,6],[7,5,0,1,8],[1,0,8],[0,6]] -> []
[[9,0,2,3,7],[0,7,6,5],[6,9,4,7],[9,1,2,3,0,5],[2,8,5,7,4,6],[6,5,7,1],[5,9,4],[5,9,3,8,1],[5,0,6,4],[0,7,2,1,3,4,8]] -> [[9,0,2,3,7],[0,7,6,5],[6,9,4,7],[9,1,2,3,0,5],[2,8,5,7,4,6],[6,5,7,1],[5,9,4],[5,9,3,8,1],[5,0,6,4],[0,7,2,1,3,4,8]]
[[2,6,0],[0,4,3],[0,6,2],[0,7],[0,9,2,3,6,1,4],[1,7,2],[2,7,8],[8,6,7],[6,5,2,8,0],[5,8,1,4]] -> [[2,6,0],[3],[0,6,2],[0,7],[9],[1],[2,7,8],[8,6,7],[5],[4]]
[[8],[8,0,6,5,7,2,4,1],[8,6,9,3,5,0,7],[3,9,1,0],[9],[9,2,6],[2,8,3],[3,1,6,8,2],[6],[6,4,5,3,0,7]] -> [[8],[5,7,4],[5,7],[0],[9],[2],[3],[1],[6],[4,5,7]]
[[8,1,0],[5,8,7,6,2,0],[6,8,2],[2,4,0,9],[4,1,7,3,6,8],[8,1],[8,0,3],[0,8,2],[0,8,3],[1,8,0]] -> []
This is a code-golf so make your answers as short as possible!