Introduction
You may remember sudoku, where there can only be one of each number in each row, column and block of nine. The general idea is that if the array contains but one element, you can submit that as a deterministic solution. If there are already filtered arrays (by rows, columns and ×ばつ3 blocks) the remainder can still be deterministic. Where
- a=[1,2];
- b=[1,3];
- c=[2,3];
- d=[1,2,3,4]
d should be reduced to [4] as it apparently cannot be anything else.
Challenge
Provide a sniplet that compares n arrays. For every X, if only X numbers appear at least once in a set of X arrays, then all those X numbers should be removed from each array not in the set. The elements are integer numbers ranging [1, n].
Winning criterion
Lowest character count.
(削除) "I'm too young to die" version
9 arrays at most, 9 elements at most. (削除ここまで)
Test Cases
[1,2]; [2,3]; [1,3]; [1,2,3,4]
[1,2,3]; [2,3,4]; [1,5,6]; [2,3]; [1,4]; [1,2,3,4,6]Test outputs
[1,2]; [2,3]; [1,3]; [4]
[1,2,3]; [2,3,4]; [5]; [2,3]; [1,4]; [6]
2 Answers 2
Pyth: (削除) 37 (削除ここまで) (削除) 29 (削除ここまで) 28 bytes
um?d|-lHl{sHsmg{kdH-dsHGtyQQ
Input is a list of lists like [[1,2], [2,3], [1,3], [1,2,3,4]]
. Try it online: Pyth Compiler/Executor
Explanation:
um?d|-lHl{sHsmg{kdH-dsHGtyQQ
u Q G = input array
u tyQQ For each H in powerset(input):
m G G = [ ... for d in G]
?d -dsH [d if ... else d-sum(H) for d in G]
-lHl{sH len(H)-len(set(sum(H)))!=0
| or
smg{kdH sum(d is subset of k for k in H)>0
-
\$\begingroup\$ I love this. Clear, concise. \$\endgroup\$user3819867– user38198672015年02月09日 10:58:23 +00:00Commented Feb 9, 2015 at 10:58
C# - 231 characters
static int[][] P(int[][]a){var m=a.GetUpperBound(0)+1;var j=0;for(var i=0;i<m;i++){var u=new List<int>();for(j=0;j<=i;j++)u=u.Union(a[j]).ToList();if(u.Count()==m)for(j=0;j<m;j++)if(i!=j)a[i]=a[i].Except(a[j]).ToArray();}return a;}
Ungolfed and harness
using System.Collections.Generic;
using System.Linq;
namespace ArrayCombCompar
{
class Program
{
static void Main(string[] args)
{
var testArray1 = new int[][] {
new int[] {1,2},
new int[] {2,3},
new int[] {1,3},
new int[] {1,2,3,4}
};
var testArray2 = new int[][] {
new int[] {1,2,3},
new int[] {2,3,4},
new int[] {1,5,6},
new int[] {2,3},
new int[] {1,4},
new int[] {1,2,3,4,6}
};
var result1 = P(testArray1);
var result2 = P(testArray2);
} // Put breakpoint here to see results
static int[][] P(int[][]a)
{
var m = a.GetUpperBound(0) + 1;
var j = 0;
for (var i = 0; i < m; i++)
{
var u = new List<int>();
for (j = 0; j <= i; j++)
u = u.Union(a[j]).ToList();
if (u.Count() == m)
for (j = 0; j < m; j++)
if (i != j) a[i] = a[i].Except(a[j]).ToArray();
}
return a;
}
}
}
n
arrays in the input, and their union is exactly[1,2,...,n]
? Also, I don't really understand the challenge. What should the output be in your example? \$\endgroup\$