laserlight wrote:The complexity is very different though.
In other words, not that similar after all 🙂
The way I see it, Weedpacket, is that array_diff() will not work here, since it may be possible to have repeated guesses and outcomes.
Oh, yes. For some reason I thought of the array of outcomes as a set - that the values would all be distinct. In fact they're actual outcomes, not possible outcomes. I think it was those nested loops that go up to 3 that threw me. If the arrays really are that small I'd seriously consider listing cases explicitly.
If the arrays are going to be large, though, with a limited number of possible outcomes and the order is irrelevant (if the order matters then array_diff_assoc() could be used instead to find matching pairs), then keeping a count of each outcome as it happens might be more efficient. Instead of array(4,3,6,5,2,2,1,5,3,3,6,2,4,3,3,1) there's array(1=>2, 2=>3, 3=>5, 4=>2, 5=>2, 6=>2).
$possible_outcomes = array(1,2,3,4,5,6, 'yahtzee');
foreach($possible_outcomes as $outcome)
{
$matches = min($do_array[$outcome], $dg_array[$outcome]);
$do_array[$outcome]-=$matches;
$dg_array[$outcome]-=$matches;
}
Constant in time, constant in space, the $possible_outcomes array would be useful elsewhere, and the win comes when array_sum($dg_array)==0.