有効なバドミントンスコア?


27

前書き:

バドミントンに関連する他のチャレンジは、たった1つしかありませんでした。自分でバドミントンをしているので(過去13年間)、バドミントンに関連するいくつかの課題を追加すると思いました。ここで最初のもの:

チャレンジ:

入力: 2つの整数
出力:任意の3つの異なる一意の出力の1つ。入力が有効なバドミントンスコアであり、セットが勝者で終了したことを示すもの。入力が有効なバドミントンスコアであり、セットがまだ使用中であることを示すもの。入力が有効なバドミントンスコアではないことを示すもの。

バドミントンでは、両方のプレーヤー(ペア)が0ポイントで開始し、2人のプレーヤー(ペア)のうちの1人が少なくとも2ポイント差で最大30-29までのスコア21に達したら停止します。

したがって、これらは有効なバドミントンスコアであり、セットが終了したことを示すすべての可能な入力ペアです(どちらの順序でも)。

[[0,21],[1,21],[2,21],[3,21],[4,21],[5,21],[6,21],[7,21],[8,21],[9,21],[10,21],[11,21],[12,21],[13,21],[14,21],[15,21],[16,21],[17,21],[18,21],[19,21],[20,22],[21,23],[22,24],[23,25],[24,26],[25,27],[26,28],[27,29],[28,30],[29,30]]

そして、これらはすべて有効なバドミントンスコアであることを示す入力ペア(いずれの順序でも)ですが、セットはまだ有効です:

[[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1,12],[1,13],[1,14],[1,15],[1,16],[1,17],[1,18],[1,19],[1,20],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7],[2,8],[2,9],[2,10],[2,11],[2,12],[2,13],[2,14],[2,15],[2,16],[2,17],[2,18],[2,19],[2,20],[3,3],[3,4],[3,5],[3,6],[3,7],[3,8],[3,9],[3,10],[3,11],[3,12],[3,13],[3,14],[3,15],[3,16],[3,17],[3,18],[3,19],[3,20],[4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],[4,11],[4,12],[4,13],[4,14],[4,15],[4,16],[4,17],[4,18],[4,19],[4,20],[5,5],[5,6],[5,7],[5,8],[5,9],[5,10],[5,11],[5,12],[5,13],[5,14],[5,15],[5,16],[5,17],[5,18],[5,19],[5,20],[6,6],[6,7],[6,8],[6,9],[6,10],[6,11],[6,12],[6,13],[6,14],[6,15],[6,16],[6,17],[6,18],[6,19],[6,20],[7,7],[7,8],[7,9],[7,10],[7,11],[7,12],[7,13],[7,14],[7,15],[7,16],[7,17],[7,18],[7,19],[7,20],[8,8],[8,9],[8,10],[8,11],[8,12],[8,13],[8,14],[8,15],[8,16],[8,17],[8,18],[8,19],[8,20],[9,9],[9,10],[9,11],[9,12],[9,13],[9,14],[9,15],[9,16],[9,17],[9,18],[9,19],[9,20],[10,10],[10,11],[10,12],[10,13],[10,14],[10,15],[10,16],[10,17],[10,18],[10,19],[10,20],[11,11],[11,12],[11,13],[11,14],[11,15],[11,16],[11,17],[11,18],[11,19],[11,20],[12,12],[12,13],[12,14],[12,15],[12,16],[12,17],[12,18],[12,19],[12,20],[13,13],[13,14],[13,15],[13,16],[13,17],[13,18],[13,19],[13,20],[14,14],[14,15],[14,16],[14,17],[14,18],[14,19],[14,20],[15,15],[15,16],[15,17],[15,18],[15,19],[15,20],[16,16],[16,17],[16,18],[16,19],[16,20],[17,17],[17,18],[17,19],[17,20],[18,18],[18,19],[18,20],[19,19],[19,20],[20,20],[20,21],[21,21],[21,22],[22,22],[22,23],[23,23],[23,24],[24,24],[24,25],[25,25],[25,26],[26,26],[26,27],[27,27],[27,28],[28,28],[28,29],[29,29]]

整数のその他のペアは、無効なバドミントンスコアになります。

チャレンジルール:

  • I / Oは柔軟なので、次のとおりです。
    • 入力を2つの数字のリストとして取得できます。STDINまたは関数パラメーターを介した2つの分離された数値。2つの文字列。等
    • 出力は、選択した3つの異なる一意の値になります。整数(すなわちすることができ[0,1,2][1,2,3][-1,0,1]、など)。ブール値(つまり[true,false,undefined/null/empty])にすることができます。文字/文字列(つまり["valid & ended","valid","invalid"])にすることができます。等
    • 回答で使用したI / Oを指定してください!
  • 入力整数は、最低から最高、またはその逆に事前に順序付けすることができます。
  • 入力整数は負になる場合があり、その場合はもちろん無効です。

一般的なルール:

  • これはであるため、バイト単位の最短回答が優先されます。
    コードゴルフ言語では、非コードゴルフ言語で回答を投稿しないようにしてください。「任意の」プログラミング言語の可能な限り短い答えを考えてみてください。
  • デフォルトのI / Oルールを使用した回答には標準ルールが適用されるため、STDIN / STDOUT、関数/メソッド、適切なパラメーターおよび戻り値型、完全なプログラムを使用できます。あなたの電話。
  • デフォルトの抜け穴は禁止されています。
  • 可能であれば、コードのテスト(TIOなど)へのリンクを追加してください。
  • また、回答の説明を追加することを強くお勧めします。

テストケース:

これらのテストケースは有効であり、セットは終了しています。

0 21
12 21
21 23
28 30
29 30

これらのテストケースは有効ですが、セットはまだ使用中です。

0 0
0 20
12 12
21 21
21 22

これらのテストケースは無効です。

-21 19
-19 21
-1 1
12 22
29 31
30 30
42 43
1021 1021

回答:


1

スタックス、20 バイト

ÇåπßéD╩¬7▼ß▌ΣU¬í╡S┤╘

実行してデバッグする

例と同じ形式で入力を受け取ります。 0有効な勝者がいることを意味します。1ゲームが進行中であることを意味します。-1無効なスコアを意味します。

擬似コードでは、順序付けされた入力xyで、アルゴリズムは

sign(clamp(x + 2, 21, 30) - y) | (x < 0 || x >= 30 ? 0 : -1)
  • sign意味数値記号(-10、または1
  • clamp 最初の引数を指定された半開間隔に強制します

6

パイソン297 95 75 72 71 70 69 64 55 54の 52 51 50 48バイト

lambda a,b:(b-61<~a<a>b/22*b-3)*~(19<b-(b<30)>a)

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

入力をpre-orderedとして受け取りますa,b

戻り値-2-10のためにendedin playinvalid

-1バイト、Kevin Cruijssenのおかげ


左の部分(b-61<~a<a>b/22*b-3)は有効性チェックで、右の部分(19<b-(b<30)>a)はゲーム終了のチェッ​​クです。


6

Python 2、47バイト

lambda a,b:[61>60-a>b<3+max(19,a)for b in-~b,b]

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

2つのブール値のリストを出力します。おかげTFeldでテストスイート書くための彼らの答え、それは簡単に私の解決策を確認するために作られました。

ended: [False, True]
going: [True, True]
invalid: [False, False]

重要な洞察は、より高い値bを増やすとスコアが無効になる場合、有効なスコアはゲームを正確に終了するということです。そのため、妥当性条件をコーディングし、それを確認します(a,b+1)さらに(a,b)ゲームが終了したかどうかを確認します。

有効性は、連鎖している3つの条件によってチェックされます。

  • b<3+max(19,a):より高いスコアがあることをチェックb優勝過去、のいずれかではないb<=21か、b<=a+2(2で勝利)
  • 60-a>b:に相当しa+b<=59、スコアが上にならないようにします(29,30)
  • 61>60-a:と同等でa>=0、低いスコアが負でないことを保証します

Python 2、44バイト

lambda a,b:[b-61<~a<a>b/22*b-3for b in-~b,b]

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

TFeldによる有効性チェックの改善により、3バイトが節約されます。主なアイデアは、21 時間未満のスコアを実質的にゼロに設定する「残業」b>21b/22*b分岐することですが、私a>19はより長いで分岐しmax(19,a)ます。


Python 2、43バイト

lambda a,b:a>>99|cmp(2+max(19,a)%30-a/29,b)

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

出力:

ended: 0
going: -1
invalid: 1

299


1
最新の有効性チェック(b-61<~a<a>b/22*b-3)を使用すると、3バイトを節約できます。
TFeld

1
2番目のソリューションがすべての入力に対して機能するように+1バイト:lambda a,b:-(a<0)|cmp(2+max(19,a)%30-a/29,b)
TFeld



4

ゼリー、25バイト

»19«28‘<‘×+2>ɗʋ⁹×,%Ƒ“œþ‘ɗ

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

左引数:最小。正しい引数:最大。
無効:0。継続中:1。終了しました:2

バツy

[a]={a:1¬a:0(a,b)=(amod30,bmod31)x,yZX:=min(max(x+1,20),29)p:=(x,y)([X<y]+1)[X+2>y][p=p]

説明:

»19«28‘<‘×+2>ɗʋ⁹×,%Ƒ“œþ‘ɗ Left argument: x, Right argument: y
»19«28‘                   X := Bound x + 1 in [20, 29]:
»19                         X := max(x, 19).
   «28                      X := min(X, 28).
      ‘                     X := X + 1.
       <‘×+2>ɗʋ⁹          X := If X + 2 <= y, then 0, else if X < y, then 2, else 1:
       <                    t := If X < y, then 1, else 0.
        ‘                   t := t + 1.
          +2>ɗ              u := Check if X + 2 > y:
          +2                  u := X + 2.
            >                 u := If u > y, then 1, else 0.
         ×                  X := t * u.
                 ,%Ƒ“œþ‘ɗ z := If x mod 30 = x and y mod 31 = y, then 1, else 0:
                 ,          z := (x, y).
                  % “œþ‘    m := z mod (30, 31) = (x mod 30, y mod 31).
                   Ƒ        z := If z = m, then 1, else 0.
                ×         X * z.

3

VDM-SL、80バイト

f(i,j)==if(j-i>2and j>21)or(i<0or i=30or j>30)then{}else{(j>20and j-i>1or j=30)} 

この関数は、昇順に並べられたスコアを取得し、スコアが無効な場合は空のセットを返し、セットが完全かどうかを含むセットを返します(セットが完全で有効な場合は{true}、セットが不完全な場合は{false}有効)

実行する完全なプログラムは次のようになります。

functions
f:int*int+>set of bool
f(i,j)==if(j-i>2and j>21)or(i<0or i=30or j>30)then{}else{(j>20and j-i>1or j=30)}

説明:

if(j-i>2 and j>21)             /*if scores are too far apart*/
or(i<0 or i=30 or j>30)        /*or scores not in a valid range*/
then {}                        /*return the empty set*/
else{                       }  /*else return the set containing...*/
     (j>20 and j-i>1 or j=30)  /*if the set is complete*/

3

Java(JDK)59 48バイト

a->b->b<0|b>29|a>b+2&a>21|a>30?0:a<21|a<30&a<b+2

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

を返しますObject。これは、Integer 0無効なゲームとBooleans trueおよびfalse有効な進行中のゲームと有効な終了したゲームのそれぞれに対してです。順序付けられた(そしてカレーされた)スコアを取得し、高いスコアが最初になります。

-2 bytes 一致の終了チェックを反転します。
-11 bytesカリー化、ビット演算子の使用、およびいくつかの戻り値型のオートボクシングトリック-@KevinCruijssenに感謝

非ゴルフ

a->                      // Curried: Target type IntFunction<IntFunction<Object>>
    b->                  // Target type IntFunction<Object>
                         // Invalid if:
            b<0          //    Any score is negative
          | b > 29       //    Both scores above 29
          |   a > b + 2  //    Lead too big
            & a > 21     //        and leader has at least 21 points
          | a > 30       //    Anyone has 31 points
        ? 0              // If invalid, return 0 (autoboxed to Integer)
                         // If valid, return whether the game is ongoing (autoboxed to Boolean)
                         // Ongoing if:
        :   a < 21       //    Nobody has 21 points
          |   a < 30     //    Leader has fewer than 30 points
            & a < b + 2  //        and lead is small

3

APL(Dyalog Unicode)、35 バイトSBCS

終了が2である中置暗黙関数は、進行中が1で、無効が0であり、より小さいスコアと大きいスコアが残ります。

(,≡30 31|,)×(⊢<2+X1+⊢>X29201+⊣

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

Erik the Outgolferの数式を組み合わせて実装します

バツ:=最大バツ+12029 [バツ<y]+1[バツ+2>y][バツy=バツmod30ymod31]
再配置(従来の数学表記にベクトル化とインライン割り当てがあるかのように)

[バツy=バツymod3031]×[y<2+バツ]×1+[y<バツ:=29最大201+バツ]

APLに直接変換されます(厳密に右結合なので、括弧を避けます):

バツy30 31|バツy×y<2+バツ×1+y>バツ29201+バツ

これは、単純に代入することで暗黙の関数に変換できます。バツy左右の引数ではなく、2つの変数を象徴します:

30 31|×<2+バツ×1+>バツ29201+

いま に等しい 挿入関数の場合 、したがって、

(,30 31|,)×(<2+X)×1+>X29201+

which is our solution; (,≡30 31|,)×(⊢<2+X)×1+⊢>X←29⌊20⌈1+⊣:

 the left argument; x
1+ one plus that; 1+x
20⌈ maximum of 20 and that; max(20,)
29⌊ minimum of 29 and that; min(29,)
X← assign that to X; X:=
⊢> is the right argument greater (0/1)?; [y>]
1+ add one; 1+
( multiply the following by that; ()×
2+X two plus X; 2+X
⊢< is the right argument less than that (0/1); [y<]
( multiply the following by that; ()×
, concatenate the arguments; (x,y)
30 31| remainders when divided by these numbers; mod(30,31)
,≡ are the concatenated arguments identical to that (0/1)?; [(x,y)=]


3

x86 Assembly, 42 Bytes

Takes input in ECX and EDX registers. Note that ECX must be greater than EDX.
Outputs into EAX, where 0 means the game's still on, 1 representing the game being over and -1 (aka FFFFFFFF) representing an invalid score.

31 C0 83 F9 1E 77 1F 83 FA 1D 77 1A 83 F9 15 7C 
18 83 F9 1E 74 12 89 CB 29 D3 83 FB 02 74 09 7C 
08 83 F9 15 74 02 48 C3 40 C3

Or, more readable in Intel Syntax:

check:
    XOR EAX, EAX
    CMP ECX, 30     ; check i_1 against 30
    JA .invalid     ; if >, invalid.
    CMP EDX, 29     ; check i_2 against 29
    JA .invalid     ; if >, invalid.
    CMP ECX, 21     ; check i_1 against 21
    JL .runi        ; if <, running.
    CMP ECX, 30     ; check i_1 against 30
    JE .over        ; if ==, over.
    MOV EBX, ECX
    SUB EBX, EDX    ; EBX = i_1 - i_2
    CMP EBX, 2      ; check EBX against 2
    JE .over        ; if ==, over.
    JL .runi        ; if <, running.
                    ; if >, keep executing!
    CMP ECX, 21     ; check i_1 against 21
    JE .over        ; if ==, over.
                    ; otherwise, it's invalid.
    ; fallthrough!
    .invalid:
        DEC EAX     ; EAX = -1
        RETN
    .over:
        INC EAX     ; EAX = 1
    ; fallthrough!
    .runi:
        RETN        ; EAX = 0 or 1

Fun fact: this function almost follows the C Calling Convention's rules on which registers to preserve, except I had to clobber EBX to save some bytes on stack usage.


Optional (not included in byte-count)

By adding the following 6 bytes directly before start of the code above, you can pass ECX and EDX unordered:

39 D1 7D 02 87 CA

Which is the following in readable Intel Syntax:

CMP ECX, EDX
JGE check
XCHG ECX, EDX

2

Retina 0.8.2, 92 bytes

\d+
$*
^(1{0,19},1{21}|(1{20,28}),11\2|1{29},1{30})$|^(1*,1{0,20}|(1{0,28}),1?\4)$|.+
$#1$#3

Try it online! Link includes test cases. Takes input in ascending order. Explanation: The first stage simply converts from decimal to unary so that the scores can be properly compared. The second stage contains six alternate patterns, grouped into three groups so that three distinct values can be output, which are 10 for win, 01 for ongoing and 00 for illegal. The patterns are:

  • Against 0-19, a score of 21 is a win
  • Against 20-28, a score of +2 is a win
  • Against 29, a score of 30 is a win
  • Against any (lower) score, a score of 0-20 is ongoing
  • Against a score of up to 28, a score of +1 is ongoing
  • Anything else (including negative scores) is illegal


1

Bash 4+, 97 89 91 88 bytes

Assume that inputs are ascending. Used concepts from VDM-SL answer. Try it Online
z==0 - game in progress
z==1 - game completed
z==2 - invalid

-8 by bracket cleanup from (( & | )) conditions
+2 fixing a bug, thanks to Kevin Cruijssen
-3 logic improvements by Kevin Cruijssen

i=$1 j=$2 z=0
((j-i>2&j>21|i<0|i>29|j>30?z=2:0))
((z<1&(j>20&j-i>1|j>29)?z=1:0))
echo $z

1
Your 89 bytes version seems to output 1 instead of 2 for 0 30. Your 97 bytes version was working correctly, so if you're unable to fix it, you can always rollback. Upvoted for that 97 version. :)
Kevin Cruijssen


1
I fixed it, but yours was better! Hard to keep up :P
roblogic

Bug at 29 30 :( it should be "completed"
roblogic

1
Ah oops.. the i>29 should be j>29 in the second ternary to fix it.
Kevin Cruijssen
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.