Solves the question asked here: https://stackoverflow.com/q/47261385
- Input array contains set of arrays.
- Each inner array contains unique elements.
- Each inner array may have different length and different values.
- Output array must contain exact same values.
- Output inner array must have unique values on same key.
- If there is no solution, wildcard ie.: null are allowed.
- Wildcards can be duplicated on same key.
- Solution should have as few wildcards as possible.
- Algorithm should be able to handle array up to 30x30 in less than 180 s.
The idea is to identify conflicting elements and swap them to a column where they are no longer a problem. For cases where this is not applicable a random selection is done. The code works recursive and thus there are edge-cases where it takes very long to complete.
An extreme edge-case is an input where all rows consist of exactly the same values.
<?php
$solver = new SwapSolver();
$solution = $solver->solve([
['A', 'B', 'C'],
['A', 'B', 'C'],
['A', 'B', 'C'],
]);
> php solve.php solve unique --size=30 --num=1
> php solve.php solve random --size=30 --num=1
> php solve.php solve equal --size=30 --num=1
> php solve.php solve fixed --num=22
When