ヒドラスレイヤーの帰還


13

あなたがそのヒドラを殺してからしばらく経ちましたが、あなたは長年栄光を浴びていましたが、今では人々はあなたを洗い流していると呼んでいます。さて、あなたは彼らが間違っていることを証明する時、あなたは別のヒドラの行方を聞いたことがあります。単にそれを殺すと、あなたはあなたが値するすべての栄光を授与されます。

あなたは剣を受け取るために武器庫に到着しますが、それらはすべて通常の剣ではなく、残っているのはセクターだけです。nセクターはHydraの頭の数をnで除算しますが、頭の数がnで割り切れる場合にのみ使用できます。

もう一度、あなたはヒドラを殺すのを助けるためにいくつかのコードを書くつもりです。コードは、ヒドラの頭の数を入力として受け取り、戦いを開始し、ヒドラが毎ターン成長する頭の数、および使用できるnセクターのリストを入力します。あなたのコードは、可能な限り迅速にヒドラを殺すための動きの最適なパターンを出力します

戦いの各ターンでは、1つのスライスを選択して、スライスの後にヒドラの頭が1つだけの場合は勝ち、そうでない場合は頭を成長させます。あなたは決して動きをすることはできませんし、可能な動きがなければ損失します。

解決策が不可能な場合は、解決策以外の何か、たとえば空のリスト、何もない、数字のゼロなどを出力できます。

これはため、回答はバイト数として記録され、少ない方が良い結果が得られます。

テストケース

ここにいくつかの超基本的なテストケースがありますが、リクエストに応じてさらにテストケースが追加されます。

24 heads, 1  heads per turn, [2,3] -> [3,3,2,3]
25 heads, 2  heads per turn, [2,3] -> No solutions
4  heads, 2  heads per turn, [2]   -> No solutions
4  heads, 3  heads per turn, [2,5] -> [2,5]
10 heads, 17 heads per turn, [2, 3, 7, 19] -> No solutions
10 heads, 6  heads per turn, [1,16] -> [1,16]
6  heads, 2  heads per turn, [2, 3, 5] -> [2, 5]
125 heads, 1  head per turn, [1, 2, 3, 127] -> [1, 1, 127]

ヒドラの頭は1つだけですか?
-ETHproductions

@ETHproductionsそのケースを処理する必要はありません。
ポストロックガーフハンター

リストがソートされていると仮定できますか?
-ETHproductions

@ETHproductionsはい、できます。なぜだか分かりません。
ポストロックガーフハンター

1セクターは基本的に「スキップターン」の剣ですか?
ニール

回答:


5

JavaScriptの(ES6)、111の 105バイト

@ThePirateBayのおかげで4バイト節約

(h,p,a)=>{for(b={[h]:[]};c=b,b=[];)for(d in c)for(e of a){d%e?0:q=b[d/e+p]=[...c[d],e];if(d==e)return q}}

私は、より安全な幅優先ループに使用しようとした深さ優先の再帰を捨てました。存在する場合はソリューションを配列として出力し、存在しない場合は永久に実行します。これが許容できない場合、最終的に停止するものがあります(ほとんどの場合、とにかく):

(h,p,a)=>{for(b={[h]:[]};c=b,b=[],c+c;)for(d in c){for(e of a){a[[,d]]||d%e?0:q=b[d/e+p]=[...c[d],e];if(d==e)return q}a[[,d]]=1}}

3

JavaScript、191190バイト

Step Henのおかげで1バイト節約

(h,t,s)=>eval(`r=[],d=0,q=[],s.map(a=>q.push([],h));while(q.length){p=q.shift(),h=q.shift(),s.map(w=>(a=h/w)==1?d=w:!(a%1)&!r[a+=t]?r[q.push([...p,w],a),a]=1:0);d?(q=[],p).push(d):0}d?p:[]`)

f=(h,t,s)=>eval(`r=[],d=0,q=[],s.map(a=>q.push([],h));while(q.length){p=q.shift(),h=q.shift(),s.map(w=>(a=h/w)==1?d=w:!(a%1)&!r[a+=t]?r[q.push([...p,w],a),a]=1:0);d?(q=[],p).push(d):0}d?p:[]`)

console.log(`[${f(24, 1, [2,3])}]`);
console.log(`[${f(25, 2, [2,3])}]`);
console.log(`[${f(4, 2, [2])}]`);
console.log(`[${f(4, 3, [2,5])}]`);
console.log(`[${f(10, 17, [2, 3, 7, 19])}]`);
console.log(`[${f(10, 6, [1,16])}]`);
console.log(`[${f(125, 1, [1, 16])}]`);
console.log(`[${f(1024, 3, [1, 2, 137])}]`);



2

パイソン2169の 195 222バイト

不良な武器の選択でサイクリックヘッドの再生を適切に処理するための+26バイト。(指摘してくれた@ThePirateBayに感謝)

エラーの原因となる特定のエッジケースを修正するための+27バイト。

lambda n,p,w:g(n,n,p,w[::-1])[:-1]
def g(n,b,p,w,a=[]):
 if b<2:return[1]
 for x in w:
	if n%x<1and n/x+p!=n and n not in a:
	 try:
		l=[x]+g(n/x+p,n/x,p,w,[n]+a)
	 	if l and l[-1]!=0:return l
	 except:return[0]
 return[0]

オンラインでお試しください!


通常、resuableビットは、グローバル変数を作成、変更、および次回に元の値に戻ることを想定できないことを意味します。Dunnoは、ここでのエラーに関するポリシーを説明しています。完全なプログラムは、空の出力でエラーが発生する可能性があります。
スティーブン



1

VB.NET(.NET 4.5)、439 + 35(インポート)= 474バイト

が必要です Imports System.Collections.Generic

Const N=Nothing
Function Z(h,r,a,Optional c=N,Optional p=N,Optional ByRef s=N)
If c Is N Then
c=New List(Of Long)
p=New List(Of Long)
End If
If s IsNot N And s?.Count<c.Count Then Return N
If p.Contains(h)Then Return N
p.Add(h)
For i=0To a.Count-1
Dim w=a(i)
If h Mod w=0Then
c.Add(w)
If h\w=1And(s Is N Or s?.Count>c.Count)Then
s=New List(Of Long)
s.AddRange(c)
End If
Z(h\w+r,r,a,c,p,s)
c.RemoveAt(c.Count-1)
End If
Next
Z=s
End Function

この関数Zは2つInt64(ヘッドの数とヘッドの再成長率)と1つのList(Of Int64)(セクター)をList(Of Int64) (the ordered choice of Sectors). Returns取り、解決策がない場合はNothing` を返します。

セクターは、最大から最小にソートされた順序で表示されると想定しています。

Optional再帰呼び出しは、状態を保存するためのパラメータがあります。使用中のセクターの現在の順序、これまでで最も短いセクターの順序、およびこれまでに遭遇したヘッドの数を追跡します。同じ数の頭に再び出くわす場合は、もっと短い方法で到達する必要があります。

重要なセクターの唯一の順序は1、存在する場合は最後にする必要があるということです。そうしないと、ヒドラが無限に成長する可能性があります。なぜなら、私は毎ターン1セクターを使用するだけで、他のものを試すことは決してできないからです。

使用するたびに6バイトを削ることNを表す定数を宣言しました。NothingNothing

And/ Orはショートサーキットではないため、ヌル条件演算子(?.)を使用してオブジェクトのヌルエラーを回避します。実際のコードでは、短絡を行うAndAlso/ OrElseを使用します。

オンラインでお試しください!


Z 読みやすさのためにゴルフをしていません

Public Function Z(currentHeads As Long, regrowRate As Integer, weapons As ISet(Of Long), Optional currentWeapons As List(Of Long) = Nothing, Optional previousHeads As List(Of Long) = Nothing, Optional shortestWeapons As List(Of Long) = Nothing) As List(Of Long)

    ' initial call
    If currentWeapons Is Nothing Then
        currentWeapons = New List(Of Long)
        previousHeads = New List(Of Long)
    End If

    ' we've made more moves than our best so far
    If shortestWeapons IsNot Nothing AndAlso shortestWeapons.Count <= currentWeapons.Count Then
        Return Nothing
    End If

    ' exit, we've been here before
    If previousHeads.Contains(currentHeads) Then
        Return Nothing
    End If

    ' keep track of previous state to prevent duplicate paths
    previousHeads.Add(currentHeads)

    For Each w In weapons

        ' save 1 for last
        If w = 1 Then Continue For

        If currentHeads Mod w = 0 Then
            currentWeapons.Add(w)

            If currentHeads \ w = 1 Then
                If shortestWeapons Is Nothing OrElse shortestWeapons.Count > currentWeapons.Count Then
                    shortestWeapons = New List(Of Long)(currentWeapons)
                End If
            End If

            Dim answer = A(currentHeads \ w + regrowRate, regrowRate, weapons, currentWeapons, previousHeads, shortestWeapons)
            If answer IsNot Nothing Then
                If shortestWeapons Is Nothing OrElse shortestWeapons.Count > answer.Count Then
                    shortestWeapons = New List(Of Long)(answer)
                End If
            End If

            currentWeapons.RemoveAt(currentWeapons.Count - 1)
        End If
    Next

    If weapons.Contains(1) Then
        currentWeapons.Add(1)

        Dim answer = A(currentHeads \ 1 + regrowRate, regrowRate, weapons, currentWeapons, previousHeads, shortestWeapons)
        If answer IsNot Nothing Then
            If shortestWeapons Is Nothing OrElse shortestWeapons.Count > answer.Count Then
                shortestWeapons = New List(Of Long)(answer)
            End If
        End If

        currentWeapons.RemoveAt(currentWeapons.Count - 1)
    End If

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