Skip to main content
added 64 characters in body
Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13

Scala, 378378 336 bytes

Saved 42 bytes thanks to the comment of @Kjetil S

Golfed version. Try it online!Try it online!

def f(s:(Int,Int),b:Seq[(Int,Int)],t:(Int,Int),K:Int=0):Int=={var p=Seq(s);var k=K;while(!p.contains(t)&&k<64){p=(for(i<-0 until 64;u=i/8;v=i%8;if !b.contains((u,v))&&p.exists{case(x,y)=>(x-u)*(x-u)+(y-v)*(y-v)==5})yield(u,v)).toList;k+=1};k%64}
def C(s:String):(Int,Int)=("abcdefgh".indexOf(s(0))-97,s(1).asDigit-149)
def h(a:String,b:List[String],c:String):Int=f=f(C(a),b.map(C),C(c))

Scala, 378 bytes

Golfed version. Try it online!

def f(s:(Int,Int),b:Seq[(Int,Int)],t:(Int,Int),K:Int=0):Int={var p=Seq(s);var k=K;while(!p.contains(t)&&k<64){p=(for(i<-0 until 64;u=i/8;v=i%8;if !b.contains((u,v))&&p.exists{case(x,y)=>(x-u)*(x-u)+(y-v)*(y-v)==5})yield(u,v)).toList;k+=1};k%64}
def C(s:String):(Int,Int)=("abcdefgh".indexOf(s(0)),s(1).asDigit-1)
def h(a:String,b:List[String],c:String):Int=f(C(a),b.map(C),C(c))

Scala, 378 336 bytes

Saved 42 bytes thanks to the comment of @Kjetil S

Golfed version. Try it online!

def f(s:(Int,Int),b:Seq[(Int,Int)],t:(Int,Int),K:Int=0)={var p=Seq(s);var k=K;while(!p.contains(t)&&k<64){p=(for(i<-0 until 64;u=i/8;v=i%8;if !b.contains((u,v))&&p.exists{case(x,y)=>(x-u)*(x-u)+(y-v)*(y-v)==5})yield(u,v)).toList;k+=1};k%64}
def C(s:String)=(s(0)-97,s(1)-49)
def h(a:String,b:List[String],c:String)=f(C(a),b.map(C),C(c))
Source Link
138 Aspen
  • 6.6k
  • 1
  • 4
  • 13

Scala, 378 bytes

Port of @bsoelch's Python answer in Scala.


Golfed version. Try it online!

def f(s:(Int,Int),b:Seq[(Int,Int)],t:(Int,Int),K:Int=0):Int={var p=Seq(s);var k=K;while(!p.contains(t)&&k<64){p=(for(i<-0 until 64;u=i/8;v=i%8;if !b.contains((u,v))&&p.exists{case(x,y)=>(x-u)*(x-u)+(y-v)*(y-v)==5})yield(u,v)).toList;k+=1};k%64}
def C(s:String):(Int,Int)=("abcdefgh".indexOf(s(0)),s(1).asDigit-1)
def h(a:String,b:List[String],c:String):Int=f(C(a),b.map(C),C(c))

Ungolfed version. Try it online!

object Main {
  def f(s: (Int, Int), b: List[(Int, Int)], t: (Int, Int), k: Int = 0): Int = {
    var p = List(s)
    var kVar = k
    while (!p.contains(t) && kVar < 64) {
      p = (for (i <- 0 until 64; 
                u = i / 8; 
                v = i % 8; 
                if !b.contains((u, v)) && p.exists { case (x, y) => (x - u) * (x - u) + (y - v) * (y - v) == 5 }) 
            yield (u, v)).toList
      kVar += 1
    }
    kVar % 64
  }

  def coord(s: String): (Int, Int) = {
    ("abcdefgh".indexOf(s.charAt(0)), s.charAt(1).asDigit - 1)
  }

  def helper(a: String, b: List[String], c: String): Int = {
    f(coord(a), b.map(coord), coord(c))
  }
  
  def main(args: Array[String]): Unit = {
    println(helper("e3", List(), "f5")) //-> 1
    println(helper("e3", List(), "f6")) //-> 2
    println(helper("c4", List(), "f6")) //-> 3
    println(helper("g1", List("f3", "g3"), "h4")) //-> 4
    println(helper("c4", List("d6", "b6", "e5", "e3", "e4"), "f6")) //-> 5
    println(helper("a1", List(), "h8")) //-> 6
    println(helper("g1", List("h3","h5","g2","g3","g6","f3", "e5", "e6", "d4", "d5", "c1", "c3", "c5", "b2"), "b7")) //-> 7
    println(helper("h1", List("f2", "g3"), "h3")) //-> 0
    println(helper("d4", List("b3", "b5", "c2", "c6", "e2", "e6", "f3", "f5"), "a8")) //-> 0
  }
}