コアラ、カニ、およびCommapillars


21

2つの正の整数XとYが与えられた場合、可能であれば正確にXコンマ(,)とYピリオド(.)が出力に含まれるように、次の3つのASCIIアート動物の任意の組み合わせを出力します。

  1. コアラ: 1コンマ、2ピリオド

    <.,.>
    
  2. カニ:コンマ2個、ピリオド2個

    ,<..>,
    
  3. Commapillar: 3つ以上のコンマ、1つのピリオド

    <,,,.>
    

    または<,,,,.>または<,,,,,.>または<,,,,,,.>または<,,,,,,,.>など。

これらの動物の組み合わせが正確にXコンマとYピリオドを生成できない場合、失敗をカモフラージュする単一のコンマレオンを出力します。

~<.,,>~~

産出される動物は、どんな量でも、どんな順序でもかまいません。文字列、スペース、改行で区切られているか、各動物が1つの要素であるリストに含まれている場合があります。

たとえば、X = 7、Y = 5の場合、これらはすべて有効な出力になります(空行で区切られます)。

<.,.> <.,.> <,,,,,.>

<.,.>
<,,,,,.>
<.,.>

,<..>, <.,.> <,,,,.>

<,,,,.>
,<..>,
<.,.>

,<..>, <,,,.> ,<..>,

[",<..>,", ",<..>,", "<,,,.>"] (list syntax depends on language)

(少なくともこの例では)動作可能な動物のセットが複数あることに注意してください。しかし、あなたは出力のみに任意の必要覚えて1つのが存在する場合、有効な解決策を。動物の数または別個の動物の数は重要ではありません。

X = 3、Y = 3またはX = 1などの入力の場合、解がない場合、出力は常に

~<.,,>~~

おそらく単一要素のリストに含まれます。

バイト単位の最短コードが優先されます。


6
コアラはそのように見えます:-)
ルイスメンドー

各ペア(X、Y)の組み合わせの数を出力する小さなプログラムを作成しました。残念ながら、(128、128)までしか実行できませんでした。__int128あり、bignumライブラリを使用するにはあまりにも面倒だったからです。ここでCSVダンプである:pastebin.com/ght5xkRu最初の行と列は、XとYの値である
Fytch

回答:


4

ルビー、139バイト

Lambda関数、引数としてxとyを取り、文字列を返します

->x,y{c=y-2*n=y-(x>y ?1:0)>>1
x+=-c/2*s=[x-n,c*3].max
x<n||x>n*2?'~<.,,>~~':',<..>, '*(x-n)+'<.,.> '*(2*n-x)+"<%s.> "*c%[?,*s-=c/2*3,?,*3]}

解決策が存在する場合は、すべてのコアラ+コマピラーまたはすべてのコアラ+カニで実行できます。

原則は、最小限のコンマピラーを使用することです。数が奇数の場合、1つのコンマピラーを使用します。ピリオドよりもカンマが多い場合を除き、0個のコンマピラーを使用する場合でも、その場合は2を使用します。

非共マッピング者(カニ+コアラ)で使用される期間の数は必然的に偶数であり、非共マッピング者の数は半分(number of periods)-(number of commapillars)です。すべてのコアラに対して十分なコンマがない場合、またはすべてのカニに対してコンマが多すぎる場合、解決策はありません。それ以外の場合は、ソリューションを返します。

テストプログラムでコメント

明確にするためにカメレオンの代わりに「失敗」を使用します

f=->x,y{c=y-2*n=y-(x>y ?1:0)>>1
#n=noncommapillars=y>>1 as they have 2 periods. c=commapillars=y-2*n, 1 for odd y, 0 for even y.
#if x>y there are too many commas to have 0 commapillars for even y. noncommapillars= y-1 >> 1, so 2 commapillars

x+=-c/2*s=[x-n,c*3].max
# s=number of commas allocated to commapillars. x-n to allow all noncommapillars to be koalas, but at least 3 per commapillar.
#-c/2 == -1 if there are commapillars, 0 if not (Ruby truncates toward -inf). Subtract commas for commapillars from x if necessary

x<n||x>n*2?'fail':',<..>, '*(x-n)+'<.,.> '*(2*n-x)+"<%s.> "*c%[?,*s-=c/2*3,?,*3]}
#if x<n (insufficient commas for all koalas) or x>n*2 (too many for all crabs) return fail. Else...
#return string off crabs, koalas, and (using % operator like sprintf) c commapillars (0..2), the second with 3 commas (if present) and the first with the rest.  



10.times{|j|10.times{|i|puts "%-20s %s"%[?.*j+?,*i,f[i,j]]}}
#all x,y from 0..9

出力

,                    fail
,,                   fail
,,,                  fail
,,,,                 fail
,,,,,                fail
,,,,,,               fail
,,,,,,,              fail
,,,,,,,,             fail
,,,,,,,,,            fail
.                    fail
.,                   fail
.,,                  fail
.,,,                 <,,,.>
.,,,,                <,,,,.>
.,,,,,               <,,,,,.>
.,,,,,,              <,,,,,,.>
.,,,,,,,             <,,,,,,,.>
.,,,,,,,,            <,,,,,,,,.>
.,,,,,,,,,           <,,,,,,,,,.>
..                   fail
..,                  <.,.>
..,,                 ,<..>,
..,,,                fail
..,,,,               fail
..,,,,,              fail
..,,,,,,             <,,,.> <,,,.>
..,,,,,,,            <,,,,.> <,,,.>
..,,,,,,,,           <,,,,,.> <,,,.>
..,,,,,,,,,          <,,,,,,.> <,,,.>
...                  fail
...,                 fail
...,,                fail
...,,,               fail
...,,,,              <.,.> <,,,.>
...,,,,,             <.,.> <,,,,.>
...,,,,,,            <.,.> <,,,,,.>
...,,,,,,,           <.,.> <,,,,,,.>
...,,,,,,,,          <.,.> <,,,,,,,.>
...,,,,,,,,,         <.,.> <,,,,,,,,.>
....                 fail
....,                fail
....,,               <.,.> <.,.>
....,,,              ,<..>, <.,.>
....,,,,             ,<..>, ,<..>,
....,,,,,            fail
....,,,,,,           fail
....,,,,,,,          <.,.> <,,,.> <,,,.>
....,,,,,,,,         <.,.> <,,,,.> <,,,.>
....,,,,,,,,,        <.,.> <,,,,,.> <,,,.>
.....                fail
.....,               fail
.....,,              fail
.....,,,             fail
.....,,,,            fail
.....,,,,,           <.,.> <.,.> <,,,.>
.....,,,,,,          <.,.> <.,.> <,,,,.>
.....,,,,,,,         <.,.> <.,.> <,,,,,.>
.....,,,,,,,,        <.,.> <.,.> <,,,,,,.>
.....,,,,,,,,,       <.,.> <.,.> <,,,,,,,.>
......               fail
......,              fail
......,,             fail
......,,,            <.,.> <.,.> <.,.>
......,,,,           ,<..>, <.,.> <.,.>
......,,,,,          ,<..>, ,<..>, <.,.>
......,,,,,,         ,<..>, ,<..>, ,<..>,
......,,,,,,,        fail
......,,,,,,,,       <.,.> <.,.> <,,,.> <,,,.>
......,,,,,,,,,      <.,.> <.,.> <,,,,.> <,,,.>
.......              fail
.......,             fail
.......,,            fail
.......,,,           fail
.......,,,,          fail
.......,,,,,         fail
.......,,,,,,        <.,.> <.,.> <.,.> <,,,.>
.......,,,,,,,       <.,.> <.,.> <.,.> <,,,,.>
.......,,,,,,,,      <.,.> <.,.> <.,.> <,,,,,.>
.......,,,,,,,,,     <.,.> <.,.> <.,.> <,,,,,,.>
........             fail
........,            fail
........,,           fail
........,,,          fail
........,,,,         <.,.> <.,.> <.,.> <.,.>
........,,,,,        ,<..>, <.,.> <.,.> <.,.>
........,,,,,,       ,<..>, ,<..>, <.,.> <.,.>
........,,,,,,,      ,<..>, ,<..>, ,<..>, <.,.>
........,,,,,,,,     ,<..>, ,<..>, ,<..>, ,<..>,
........,,,,,,,,,    <.,.> <.,.> <.,.> <,,,.> <,,,.>
.........            fail
.........,           fail
.........,,          fail
.........,,,         fail
.........,,,,        fail
.........,,,,,       fail
.........,,,,,,      fail
.........,,,,,,,     <.,.> <.,.> <.,.> <.,.> <,,,.>
.........,,,,,,,,    <.,.> <.,.> <.,.> <.,.> <,,,,.>
.........,,,,,,,,,   <.,.> <.,.> <.,.> <.,.> <,,,,,.>

4

Befunge、249 218バイト

&::00p&::00g\`-2/:20p2*-3*:30p\20g-`:!00g20g-*\30g*+:30g-v>:#,_@
"~<.,,>~~"0_v#\g03+`g050`\0:-g02\p05:-\*2g02:-*/6+3g03p04<^
$"<">:#,_40p>:!#|_3-0" >.,,"40g3>3g#<\#-:#1_
0" ,>..<,">:#,_$>:!#|_1-
#@_1-0" >.,.<">:#,_$>:!

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

これは、Level River StによるRubyの回答のアルゴリズムに基づいています。これにより、ゴルフの範囲が広がり、元のソリューションに比べてサイズが大幅に削減されました。


2

C#6、321 303バイト

using System.Linq;string F(int x,int y)=>S(x,y)??"~<.,,>~~";string S(int x,int y)=>x<0|y<0?null:y<1?x<1?"":null:y*3>x?S(x-1,y-2)!=null?S(x-1,y-2)+"<.,.> ":S(x-2,y-2)!=null?S(x-2,y-2)+",<..>, ":null:string.Concat(new int[y].Select((_,k)=>k<1?C(x-y*3+3):C(3)));string C(int x)=>$"<{new string(',',x)}.> ";

を呼び出しF()ます。他の2つの関数はヘルパーです。repl.itデモ

// Coalesce failed combinations with commaleon
string F(int x,int y)=>S(x,y)??"~<.,,>~~";
// Get successful combination or null
string S(int x,int y)=>
    x<0|y<0
        // Fail: Out of range
        ?null
        :y<1
            ?x<1
                // Successful: All commas and periods accounted for
                ?""
                // Fail: Not enough periods for commas
                :null
            :y*3>x
                // Not all commapillars
                ?S(x-1,y-2)!=null
                    // Try koala
                    ?S(x-1,y-2)+"<.,.> "
                    // Try crab
                    :S(x-2,y-2)!=null
                        ?S(x-2,y-2)+",<..>, "
                        // Epic fail
                        :null
                // All commapillars
                :string.Concat(new int[y].Select((_,k)=>k<1
                    // This commapillar takes most of commas
                    ?C(x-y*3+3)
                    // The rest each takes 3
                    :C(3)));
// Generate single commapillar
string C(int x)=>$"<{new string(',',x)}.> ";
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.