Skip to content

Instantly share code, notes, and snippets.

@mcmillhj
Created April 27, 2018 14:28
Show Gist options
  • Save mcmillhj/57a4e81cfbfa94d33bc2babcf4563da1 to your computer and use it in GitHub Desktop.
Save mcmillhj/57a4e81cfbfa94d33bc2babcf4563da1 to your computer and use it in GitHub Desktop.
Why does this return a Seq? I expected a List.
sub search(Int $x, Int $y, Int $max-depth is copy = 25000 --> List) {
state %cache{Set};
my @queue = (($x, $y, Nil, Nil),);
while @queue and --$max-depth > 0 {
my ($x, $y, $px, $py) = @queue.shift();
%cache{Set($x, $y)} = [$px, $py];
if $x == $y {
my @path;
while $x and $y {
@path.push: [$x, $y];
($x, $y) = %cache{Set($x, $y)};
}
return reverse @path;
}
else {
@queue.push: (2 * $x, $y + 1, $x, $y);
@queue.push: ($x + 1, 2 * $y, $x, $y);
}
}
return Nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment