Skip to main content
added 1105 characters in body
Source Link
Kjetil S
  • 6.1k
  • 10
  • 22

Perl 5, 166 bytes

sub{($s,$e,%o)=@_;@w=(0,$s);while(($n,$s,@w)=@w){$s-$e||last,push@w,map{$n+1,$_}grep{($a,$b,$c,$d)=map{$_>>3,$_%8}$s,$_;!(($a-$c)**2+($b-$d)**2-5||$o{$_}++)}0..63}$n}

Try it online!

sub steps {
  ($s,$e,%o)=@_;        #start, end, occupied set as hash from input args in @_
  @w=(0,$s);            #initial work (steps,square)
  while(($n,$s,@w)=@w){ #while work left to be done
    $s-$e||last,        #bail if current square $s equals $e end square
    push@w,             #register new work/new squares to be checked
      map{$n+1,$_}      # (new steps, new square)
      grep{                       #filter
        ($a,$b,$c,$d)             #coords of current square ($a,$b)
          =map{$_>>3,$_%8}$s,$_;  #...and potential new square ($c,$d)
        !(($a-$c)**2+($b-$d)**2-5 #pythagorean horsyness check of new vs current
          ||$o{$_}++)             #...non-occupied new squares
                                  #...and register new square as occupied
                                  #...with ++ to not enter it again
      }
      0..63                       #loop through potential new squares
  }
  $n                              #return n steps from last work
}

Perl 5, 166 bytes

sub{($s,$e,%o)=@_;@w=(0,$s);while(($n,$s,@w)=@w){$s-$e||last,push@w,map{$n+1,$_}grep{($a,$b,$c,$d)=map{$_>>3,$_%8}$s,$_;!(($a-$c)**2+($b-$d)**2-5||$o{$_}++)}0..63}$n}

Try it online!

Perl 5, 166 bytes

sub{($s,$e,%o)=@_;@w=(0,$s);while(($n,$s,@w)=@w){$s-$e||last,push@w,map{$n+1,$_}grep{($a,$b,$c,$d)=map{$_>>3,$_%8}$s,$_;!(($a-$c)**2+($b-$d)**2-5||$o{$_}++)}0..63}$n}

Try it online!

sub steps {
  ($s,$e,%o)=@_;        #start, end, occupied set as hash from input args in @_
  @w=(0,$s);            #initial work (steps,square)
  while(($n,$s,@w)=@w){ #while work left to be done
    $s-$e||last,        #bail if current square $s equals $e end square
    push@w,             #register new work/new squares to be checked
      map{$n+1,$_}      # (new steps, new square)
      grep{                       #filter
        ($a,$b,$c,$d)             #coords of current square ($a,$b)
          =map{$_>>3,$_%8}$s,$_;  #...and potential new square ($c,$d)
        !(($a-$c)**2+($b-$d)**2-5 #pythagorean horsyness check of new vs current
          ||$o{$_}++)             #...non-occupied new squares
                                  #...and register new square as occupied
                                  #...with ++ to not enter it again
      }
      0..63                       #loop through potential new squares
  }
  $n                              #return n steps from last work
}
Source Link
Kjetil S
  • 6.1k
  • 10
  • 22

Perl 5, 166 bytes

sub{($s,$e,%o)=@_;@w=(0,$s);while(($n,$s,@w)=@w){$s-$e||last,push@w,map{$n+1,$_}grep{($a,$b,$c,$d)=map{$_>>3,$_%8}$s,$_;!(($a-$c)**2+($b-$d)**2-5||$o{$_}++)}0..63}$n}

Try it online!