还剩多少水?


29

在加利福尼亚州,我们正处于干旱之中,因此我们需要知道我们剩下多少水,以便我们可以尽可能多地节约水。

由于水的供应有限,因此您的代码必须尽可能短。

例子

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

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

Output: 1
|  |
|__|

Output: 0 (or bonus)

规格

输入将仅包含以下所有:|_ ~和换行符。上方的所有内容~均视为空气;_,下方的空间~及其~本身被视为水。剩下的水百分比由电脑计算water / (air + water)。您的输出必须精确到小数点后四位(除非您追求百分比奖金)。输入将始终为矩形。该~会只在同一行,如果任何。如果需要,输入中也可以选择有换行符。

奖金

如果您同时获得这两种奖金,则在-35奖金之前应用-15%的奖金

-35字节奖金:如果您的代码显示“这只干旱的山羊失控”,则在输出为0时而不是0

-15%红利:如果输出百分比。

为此,您需要将小数点位置左移两位,修剪前导零,并%在末尾添加a 。允许零结尾(最大2),只要它们不影响该值即可。0.5->以下任何一项:50.00% 50% 50.0%


28
因为水的供应有限,所以您的代码必须尽可能短。 ”嗯,好吧,有道理...我猜吗?
致命

14
@Fatalize:这不是州外的常识,但是基于流体逻辑的计算机在加利福尼亚州非常受欢迎。我听说他们现在正在努力寻找是否可以解决基于杏仁的替代品。
JohnE 2015年

10
@JohnE,这可能可以解释为什么现在那里发生了干旱
hargasinski 2015年

1
你们知道射流式计算机不会消耗水,对吗?就像电子计算机不会造成电子短缺一样。
user253751

这是|~~~|(无下划线)有效输入吗?
edc65

Answers:


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。

奖金似乎不值得。我对字符串一的最佳选择是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"?

在线尝试

此版本获得两个奖励。输入必须包含尾随换行符,此解决方案才能起作用。

感谢@MartinBüttner节省了2个字节。

说明:

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
一如既往的漂亮测试代码和出色的代码!一个建议:你可以取代p=~ip=i&&-p&&p-q1+~p1+p保存1个字节。
user81655 2015年

@ user81655好的建议,谢谢
edc65

4

Par,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

l是一个自定义的长度函数,这是必需的,因为内建函数length返回整数值,但是我们需要除法的浮点值(也有genericLength提供此功能的函数,但是它更长,更不用说必需的了import Data.List)。f将输入i分为(-> s)行,然后成对出现,其中第一个元素是一个列表,所有行直到(且不包括)第一个都有一个~的行。第二个元素是包含其余各行的列表。结果是第二个元素的长度除以的长度s

奖金没有回报。


1

Python很冗长!

Python:98.45字节

(157 * 0.85)-35 = 98.45字节

此版本从stdin读取,并同时获得以下两项奖励:

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用作Python?
尼古拉斯·克拉克

可选的尾随换行符由您决定。作为分割,没错,但你已经处理print的功能等,所以我假设你使用Python 3
搅拌机

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

(两个代码块中的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}

我创建了QBIC以使QBasic更具竞争力,但它仍需要进行一些改进。到目前为止,没有任何错误捕获的捷径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分。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.