Skip to content

Instantly share code, notes, and snippets.

@mshock
Created July 20, 2012 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshock/3153626 to your computer and use it in GitHub Desktop.
Save mshock/3153626 to your computer and use it in GitHub Desktop.
golf - reverse numbers game
# current solution using local minima/maxima algorithm - 222
@z=split' ',<>;while(!$t){$t==$_-1?$t=$_:($t=0,last)for@z=(f(shift@z),@z)}sub f{$i++;my($p,$c)=(pop,shift@z);if(($p<$c&&$c>$z[0])||($p>$c&&$c<$z[0])||$#z==-1){if($i+1!=$l){$l=$i+1;print"$l ";$i=0;return($c,$p)}}(f($c),$p)}
=pod
@z=qw(30 13 19 23 18 11 21 8 10 4 29 14 31 27 1 9 22 32 7 2 25 12 17 33 5 26 16 6 35 3 24 28 34 15 20);
while (!$t) {
$t==$_-1?$t=$_:($t=0,last) for @z=( func(shift@z), @z);
#map {$t==$_-1?$t++:0} @z=( func(shift@z), @z);
}
print "\n",join ',', @z;
sub func {
$i++;
my($p,$c) = (pop,shift@z);
if ( ($p<$c && $c>$z[0]) || ($p>$c && $c<$z[0]) || $#z==-1) {
if ($i+1 != $l) {
$l = $i+1;
print "$l ";
$i = 0;
return($c,$p)
}
}
(func($c),$p)
}
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment