Challenge
Per Wikipedia:
Numberlink is a type of logic puzzle involving finding paths to connect numbers in a grid.
The player has to pair up all the matching numbers on the grid with single continuous lines (or paths). The lines cannot branch off or cross over each other, and the numbers have to fall at the end of each line (i.e., not in the middle).
For example, consider this numberlink puzzle:
This puzzle is solvable, see the solution below:
But some numberlink puzzle is unsolvable, like for this one when it's obvious
Another one where it is unsolvable
Your challenge is, given a numberlink puzzle, decide whether it is solvable or not. (A solution doesn't need to fill the entire grid.)
Input / Output
Input / Output can be taken in any reasonable format for taking in a rectangle numberlink puzzle, and deciding whether it is solvable or not. A dot .
is used for blank space.
You may assume there are exactly two of any of the non-zero endpoint values and that the set of these form a prefix of the positive integers \$[1,n] , n \in \Bbb{N^+} \$.
You may assume at least one pair of endpoint values present - you won't be given an empty puzzle.
Example testcases:
Input -> Output
[[1, 3, ., ., ., ., .],
[., 2, ., 4, 6, ., .],
[., ., ., ., 5, ., .],
[., ., 1, ., ., ., .], -> True
[., ., ., ., ., ., .],
[., 2, ., ., 5, 6, .],
[., ., ., 3, 4, ., .]]
[[., 2, 3, 4, .],
[1, ., ., ., 1], -> False
[., 2, 3, 4, .]]
[[1, ., 2, ., 4],
[., ., 3, ., 5],
[., ., ., ., .], -> False
[., 2, ., 5, .],
[., 1, 3, 4, .]]
Alternatively, you can take the width and height of the puzzle, and the two coordinates of each number.
Example testcases (like the above):
Input -> Output:
7 7 // denote the size of the puzzle
[[[1, 1], [3, 4]], // two coordinates of each number
[[2, 2], [2, 6]],
[[2, 1], [7, 4]],
[[4, 2], [5, 7]], --> True
[[5, 3], [5, 6]],
[[5, 2], [6, 6]],
5 3
[[[1, 2], [5, 2]],
[[2, 1], [2, 3]], --> False
[[3, 1], [3, 3]],
[[4, 1], [4, 3]]]
5 5
[[[1, 1], [2, 5]],
[[1, 3], [2, 4]],
[[3, 2], [3, 5]], --> False
[[5, 1], [4, 5]],
[[4, 4], [5, 2]]]
This is code-golf, so shortest answer (in bytes) wins!
You probably know this from the game Flow Free. Finding a solution to a Numberlink puzzle is NP-complete, see this video