どれくらいの水が残っていますか?


29

ここカリフォルニアでは干ばつになっているので、できるだけ多くの水を節約できるように、どれだけの水を残したかを知る必要があります。

水の供給は限られているため、コードはできるだけ短くする必要があります。

|   |
|   |
|~~~|
|___|

Output: 0.5
|~~~~~|
|     |
|     |
|_____|

Output: 1
|  |
|__|

Output: 0 (or bonus)

仕様

入力は、すべて|_ ~と改行のみで構成されます。以上のものはすべて~空気と見なされます。_、下のスペース~、および~それ自体は水と見なされます。残された水の割合はによってコンピューターwater / (air + water)です。出力は、小数点以下4桁まで正確である必要があります(パーセントボーナスを求めない限り)。入力は常に長方形になります。ある~場合は、1行のみになります。必要に応じて、入力にオプションで末尾の改行を含めることもできます。

ボーナス

両方のボーナスに行くと、-15%ボーナスが-35ボーナスの前に適用されます

-35バイトのボーナス:出力が0の場合、コードが0ではなく「この干ばつヤギ」を印刷すると

-15%ボーナス:パーセントを出力した場合。

これを行うには、小数点以下を2桁左にシフトし、先行ゼロを削除し%て、末尾にa を追加します。末尾のゼロ(最大2)は、値に影響を与えない限り許可されます。0.5->次のいずれか:50.00% 50% 50.0%


28
水は限られているため、コードはできるだけ短くする必要があります。」ええと、わかりました。
15

14
@Fatalize:州外では一般的な知識ではありませんが、カリフォルニアでは流動論理ベースのコンピューターが非常に人気があります。最近、アーモンドをベースにした代替品を作れるかどうかを確認しようとしているそうです。
JohnE

10
おそらくそこに干ばつが今そこにある理由を説明することを@JohnE
hargasinski

1
流体コンピュータは水を使い果たしないことを知っていますか?電気コンピューターが電子不足を引き起こさないように。
user253751

これは:(|~~~|アンダースコアなし)有効な入力ですか?
edc65

回答:


16

Pyth- 17 46 45 52 * .85-35 = 9.2バイト

入力を(新しい#フィルターmeta-op!を使用して)行にフィルターし~、それを入力にインデックス付けし、それを入力の長さで除算します。にwithがない場合~、エラーが発生し、except句がトリガーされ.xて文字列が出力されます。

.x+*100-1cxK.zh@#\~KlK\%." u(C$éáPãbÉãç*îÂe[W

こちらからオンラインでお試しください


10

Python 3、37バイト

lambda x:1-(x+'|~').find('|~')/len(x)

ボーナスなし。末尾の改行を含む、改行を含む入力文字列を受け取ります。

数式が機能する理由を見てみましょう。水の割合は、導出する空気の割合の補数です。

frac_water = 1 - frac_air

0, 1, 2, ...に番号を付けると、

frac_air = water_row_index / num_rows 

両方に各行の幅を掛けて改行をカウントする場合も同様です。これにより、文字数の式が簡素化されます。

frac_air = (width * water_row_index) / (width * num_rows)
         = water_row_start_char_index / num_chars

水列の開始点は、入力文字列でxを検索することで検出され|~、文字数はちょうど長さです。

frac_air = x.find('|~') / len(x)

最後に、水なしの入力を機能させるために、|~検索前に架空の水行の開始を最後に追加します。これにより、水位が0のように見えます。

ボーナスは価値がないと思われました。文字列1で得た最高のものは73-35 = 38です。

lambda x:['This drought goat out of hand',1-x.find('|~')/len(x)]['~'in x]

7

CJam、19 17 16 58 * 0.85-35 = 14.3バイト

q'|-_'~#_)\@,d/1\m100*s'%+"This drought goat out of hand"?

オンラインで試す

このバージョンには両方のボーナスがあります。このソリューションが機能するには、入力に末尾の改行が必要です。

2バイトを節約してくれた@MartinBüttnerに感謝します。

説明:

q       Get input.
'|-     Remove left/right wall, so that position of first ~ in remaining string
        corresponds to the water level.
_       Make a copy.
'~#     Find ~ character.
_)      Make copy of find result, and increment it. This is 0 if the ~
        was not found, and will be used for the bonus condition.
\       Swap original find result to top.
@,      Rotate copy of remaining input to top, and get its length.
d       Convert to double to get float division.
/       Divide the two values. Since the position of the ~ was indexed from
        the top, this is 1 minus the desired result.
1\m     Subtract value from 1, to get the actual result.
100*    Multiply by 100 to get percent.
s       Convert to string.
'%+     Append % sign.
"This drought goat out of hand"
        Push bonus zero string.
?       Ternary operator to pick calculated result or zero string.

5

JavaScript(ES6)、45(94 -15%-35)

匿名関数として。テンプレート文字列を使用すると、重要な改行がバイトカウントに含まれます。

1バイト保存されたthx @ user81655を編集

p=>p.split`
`.map((r,i)=>r>'|~'?p=i:q=~i)&&q-p?(1+p/q)*100+'%':'This drought goat out of hand'

少ないゴルフ

p=>(
  p.split('\n') // split in rows
  .map((r,i)=> // execute for each row
    r>'|~'   // look for the water top
      ? p=i  // position of water top in p
      : q=~i // if not water top, set current position (-i-1) in q
  ),
  // at the end,if water top not found, p still contains the input string
  q-p // subtracting the input string I get NaN (that is a falsy value)
  ? (1+p/q)*100+'%' // calc % taking into account the negative sign of q
  : 'This drought goat out of hand' 
)

テストスニペット

F=p=>p.split`\n`.map((r,i)=>r>'|~'?p=i:q=~i)&&q-p?(1+p/q)*100+'%':'This drought goat out of hand'

function Update() {
  var w=+W.value, h=+H.value, t=+T.value,
      b=Array(h).fill().map((r,i)=>'|'+(i==h-1?'_':i==t?'~':' ').repeat(w)+'|').join`\n`  
  O.textContent = b+'\n\n'+F(b)
  
}

Update()
<table>
  <tr><td>Width</td><td><input id=W type=number value=4 oninput='Update()'></td></tr>
  <tr><td>Height</td><td><input id=H type=number value=4 oninput='Update()'></td></tr>
  <tr><td>~~~ at row</td><td><input id=T type=number value=2 oninput='Update()'></td></tr>
</table>  
<pre id=O></pre>


1
いつものように美しいテストスニペットと素晴​​らしいコード!1つの提案:あなたは置き換えることができp=~ip=i&&-p&&p-q1+~p1+p1つのバイトを保存します。
user81655

@ user81655素敵な提案、ありがとう
edc65

4

パー、57 * 85%-35 = 13.45バイト

`This drought goat out of hand`r√″T┐↑⌐'~˦↑↔~÷Zx²*'%↔╡\z_g

説明

`This dr...d`  ## 'This drought goat out of hand'
r              ## Read entire input
√              ## Split by newlines
″              ## Duplicate
T              ## Transpose
┐↑             ## Second element of each line
⌐              ## Reverse
'~˦            ## First index of '~'
↑              ## Plus one
↔              ## Swap
~÷             ## Divide by size
Z              ## Assign to z
x²*            ## Multiply by 100
'%↔╡           ## Append '%'
\              ## Array of string and number
z_g            ## If z=0, then string; else, number

3

Perl、70-15 %-35 = 24.5バイト

+1を含む -p

 $S[$w|=/~/]++}{$_=$w?100*$S[1]/$..'%':'This drought goat out of hand'

コメント付き:

$S[ $w |= /~/ ]++                   # $w=0 for air, 1 for having seen water; count element
}{                                  # -n/-p: end the `while(<>){` and begin END block
$_ = $w                             # assign output for -p
  ? 100 * $S[1] / $. . '%'          # $. is $INPUT_LINE_NUMBER
  :'This drought goat out of hand'  # costs 35 aswell, but is effectively more after -15%

  • 26 + 1バイトバージョン、ボーナスなし:27

    $S[$w|=/~/]++}{$_=$S[1]/$.
    
  • 34 + 1バイトバージョン、15%ボーナス:29.75

    $S[$w|=/~/]++}{$_=100*$S[1]/$..'%'
    
  • 61 + 1バイトバージョン、-35ボーナス付き:27

    $S[$w|=/~/]++}{$_=$w?$S[1]/$.:'This drought goat out of hand'
    
  • 69 + 1バイトバージョン、両方のボーナス:24.50

    $S[$w|=/~/]++}{$_=$w?100*$S[1]/$..'%':'This drought goat out of hand'
    

2

Javascript、59.3

小数点以下の桁数を増やしてもいいと思います。末尾の改行を想定していません。

drought=
// code
a=>(b=-1,e=a.split`
`.map((c,d)=>b=c[1]=='~'?d:b).length,++b?(e-b+1)*100/e+"%":"This drought goat out of hand")

// I/O
var i = document.getElementById("i");
var o = document.getElementById("o");
i.onchange = i.onkeyup = function(){
  o.textContent = drought(i.value);
};

// explanation
inputStr=>(
  tildePosition = -1, // default: not found
  containerDepth =    // if the current line has a tilde, set tildePosition, otherwise
                      // keep current tildePosition
      inputStr.split`\n`.map((line, pos)=> tildePosition = line[1]=='~' ? pos : tildePosition)
    .length,          // assign number of lines (container depth) to containerDepth
  ++tildePosition     // if it's still -1, print the message, otherwise print percent
    ?(containerDepth-tildePosition+1)*100/containerDepth+"%"
    :"This drought goat out of hand")
<textarea id="i"></textarea>
<p id="o"></p>


1

Haskell、56バイト

l=sum.(>>[1])
f i|s<-lines i=l(snd$break(elem '~')s)/l s

使用例:f "| |\n|~~|\n| |\n|__|"->0.75

llength組み込みは整数値を返すため、カスタムの長さ関数が必要ですが、除算には浮動小数点値が必要です(genericLengthこの機能を提供するものもありますが、必要なだけでなく、それより長くなりますimport Data.List)。f入力iを行(-> s)に分割し、次にペアに分割します。最初の要素は、最初のaまでのすべての行を含むリスト~です。2番目の要素は、残りの行を含むリストです。結果は、2番目の要素の長さをの長さで割ったものですs

ボーナスは報われません。


1

Pythonは冗長です!

Python:98.45バイト

(157 * 0.85)-35 = 98.45バイト

このバージョンは標準入力から読み取り、両方のボーナスを収集します。

import sys
r=[x[1]for x in sys.stdin.read().split('\n|')]
o="This drought goat out of hand"if'~'not in r else"%g%%"%(100-100.0*r.index('~')/len(r))
print(o)

2
ボーナスなしの66:import sys;r=[x[1]for x in sys.stdin];print(1-r.index('~')/len(r))
Blender

いいね!しかし、オプションの末尾の改行によってつまずかれますよね?また、除算演算子のためにPython 3のみですか?
ニコラスクラーク

オプションの末尾の改行は含める必要があります。分割については、そうです、しかし、あなたはすでにprint関数のように扱っているので、私はあなたがPython 3を使用していると仮定しました。
Blender

1

Awk、72文字-15%-35 = 26.2

/~/{w=NR}END{print w?(NR-w+1)/NR*100"%":"This drought goat out of hand"}

サンプル実行:

1;これらのサンプル実行でのみ使用され、「人間が読める」タンクを表示します。)

bash-4.3$ awk '1;/~/{w=NR}END{print w?(NR-w+1)/NR*100"%":"This drought goat out of hand"}' <<< $'| |\n| |\n| |\n|_|'
| |
| |
| |
|_|
This drought goat out of hand

bash-4.3$ awk '1;/~/{w=NR}END{print w?(NR-w+1)/NR*100"%":"This drought goat out of hand"}' <<< $'| |\n| |\n|~|\n|_|'
| |
| |
|~|
|_|
50%

1

PHP、92文字-15%-35 = 43.2

(2ブロックのコードに88文字+ 4文字のコマンドラインオプション。)

$argn[1]>z&&$w=+$argi;
echo$w?100*($argi-$w+1)/$argi."%":"This drought goat out of hand";

想定 error_reportingはデフォルトに設定されています。

(大しただけ使用することを望んだ-R-E一回。今だけ-B残しました。)

サンプル実行:

echo"$argn\n";これらのサンプル実行でのみ使用され、「人間が読める」タンクを表示します。)

bash-4.3$ php -R 'echo"$argn\n";$argn[1]>z&&$w=+$argi;' -E 'echo$w?100*($argi-$w+1)/$argi."%":"This drought goat out of hand";' <<< $'| |\n| |\n| |\n|_|'
| |
| |
| |
|_|
This drought goat out of hand

bash-4.3$ php -R 'echo"$argn\n";$argn[1]>z&&$w=+$argi;' -E 'echo$w?100*($argi-$w+1)/$argi."%":"This drought goat out of hand";' <<< $'| |\n| |\n|~|\n|_|'
| |
| |
|~|
|_|
50%

0

QBIC - 116-15%= 98.6バイト

{input S$:S$=MID$(S$,2,1):I=I+1:IF y<1 then x=I
y=y+instr(S$,"~"):IF instr(S$,"_")>0 THEN ?(1-(x-y)/I)*100;"%":END}

QBasicの競争力を高めるためにQBICを作成しましたが、まだいくつかの改善が必要です。現在のところ、エラートラップのショートカットはありませんTHEN(これはかなり大きな見落としinput$です)。まもなく追加されます。

私は、0のボーナスを打つことができませんでした。費用がかかりすぎました...パーセンテージを印刷することができました。

サンプル入出力:

? |  |
? |  |
? |~~|
? |__|
 50 %

プログラムは対話形式で入力を読み取ります。湖の底を検出すると(_)、パーセンテージを出力して終了します。満杯のコンテナと空のコンテナもテスト済みです。

編集:昨年中にQBICがどのように拡張されたかを示すために、現在の通訳のために書かれた同じプログラムを以下に示します。

{_?A=$MID$|(A,2,1)#~|#_| i=i+1~j<1|k=i]j=j+instr(A,B)~instr(A,C)|?(1-(k-j)/i)*100,@%|_X

87バイト、印刷パーセンテージ、74のスコアです。

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