数値成長ブレードをゴルフ


23

編組の説明

このブレードでは、ストランドが別のストランドの上部を横切ると、他のストランドの値がそれ自体に追加され、他のすべてのストランド値が通過します。編組には3つのストランドがあり、各ストランドは1から始まります。最初のクロスオーバーは、中央のストランドを横切る最も左のストランドです。次のクロスオーバーは、新しい中央のストランド(以前は左端のストランド)を横切る右端のストランドです。クロスオーバーのこれら2つのステップが繰り返されます。つまり、最初のクロスオーバーはで[a, b, c] -> [b, a+b, c]あり、2番目のクロスオーバーはです[a, b, c] -> [a, b+c, b]。ここでこれらのルールを使用すると、組み紐の最初の6つのレベルになります。

1,1,1
1,2,1
1,3,2
3,4,2
3,6,4
6,9,4

あなたのタスク

編組レベルとして整数を受け入れ、編組のそのレベルの3つの値を出力するゴルフプログラムまたは関数を作成します。レベルがゼロベースか1ベースかを示す必要があります。入力と出力は任意の適切な形式で入力でき、末尾の空白は許可されます。

テストケース(1ベース)

1 -> 1,1,1

2 -> 1,2,1

5 -> 3,6,4

10 -> 28,41,19

回答:


7

MATL18 17 16バイト

7Bi:"2:4PB@EX!Y*

入力は0ベースです。

オンラインでお試しください!または、すべてのテストケースを確認します

説明

行ベクトルが与えられた[a b c]場合、次のベクトルは、行列の乗算後に取得されます。

[1 0 0;
 0 1 1;
 0 1 0]

または

[0 1 0;
 1 1 0;
 0 0 1]

反復インデックスが奇数か偶数かによって異なります。たとえば、行列積[1 3 2]*[0 1 0; 1 1 0; 0 0 1]はを与え[3 4 2]ます。次に、など[3,4,2]*[1 0 0; 0 1 1; 0 1 0]を与え[3 6 4]ます。

また、2番目のマトリックスは、最初の180度回転したものに等しいことに注意してください。これは、数バイトを節約するために活用できます。

7        % Push 7
B        % Convert to binary. Gives [1 1 1]. This is the initial level
i        % Take input n
:        % Push range [1 2 ... n]
"        % For each
  5      %   Push 5
  I:     %   Push range [1 2 3]
  -      %   Subtract, element-wise: gives [4 3 2]
  B      %   Convert to binary. This gives the matrix [1 0 0; 0 1 1; 0 1 0]
  @      %   Push current iteration index
  E      %   Multiply by 2. Gives 2 in the firt iteration, 4 in the second etc
  X!     %   Rotate matrix 90 degrees either 2 or 0 times
  Y*     %   Matrix multiply
         % End. Implicitly display

ステップのペアリングを検討しましたか?そのように、あなたは1つの行列を持ち[[0, 1, 0], [1, 1, 1], [1, 1, 0]]、異なる開始位置は、偶数と奇数のために非常に似ているn
ピーター・テイラー

@PeterTaylorアイデアをありがとう。この場合、初期ベクトルを変化させると2で入力を分割する複数バイトコストがかかると思われる
ルイスMendo

5

Haskell、51バイト

f p@(a,b,c)=p:(b,a+b,c):f(b,a+b+c,a+b)
(f(1,1,1)!!)

これは、0ベースのインデックスを使用します。使用例:(f(1,1,1)!!) 10-> (28,60,41)

f三つ編みの無限リストを作成し(f(1,1,1)!!)、n番目のトリプルを選択します。fそれ自体は、引数のリストを作成する単純な再帰であり、その後に左のクロスオーバーと、左と右のクロスオーバーを伴う再帰呼び出しが続きます。


4

ルビー、60 57バイト

->n{f=->x{x<2?1:f[x-1]+f[x-3]};[f[n-2|1],f[n],f[n-1&-2]]}

レベルは1ベースです。

次の式に基づきます。

f(-1) = 1
f(0)  = 1
f(1)  = 1
f(x)  = f(x-1) + f(x-3)

braid(x) = {
    [f(x-1), f(x), f(x-2)]  if x is even,
    [f(x-2), f(x), f(x-1)]  if x is odd
}

Neilに3バイトオフしてくれて、気の利いたビットワイズシェナンガンがあります。


1
ビット演算子FTW: [f[n-2|1],f[n],f[n-1&-2]]
ニール

@ニールそれはとてもきれいです、ありがとう!
ドアノブ


3

ゼリー、14バイト

Ḋ+\;Ḣ
6BÇ⁸¡Ṛ⁸¡

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

使い方

6BÇ⁸¡Ṛ⁸¡  Main link. Argument: n (integer)

6B        Convert 6 to binary, yielding [1, 1, 0], which is the braid at index 0.
  Ç⁸¡     Call the helper link n times.
     Ṛ⁸¡  Reverse the resulting array n times.


Ḋ+\;Ḣ     Helper link. Argument: [a, b, c] (integer array)

Ḋ         Dequeue, yielding [b, c].
 +\       Cumulative sum, yielding [b, b+c].
   ;Ḣ     Concatenate with the head of [a, b, c], yielding [b, b+c, a].

2

TI-Basic、58バイト

1ベース。

Prompt N
{1,1,1
For(I,1,Ans
If fPart(I/2
Then
{Ans(2),Ans(1)+Ans(2),Ans(3
Else
{Ans(1),Ans(2)+Ans(3),Ans(2
End
End

この58バイトはどうですか?114を数えます。何か不足していますか?
ブリアンティスト16

@briantist TI-Basicコマンドは、1または2バイト長です。たとえばPrompt、2バイトのコマンドです。
ジョンファンミン

@JungHwanMinクール、気づかなかった。見えないものがあると感じました。なじみのない他の人にコメントを残します。
ブリアンティスト16

2
どのトークンが1バイトまたは2バイトであるかを確認するには、tibasicdev.wikidot.com / tokens
Timtech

@JungHwanMin Prompt は1バイトのみです。しかし、トークンの概念を説明してくれてありがとう:)
Timtech

2

PowerShell 2 +、75バイト

1ベースのインデックス

$a=1,1,0;1..$args[0]|%{$d=(0,2)[$_%2];$a[1],$a[$d]=($a[1]+$a[$d]),$a[1]};$a

オンラインでお試しください!またはすべてのテストケースを試してください!

ループは常に1回実行されるため、ブレードレベルの場合は、makeを使用してアルゴリズムの結果を1配列で開始します。1,1,01,1,1

$a[1](常に真ん中され、その後、私は他の要素のインデックスかどうかを判断する$dことになるだろう)、0または2現在のレベルが偶数か奇数かに基づきます。PowerShellは一度に複数の割り当てをサポートするので、$x,$y=$y,$x基本的には配列要素を使用して行うのと同じくらい簡単にスワッピングができ、その割り当て内に追加を埋め込むだけです。


2

Javascript(ES6)、55バイト

x=>(f=y=>y<2?1:f(y-1)+f(y-3),[f(x-2|1),f(x),f(x-1&-2)])

repl.it

1ベース

これは、@ Neilの素晴らしいビットワイズゴルフでの@DoorknobのRuby回答の単なる移植版です。


1

Befunge、64バイト

110p100p1&v
01:\_v#:-1<\p00<v\+g
..g.@>_10g
\1-:!#^_\:00g+\v>10p

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

説明

110p                Initialise a to 1 (at location 1;0).  
    100p            Initialise c to 1 (at location 0;0).
        1           Initialise b to 1 (on the stack, since it'll be most frequently used).
         &v         Read n from stdin and turn down.

          <         The main loop starts here, executing right to left.
        -1          Decrement n.
    _v#:            Duplicate and check if zero; if not, continue left.
   \                Swap b to the top of the stack, leaving n below it.
01:            g    Make a duplicate copy, and read a from memory (at location 1;0). 
              +     Add a to b, the result becoming the new b.
            v\      Swap the original b to the top of the stack and turn down.
            >10p    Turn around and save the original b as a (at location 1;0).
\1-                 Swap n back to the top of the stack and decrement.
   :!#^_            Duplicate and check if zero; if not continue right.
        \           Swap b to the top of the stack, leaving n below it.
         :00g       Make a duplicate copy, and read c from memory (at location 0;0).
             +      Add c to b, the result becoming the new b.
              \v    Swap the original b to the top of the stack and turn down.
            p00<    Turn around and save the original b as c (at location 0;0).
           \        Swap n back to the top of the stack.
          <         And repeat the loop from the beginning.

      >             If either of the zero tests succeed, we end up on line 3 going right.
       _            This just drops the n that is now zero, and continues to the right.
        10g         Read the final value of a (at location 1;0).
..                  Output a along with the b that was already on the stack.
  g                 Read the final value of c (the 0;0 location is implied).
   .@               Output c and exit.


1

Java 8、121

これは、1ベースのレベルを使用します。

(int l)->{int a=1,b=a,c=a,i=a;while(i<l)if(i++%2>0){b^=a;a^=b;b=(a^b)+a;}else{b^=c;c^=b;b=(c^b)+c;}return a+","+b+","+c;}

Ungolfed、テストプログラム付き:

import java.util.function.IntFunction;

public class GolfANumericalGrowingBraid {

  public static void main(String[] args) {
    for (int level : new int[] { 1, 2, 5, 10 }) {
      output((int l) -> {
        int a = 1, b = a, c = a, i = a;
        while (i < l) {
          if (i++ % 2 > 0) {
            b ^= a;
            a ^= b;
            b = (a ^ b) + a;
          }
          else {
            b ^= c;
            c ^= b;
            b = (c ^ b) + c;
          }
        }
        return a + "," + b + "," + c;
      } , level);
    }
  }

  private static void output(IntFunction<String> function, int level) {
    System.out.println(function.apply(level));
  }
}

出力:

1,1,1
1,2,1
3,6,4
28,41,19


0

GameMaker言語、113バイト

Doorknobの再帰的ソリューションに基づいた、インデックスが1つ。GameMakerでプリミティブ配列を一度に初期化できない理由を尋ねないでください、私は本当に知りません...

メインプログラム(69バイト):

a=argument0 if a mod 2c=1b[0]=a(a-c-1)b[1]=a(a)b[2]=a(a+c-2)return b

サブプログラムa(46バイト):

a=argument0 if a<2return 1return a(a-1)+a(a-3)

0

Perl 6、60バイト

{(1 xx 3,->[\a,\b,\c]{$++%2??(a,b+c,b)!!(b,b+a,c)}...*)[$_]}

ゼロベース。

レイジー無限シーケンスを直接生成し、インデックスを作成します。
おそらくより良いアプローチがあります。


0

Clojure、98バイト

#(ffirst(drop %(iterate(fn[[v[a b c d]]][[(v a)(+(v b)(v c))(v d)][b a d c]])[[1 1 0][0 1 2 1]])))

現在の値vと、次のラウンドのためにどの位置から合計を行う必要があるかを追跡します。[1 1 1]1ベースのインデックスを作成する前に1つの状態を開始します。


0

C#88 86バイト

f(int n,int a=1,int b=1,int c=1)=>n>1?n--%2>0?f(n,b,a+b,c):f(n,a,b+c,b):a+","+b+","+c;

説明

f(int n,int a=1,int b=1,int c=1)=>  //Using an expression bodied function to allow for defaults and remove return statement
    n>1?                            //recurse or return result
        n--%2>0?                    //get odd or even then decrement n
            f(n,b,a+b,c)            //odd recursion
           :f(n,a,b+c,b)            //even recursion
       :a+","+b+","+c;              //build output

0

Mathematica、68バイト

If[#<3,{1,#,1},{{#,+##2,#2}&,{#2,#+#2,#3}&}[[Mod[#,2,1]]]@@#0[#-1]]&

名前のない関数の単純な再帰的定義。正の整数引数を取り、3つの整数の順序付きリストを返します。

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