Skip to main content
Shaved some bytes of my pair map array builder.
Source Link

Php, 266 241 213213 200 bytes

Solution:

function u($x,$y){for($i=0;$i<countforeach($x);$k[$y[$i]][]=$x[$i],$i++ as$i=>$j)$k[$x[$i]][]=$y[$i];$h=function{$k[$y[$i]][]=$j;$k[$j][]=$y[$i];}$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not-so golfed:

function unify($x, $y)
{
    for($i=0; $i < countforeach($x); $i++as $i=>$j) {
        $k[$y[$i]][] = $x[$i];$j;
        $k[$x[$i]][]$k[$j][] = $y[$i];
    } 

    $h = function ($c, &$w=[]) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}

Php, 266 241 213 bytes

Solution:

function u($x,$y){for($i=0;$i<count($x);$k[$y[$i]][]=$x[$i],$i++)$k[$x[$i]][]=$y[$i];$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not-so golfed:

function unify($x, $y)
{
    for($i=0; $i < count($x); $i++) {
        $k[$y[$i]][] = $x[$i];
        $k[$x[$i]][] = $y[$i];
    }
    $h = function ($c, &$w=[]) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}

Php, 266 241 213 200 bytes

Solution:

function u($x,$y){foreach($x as$i=>$j){$k[$y[$i]][]=$j;$k[$j][]=$y[$i];}$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not-so golfed:

function unify($x, $y)
{
    foreach($x as $i=>$j) {
        $k[$y[$i]][] = $j;
        $k[$j][] = $y[$i];
    } 

    $h = function ($c, &$w=[]) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}
deleted 5 characters in body
Source Link

Php, 266 241 213 bytes

Solution:

function u($x,$y){for($i=0;$i<count($x);$k[$y[$i]][]=$x[$i],$i++)$k[$x[$i]][]=$y[$i];$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not-so golfed:

function unify($x, $y)
{
    for($i=0; $i < count($x); $i++) {
        $k[$y[$i]][] = $x[$i];
        $k[$x[$i]][] = $y[$i];
    }
    $h = function ($c, &$w=array()&$w=[]) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}

Php, 266 241 213 bytes

Solution:

function u($x,$y){for($i=0;$i<count($x);$k[$y[$i]][]=$x[$i],$i++)$k[$x[$i]][]=$y[$i];$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not-so golfed:

function unify($x, $y)
{
    for($i=0; $i < count($x); $i++) {
        $k[$y[$i]][] = $x[$i];
        $k[$x[$i]][] = $y[$i];
    }
    $h = function ($c, &$w=array()) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}

Php, 266 241 213 bytes

Solution:

function u($x,$y){for($i=0;$i<count($x);$k[$y[$i]][]=$x[$i],$i++)$k[$x[$i]][]=$y[$i];$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not-so golfed:

function unify($x, $y)
{
    for($i=0; $i < count($x); $i++) {
        $k[$y[$i]][] = $x[$i];
        $k[$x[$i]][] = $y[$i];
    }
    $h = function ($c, &$w=[]) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}
Previously I was storing the highest value throughout recursion. But as this value sits in the path walked ($w) it can be pulled out with max().
Source Link

Php, 266 241241 213 bytes

Yep it's long. But I'm rather glad I got something to workSolution:

function u($x,$y){for($i=0;$i<count($x);$k[$y[$i]][]=$x[$i],$i++)$k[$x[$i]][]=$y[$i];$h=function($c,&$m,&$w)use($k,&$h){if($m===null||$c>=$m)$m=$c;$w[]=$c;foreach$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$m,$w);return$m;;return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not so-so golfed:

function unify($x, $y) {
{
    for($i=0; $i < count($x); $i++) {
        $k[$y[$i]][] = $x[$i];
        $k[$x[$i]][] = $y[$i];
    }
    $h = function ($c, &$m=null, &$w=null&$w=array()) use ($k, &$h) {
        if($m === null || $c >= $m)
            $m = $c;
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $m, $w);
        return $m;max($w);
    };

    return array_map($h, $x);
}

Php, 266 241 bytes

Yep it's long. But I'm rather glad I got something to work:

function u($x,$y){for($i=0;$i<count($x);$k[$y[$i]][]=$x[$i],$i++)$k[$x[$i]][]=$y[$i];$h=function($c,&$m,&$w)use($k,&$h){if($m===null||$c>=$m)$m=$c;$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$m,$w);return$m;};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not so golfed:

function unify($x, $y) {

    for($i=0; $i < count($x); $i++) {
        $k[$y[$i]][] = $x[$i];
        $k[$x[$i]][] = $y[$i];
    }
    $h = function ($c, &$m=null, &$w=null) use ($k, &$h) {
        if($m === null || $c >= $m)
            $m = $c;
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $m, $w);
        return $m;
    };

    return array_map($h, $x);
}

Php, 266 241 213 bytes

Solution:

function u($x,$y){for($i=0;$i<count($x);$k[$y[$i]][]=$x[$i],$i++)$k[$x[$i]][]=$y[$i];$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

Usage: u([1,2,3], [0,0,1]); returns the desired array.

Not-so golfed:

function unify($x, $y)
{
    for($i=0; $i < count($x); $i++) {
        $k[$y[$i]][] = $x[$i];
        $k[$x[$i]][] = $y[$i];
    }
    $h = function ($c, &$w=array()) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}
Removed legacy unneeded code that went unnoticed.
Source Link
Loading
Source Link
Loading