Ok, here goes!
1. For every box, create a list of touching circles:
[1] [1,9] [9] [3] [] [11] [8,11] [8]
[1] [1,2,9] [2,9] [4,5] [5,10] [10,11] [8,11] [8]
[] [2] [2] [6] [6,10] [7,10] [7] []
2. Remove duplicates (and empty lists!):
[1] [1,9] [9] [3]
[] [11] [8,11] [8]
[1] [1,2,9] [2,9] [4,5] [5,10] [10,11]
[8,11] [8]
[] [2]
[2] [6] [6,10] [7,10] [7]
[][/b]
[1] [1,9] [9] [3] [11] [8,11] [8] [1,2,9] [2,9] [4,5] [5,10] [10,11] [2] [6] [6,10] [7,10] [7]
3. Remove lists which are sub-lists:
[3] [8,11] [1,2,9] [4,5] [5,10] [10,11] [6,10] [7,10]
4. Group first list with a list containing as many same circle numbers as possible ensuring the circle count stays under 5. Repeat until max 5 count is reached:
[3] + [8,11] = [3,8,11] (nothing else contains 3!)
[3,8,11] [1,2,9] [4,5] [5,10] [10,11] [6,10] [7,10]
[3,8,11] + [10,11] = [3,8,10,11] (most matching numbers)
[3,8,10,11] [1,2,9] [4,5] [5,10] [6,10] [7,10]
[3,8,10,11] + [5,10] = [3,5,8,10,11] Group full!
[3,5,8,10,11] [1,2,9] [4,5] [6,10] [7,10]
6. Group is now full, so move onto next group and repeat:
[1,2,9] + [4,5] = [1,2,4,5,9] No groups with matching numbers. Full!
[3,5,8,10,11] [1,2,4,5,9] [6,10] [7,10]
Next group:
[6,10] + [7,10] = [6,7,10]
[3,5,8,10,11] [1,2,4,5,9] [6,7,10]
Done!