犬を散歩に連れて行く


14

私の犬は私にbarえていますが、私は彼を散歩に連れて行くのが面倒です。考えがある!コードゴルフの人々に私のためにやってもらいましょう!

あなたの課題は、犬の散歩をシミュレートすることです。犬の散歩のルールは次のとおりです。

  • 人間(H)は0,0(デカルト)座標平面から開始し、1秒ごとにランダムに1スペース上、左、右、または下に移動します。
  • 犬(D)は同じ位置から始まり、毎秒0、1、2、または3つのスペースを上、左、右、または下に(もちろんランダムに)行きます。犬は予測が難しく、時には速く走ったり、完全に止まったりします。
  • 犬は、人間から一定量(ユークリッド距離)以上離れることはありません。これは、綱の長さ(L)です。たとえば、Lisの6場合、有効な位置はH(0,0) D(4,4)(距離が約5.65ユニットであるため)ですが、そうではありませんH(0,0) D(5,4)(約6.4ユニット)。
    • 犬が動いたときにリーシュ距離要件に違反する場合、犬は、距離要件に違反しない決定された方向に可能な限り遠くに行かなければなりません。たとえば、位置がH(0,0) D(3,4)あり、犬がランダムに3スペース右に移動することに決めたD(4,4)場合、6ユニット以上離れることなく移動できるため、犬はに移動します。(これにより、スペースが0移動する、つまりまったく移動しないことに注意してください!)
  • その他のルール:人間が最初に動きます。人間は、その動きで綱の長さを超えてはいけません。その場合、別の方向を選択する必要があります。「ランダム」とは、「均一に分布し、パターンがない」ことを意味します。

入力は次の形式で提供されます(STDIN、関数パラメーター、ファイルなど)。

"<amount of seconds the walk will last> <leash length>"

例えば:

300 6 // a 5 minute walk with a 6 yard leash

次の形式で出力する必要があります(STDOUT、関数の戻り値、ファイルなど)。

H(0,0) D(0,0)
H(0,1) D(2,0)
H(1,1) D(2,1)
H(1,0) D(2,-1)
...

(行数はカウントされないseconds + 1ため、出力されるH(0,0) D(0,0)行数はになります。)

これはなので、バイト単位の最短コードが勝ちます!


4
明らかにあなたが意図したことですが、とにかくそれらを明示的に述べることはおそらく良いでしょう:1.距離はユークリッド距離を意味します。明確でないもの:3.動きがリーシュの長さを超える場合、人間は何をしますか?4.各ターンで、犬と人間のどちらが先に動きますか?
ジョナサンヴァンマトレ14年

選択した方向が有効な移動を許可しない場合:移動を行わないか、別の方向を選択する必要がありますか?
ティムウォラ14年

入力はどのように取得する必要がありますか?STDIN?関数パラメーターも大丈夫ですか?
ティムウォラ14年

1
私の犬は、散歩に出かけようとしているときだけ、私に向かってbarえます。
TheDoctor

わかりやすくするために@Tim編集。ありがとう。
ドアノブ

回答:


2

GolfScript、140文字

~.*:L;)[0.].@{['H('1$','*') D('4$','*')'n]@@{[@~]}+[{)}{(}{\(\}{\)\}]:^%{{~2$~@-.*@@-.*+L>!},}:F~.,{rand}:R~=\[.;{.[~}^4R=+{]}+4R*]F-1=\}*;;

実行例(オンラインで試す):

> 10 3
H(0,0) D(0,0)
H(0,1) D(0,0)
H(0,0) D(0,2)
H(0,1) D(-1,2)
H(1,1) D(-1,2)
H(1,2) D(-1,4)
H(1,3) D(-1,5)
H(0,3) D(-1,5)
H(-1,3) D(1,5)
H(-1,4) D(1,5)
H(0,4) D(-1,5)

6

CoffeeScript-324 +関数呼び出し

デモ(Leash 10を使用):

H(0,0) D(0,0)
H(0,-1) D(0,3)
H(-1,-1) D(0,3)
H(-1,-2) D(-1,3)
H(-1,-3) D(-4,3)
H(-1,-2) D(-4,4)
H(-1,-3) D(-5,4)
H(-1,-2) D(-2,4)
H(-1,-3) D(-2,5)
H(-1,-4) D(-2,5)
H(-1,-3) D(-3,5)
H(0,-3) D(-4,5)
H(-1,-3) D(-4,6)
H(-1,-4) D(-2,6)
H(-2,-4) D(-2,6)
H(-3,-4) D(-5,6)
H(-4,-4) D(-5,6)
H(-4,-3) D(-5,8)
H(-5,-3) D(-2,8)
H(-5,-2) D(-2,8)
H(-5,-3) D(-2,5)

コード:

w=(i)->
    [s,l]=i.split ' ';m=Math;h=[0,0];d=[0,0];r=->(4*m.random())|0
    a=->m.abs(h[0]-d[0])**2+m.abs(h[1]-d[1])**2<=l**2
    b=(x)->if x%2 then 1else-1
    for i in[0..s]
        console.log "H(#{h}) D(#{d})"
        loop
            H=h.slice();x=r();h[(x/2)|0]+=b x;break if a();h=H
        D=r();x=r();(d[(x/2)|0]+=b x;d[(x/2)|0]-=b x if!a())while D-->0

長いコード:

human = [ 0, 0 ]
doge = [ 0, 0 ]
randomDirection = -> [ 'u', 'd', 'l', 'r' ][(4*Math.random())|0]
allowed = -> Math.abs(human[0] - doge[0]) ** 2 + Math.abs(human[1] - doge[1]) ** 2 <= leash**2
leash = 0
walk = (input) ->
    [ seconds, leash ] = input.split ' '
    for i in [0..seconds]
        console.log "H(#{human}) D(#{doge}) #{Math.sqrt Math.abs(human[0] - doge[0]) ** 2 + Math.abs(human[1] - doge[1]) ** 2}"
        valid = no
        loop
            oldHuman = human.slice()
            switch randomDirection()
                when 'u'
                    human[0]--
                when 'd'
                    human[0]++
                when 'l'
                    human[1]--
                when 'r'
                    human[1]++

            if allowed()
                break
            else
                human = oldHuman

        dogeDistance = (Math.random() * 4)|0
        switch randomDirection()
            when 'u'
                while dogeDistance-- > 0
                    doge[0]--
                    doge[0]++ if !allowed() 
            when 'd'
                while dogeDistance-- > 0
                    doge[0]++
                    doge[0]-- if !allowed() 
            when 'l'
                while dogeDistance-- > 0
                    doge[1]--
                    doge[1]++ if !allowed() 
            when 'r'
                while dogeDistance-- > 0
                    doge[1]++
                    doge[1]-- if !allowed() 
walk "10 2"

4

ルビー、189 177

これを少し落とすことができます。好きじゃないj

h=d=0;e,l=$*
def j;[1,-1,?i,'-i'].sample.to_c end
0.upto(e.to_i){puts'H(%d,%d) D(%d,%d)'%(h.rect+d.rect)
1 while((g=h+j)-d).abs>l.to_i
h,s=g,j
3.times{d+=s if(d+s-h).abs<=l.to_i}}

ゴルフをしていない:

h=d=0 # use complex numbers since it has distance built in
time,leash=$*

def randomDirection
  [1,-1,'i','-i'].sample.to_c 
end

0.upto(time.to_i) { # (1 + time) times
  puts"H(%d,%d) D(%d,%d)"%(h.rect+d.rect) # that's [h.real, h.imag, d.real, d.imag]

  # find a newH that doesn't violate the leash
  newH = h + randomDirection
  while((g-d).abs > leash.to_i) 
    newH = h + randomDirection
  end

  h = newH

  newD = randomDirection
  3.times{
    # keep adding newD until you hit the leash
    d += newD if(d + newD - h).abs <= leash.to_i
  }
}

2

Mathematica 285

ゴルフ

r=RandomChoice;k=False;
s:=r[{{0,1},{-1,0},{1,0},{0,-1}}]
w:=r[{0,1,2,3,4}]
f[{-1,___}]:="";
f[{n_,l_,{h_,d_}}]:=
(Print["H(",h,")\t","D(",d,")"];
m[i_,u_,v_]:=EuclideanDistance[u+i,v]<l;
z=k;While[z==k,t=s;z=m[t,h,d]];
y=h+t;t=w*s;z=k;While[z==k,t=t-Sign@t;z=m[t,d,y]];
dp=d+t;f[{n-1,l,{y, dp}}])

f[{17,7,{{0,0},{0,0}}}]

H({0,0})D({0,0})
H({1,0})D({0,0})
H({2,0})D({0,2})
H( {2、-1})D({0、-1})
H({1、-1})D({-3、-1})
H({1,0})D({-3,1 })
H({1,1})D({0,1})
H({1,2})D({0,2})
H({1,1})D({0,2})
H({1,0})D({-2,2})
H({2,0})D({1,2})
H({2、-1})D({-2,2} )
H({2,0})D({-2,3})
H({2,1})D({-2,0})
H({1,1})D({-2,3 })
H({2,1})D({-2,6})
H({1,1})D({-3,6})
H({0,1})D({-4、 6})


UnGolfed

以下のテキストにいくつかのコメントがあります。また、いくつかの計算に従うことを可能にするプリントアウトも含めました。

step:=RandomChoice[{{0,1},{-1,0},{1,0},{0,-1}}]
dogWander:=RandomChoice[{0,1,2,3,4}]

f[{-1,___}]:="";

f[{n_,l_,{humanPos_,dogPos_}}]:=
Module[{tempStep,moveOK,hp,hp1,dp,p,test},

(* human imagines step, checks with leash, chooses another step if would choke the dog *)
moveOK[intendedStep_,pos1_,pos2_]:=EuclideanDistance[pos1+intendedStep,pos2]<l;
test=False;While[test==False,tempStep=step;test=moveOK[tempStep,humanPos,dogPos]];
hp=humanPos+tempStep;
Print["humanPos before: ", humanPos,"\t","human step: ",tempStep,"\thumanPos after: ",hp,"\tdistance from dog: ",N@EuclideanDistance[hp,dogPos]];

(*move closer to human if needed *)
tempStep=dogWander*step;
test=False;While[test==False,tempStep=tempStep-Sign[tempStep];test=moveOK[tempStep,dogPos,hp]];
dp=dogPos+tempStep;
Print["dog's intended step: ",tempStep,"\tdogPos before: ",dogPos,"\t","dog actual step: ",tempStep, "\t","dogPos after: ",dp,"\tdistance: ",N@EuclideanDistance[hp,dp],"\n"];
f[{n-1,l,{hp, dp}}]]

f[{17,5,{{0,0},{0,0}}}]

[印刷の一部は、コードがどのように機能するかの感覚を与えるために示されています。]

プリントアウト

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.