水で満たされたボウル


19

ボウルの容積とその中の水の容積を入力として受け取り、その中に水が入ったボウルのASCII表現を必要な容積で出力または返すプログラムまたは関数を作成する必要があります。

ボウルの構造は次のとおりです。

 \     /
  \___/

ボウルには少なくとも1つの_キャラクターがあります。数\のと/のも正であり、彼らは対称性に起因する等しいです。

ボウルの量は、合計の数である_spaceの間の文字\のと/のプラスのすべてのペアのための1 \/。これは、上記のボウルの容積が10次のことを意味します。

 \     /  =>  xxxxx x (the last one is for the \/ pair)
  \___/        xxx x (the last one is for the \/ pair)

2つの異なるボウルの容積が同じである可能性があることに注意してください。たとえば、次の両方のボウルの容積は18です。

\       /
 \     /      \         /
  \___/        \_______/

ボウルに水を注ぐことができます。水は~、ボウル内のスペースではなく、キャラクターの列として表されます。一番下の行にはスペースがないため、~「」を含めることはできません。これは、この例に一方向でのみ水を入れることができることを意味します。

 \~~~~~/
  \___/

他のボウルには複数の方法で充填できます。

 \~~~~~/   \     /
  \   /     \~~~/
   \_/       \_/

ボウルの水の量は、キャラクターののボウルの列の量です~。上記の例には4, 6 and 2それぞれの水量があります。

入力

  • ボウルの容積と水の容積の2つの正の整数。
  • 2つの番号の順序を選択できます。
  • 2つの整数は、一般的なリスト形式(リスト、タプル、配列など)で入力することも、2つの個別の整数として入力することもできます。
  • 入力値に対して、少なくとも1つの有効なボウル水構成が保証されています。

出力

  • ボウルと水の量が入力に一致する、水の入ったボウルのASCII表現。
  • 印刷する代わりに結果を返すことを選択した場合、単一の文字列(または言語の最も近い代替)として返される必要があります。
  • 末尾の空白は許可されます。
  • 不要な先頭の空白は許可されません。
  • 正しい構成が複数ある場合は、出力する構成を自由に選択できますが、出力できるのは1つだけです。

各入力整数ペアの後には、1つ以上の可能な出力が続きます。

6 2
\~~~/
 \_/

10 4
\~~~~~/
 \___/

24 8
\        /
 \~~~~~~/
  \    /
   \__/

42 12 //either of the two output is correct
\           /
 \         /
  \~~~~~~~/
   \     /
    \   /
     \_/

\               /
 \~~~~~~~~~~~~~/
  \___________/

90 68
\~~~~~~~~~~~~~~~~~~~~~/
 \                   /
  \                 /
   \               /
    \_____________/

102 42
\                     /
 \                   /
  \~~~~~~~~~~~~~~~~~/
   \               /
    \             /
     \___________/

これはコードゴルフなので、最短のエントリーが勝ちます。

回答:


6

CJam、72 70 69バイト

q~:QW=3m*{:,2ff*),)ff+}%{::)1fbQ=}=~W%ee_,S*W'_t@,~'~t.{S\+.*"\/".+N}

CJamインタープリターでオンラインで試してください。

実行時間とメモリ使用量はO(scary)なので、最後の3つのテストケースはJavaインタープリター(および追加のヒープスペース)を使用して検証する必要があります。

実行例

$ time java -Xmx4G -jar cjam-0.6.5.jar bowl.cjam <<< '[42 102]'
\                     /
 \                   /
  \~~~~~~~~~~~~~~~~~/
   \               /
    \             /
     \___________/

real    0m40.669s
user    3m13.100s
sys     0m11.690s

使い方

q~:Q     e# Read from STIDN, evaluate and save the result in Q.
W=       e# Select the last element of Q (bowl volume B).
3m*      e# Push all vectors of {0,...,B-1} × {0,...,B-1} x {0,...,B-1}.

{        e# For each vector [X Y Z]:
  :,     e#   Push [[0 1 ... X-1] [0 1 ... Y-1] [0 1 ... Z-1]].
  2ff*   e#   Multiply each coordinate by 2.
  ),)    e#   Pop the last vector, compute its length and increment.
  ff+    e#   Add the result to each component of each vector.
}%       e# Result: [[Z Z+2 ... Z+2(X-1)] [Z Z+2 ... Z+2(Y-1)]]

{        e# Find:
  ::)    e#   Increment each coordinate (to account for the volume in "\/").
  1fb    e#   Sum the coordinate of both vectors.
  Q=     e#   Compare the result to Q (desired volumes).
}=       e# If they match, push the array and break.

~        e# Dump both vectors on the stack.
W%       e# Reverse the rightmost one (corresponds to the bowl volume).
ee       e# Enumerate its coordinates.
         e# [Z+2(Y-1) ... Z+2 Z] -> [[0 Z+2(Y-1)] ... [Y-2 Z+2] [Y-1 Z]].
_,S*     e# Compute the length (Y) and push a string of Y spaces.
W'_t     e# Replace the last space with an underscore.
@        e# Rotate the leftmost vector (corresponds to the water volume) on top.
,        e# Compute its length (X).
~'~t     e# Replace the space at index X from the right with a tilde.

.{       e# For each enumerates coordinate and the corresponding character:
  S\+    e#   Append the character to the string " ".
  .*     e#   Vectorized repetition: [1 2] " ~" -> [" " "~~"]
  "\/".+ e#   Append the first (second) solidus to the first (second) string.
  N      e#   Push a linefeed.
}

2

C、231の 229バイト

早めの提出:)ここでもっと多くのゴルフができます。

v,V,w,h,H,i,j;main(c,a)char**a;{V=atoi(a[1]);v=atoi(a[2]);for(;++H;)for(h=0;h++<H;){for(w=1;h*h+w*h-h<v;++w);if(H*H+w*H-H==V){for(;H--;){printf("%*s",++i,"\\");for(j=0;j++<w-1+2*H;)putchar(H?H==h?'~':32:95);puts("/");}exit(0);}}}

ゴルフをしていない:

int v,V,w,h,H,i,j;
int main(int c, char **a)
{
    V=atoi(a[1]); /* Volume of bowl */
    v=atoi(a[2]); /* Volume of water */

    for(;++H;) /* Make the bowl taller */
    {
        for(h=0;h++<H;) /* Make the water taller */
        {
            for(w=1;h*h+w*h-h<v;++w); /* Make the bowl wider until the water volume matches */
            if(H*H+w*H-H==V) /* if the bowl volume matches, then we're good */
            {
                for(;H--;) /* Print out the bowl, one line at a time */
                {
                    printf("%*s",++i,"\\"); /* Print the left edge */
                    /* Print the inside (either with air/water, the top of the water, or the bottom of the bowl */
                    for(j=0;j++<w-1+2*H;)
                        putchar(H?H==h?'~':32:95);
                    /* Print the right edge of the bowl */
                    puts("/");
                }
                exit(0); /* die, we're done */
            }
        }
    }
}

ボウルの量に合っているが、水の量に合わないボウルに出くわすことはありますか?
バルタン

At least one valid bowl-water configuration is guaranteed for the input values.-OP
コールキャメロン

2

Javascript ES5、364バイト

これが私の昼食時にすぐに思い付くことができ、私のシフトが終了する間にゴルフをするのを助けてくれます!

ソース

function V(x,v) { // calculate volume of bowl/water
    for(i=v,j=x;i--;j+=2) {
      v+=j; 
    }
    return v
}
function B(x,y,l) { // draw bowl/water
    for(s="",h=y,w = x+2*y;y--;s+="\n")
        for(i=w;i--;) {
            f= i>h-y-1 && w-i > h-y;
            s+=i==h-y-1?"/": 
                w-i == h-y? "\\":
                y==l-1 && f? "~" :
                !y && f?"_":" "
        }
    return s;
}
n=prompt().split(" ");
b=+n[0]; // bowl volume
w=+n[1]; // water volume
for(x=b;x;x--)  // loop through possible widths
  for(y=b;y;y--)  // loop through possible heights
    if(V(x,y)==b) // check if we found bowl volume
       for(y2=y;y2;y2--) { // check possible water heights
         v = V(x,y2-1);
         if(v==w){ // see if volume matches
          alert(B(x,y,y2));
          x=1;break;
         }
       }

ゴルフ:

(圧縮するためにミニファイナーを実行し、ランチシフトは終了しました)

function V(f,r){for(i=r,j=f;i--;j+=2)r+=j;return r}function B(r,y,n){for(s="",h=y,w=r+2*y;y--;s+="\n")for(i=w;i--;)f=i>h-y-1&&w-i>h-y,s+=i==h-y-1?"/":w-i==h-y?"\\":y==n-1&&f?"~":!y&&f?"_":" ";return s}for(n=prompt().split(" "),b=+n[0],w=+n[1],x=b;x;x--)for(y=b;y;y--)if(V(x,y)==b)for(y2=y;y2;y2--)if(v=V(x,y2-1),v==w){alert(B(x,y,y2)),x=1;break}

2

Perl、227 172バイト

-nオプションを使用して実行します。

/ /;for$h(1..$`){for$w(1..$`){for$l(1..($h*($w+$h)==$`)*$h){if($l*($w+$l)==$'){for(0..$h-1){print$"x$_."\\".($_<$h-1?$_==$h-$l-1?"~":$":"_")x($w+($h-$_-1)*2)."/
"}exit}}}}

ゴルフを助けてくれたデニスに感謝します。

ボウルの容積を高さ*(幅+高さ)として計算します。ここで、幅は_文字数、高さは\文字数です。

高さと幅のそれぞれの組み合わせは、正しいボウルの容積が見つかるまでネストされたループのペアでテストされ、次に、その幅で正しい水の容積が可能かどうかを確認するために、可能な水の高さレベルの別のループが実行されます。

3番目のループを削除するには、aを1、bを幅、cを目的の水量の負の値とする2次式を使用して水位を計算し、整数かどうかを確認しますが、それはより多くのバイトを必要とします単にループを行うよりも。とにかく(183バイト):

/ /;for$h(1..$`){for$w(1..$`){if($h*($w+$h)==$`){$l=(sqrt($w*$w+4*$')-$w)/2;if(int$l==$l){for(0..$h-1){print$"x$_."\\".($_<$h-1?$_==$h-$l-1?"~":$":"_")x($w+($h-$_-1)*2)."/
"}exit}}}}

2

Python 2、162バイト

V,W=input()
r=1
while r*r<V:a=V/r-r;k=1;exec"if(a+k)*k==W*(V%r<1):i=1;exec\"print' '*~-i+'\%s/'%(' _~'[(i==r)-(i==r-k)]*(a+2*(r-i)));i+=1;\"*r;r=V\nk+=1\n"*r;r+=1

少し面倒ですが、これが私の最初の試みです。すべての可能な行数を試してr、ベースアンダースコアの数をに設定しますa = V/r-r。次に、考えられるすべての水位の高さkを試し、ボウルが有効かどうかを確認し、有効であればそれを印刷します。


1

Python 2.7、 284 270 260バイト

def f(b,w,i=1,e='while s<%s:j+=2;s+=j'):
 while 1:
    i+=1;j=s=i;exec e%w
    if s==w:p=j;exec e%b
    if s==b:break
 h=(j-i)/2+1;t=w=i+(h-1)*2+1
 for j in range(h):r,s,t=((' '*(t-2),'_'*(i-1))[j==h-1],'~'*(t-2))[j==h-(p-i)/2-2],(w-t)/2,t-2;print" "*s+"\\"+r+"/"+" "*s

これは基本的に、バケツと水の高さと幅を計算して印刷します。

開始時にwhileいwhileループ部分を削除しようとしました(ここで、バケットの高さと、水を引くべき高さを計算します。現在、最後の行を除くコードのすべての行は、幅と高さ)。まだ試しています:P

さまざまなケースでテストしています-

>>> execfile("buckets.py")
(6, 2)
\~~~/
 \_/

(10, 4)
\~~~~~/
 \___/

(24, 8)
\        /
 \~~~~~~/
  \    /
   \__/

(42, 12)
\           /
 \         /
  \~~~~~~~/
   \     /
    \   /
     \_/

(90, 68)
\~~~~~~~~~~~~~~~~~~~~~/
 \                   /
  \                 /
   \               /
    \_____________/

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