Can I follow this recipe?
You work in a kitchen which has a peculiar rule. When you mix something into a pot you must always add at least one new ingredient.
So you can add pasta, oil, salt then pesto, but not pasta, pesto, salt and oil since pesto already contains salt and oil. You only have one pot per dish, so you can't mix ingredients in one pot and then dump that into another.
So we can say that mixing is an associative binary operation $\otimes$, which takes the union of two sets of ingredients, but only operates on pairs where the second set contains some element not in the first, so
$$ \{1,2\}\otimes\{6,2\} = \{6,1,2\} $$but
$$ \{1,2,3\}\otimes\{1,3\} $$is undefined.
Now this rule is annoying because it means you have to be careful about the order you combine things. Sometimes there are even recipes can't be followed because there's no way to mix all the parts without breaking the rule.
A recipe can be followed if there is a way to order the parts so that each part contains an ingredient not found in prior parts.
So if the parts of the recipe are:
$$ \{1,2,3\},\,\{\},\,\{2,6\},\,\{1,2,4\},\,\{3\} $$then they can be ordered:
$$ \{\}\otimes\{3\}\otimes\{1,2,3\}\otimes\{2,6\}\otimes\{1,2,4\} $$However
$$ \{1,2,3\},\,\{\},\,\{2,4\},\,\{1,2,4\},\,\{3\} $$cannot be ordered without breaking the kitchen's rule.
Task
Take as input sets of positive integers representing parts of a recipe. Output one of two distinct consistent values depending on whether the input represents a possible or impossible recipe.
This is code-golf. The goal is to minimize the size of your source code as measured in bytes.
Test cases
{},{} -> False
{2},{9} -> True
{},{3},{2,6},{1,2,4},{1,2,3} -> True
{1,2,3},{},{2,4},{1,2,4},{3} -> False
1 answer
Pyth, 15 bytes
!f.A-VtTsM._T.p
Outputs True if no valid recipe exists, False if one does. If truthy/falsy outputs are allowed, the !
can be removed, to just output all valid permutations. Takes input as lists, not sets.
!f.A-VtTsM._T.p
.p All permutations of parts
f Filter permutations
._T All prefixes of parts
sM Sum prefixes
-VtT Filter each part for ingredients
not contained in previous part
.A Check all parts had something new
! Check whether no permutations remain
0 comment threads