我生日的主题:如火如荼


18

我需要点蜡烛。实际上,我需要点燃任意数量的蜡烛。但是只有好的。

目的给定一个文本块(包含已点燃,有效和无效的蜡烛)和一个数字N作为输入,编写一个程序或函数,使其点亮的数量等于N,减去已点燃的蜡烛的数量。如果N大于有效蜡烛数,则程序应打印缺少的有效蜡烛数。如果没有蜡烛,输出应为:(

这是一些有效的蜡烛:

.   
|   .       .
|   |   .   \
|   |   |   /

(以结束的.,仅包含|或平衡的,不一定与\和相邻/,可以是任意长度。)

这是一些无效的蜡烛:

.       .   .   .
\       |   |   |
|           |   |
\   .   |   !   

(不平衡\,没有烛台,断开连接|,没有字符,没有被种植在地面上。)

点燃的蜡烛将用.以下任意字符(您选择的)替换有效蜡烛上的:

@ ^ & " ~

您必须至少使用一个,并且您-10%的程序中使用的每个字符都会获得奖励,以使每个字符都可以出现在点燃的蜡烛上。如果您使用🔥表情符号,则会获得一个-15字节奖励,如果使用了,它将在百分比奖励之前应用。字节数已减少!

这是一个,因此以字节为单位的最短代码获胜。

IO范例

input: 8,
.         .
| . ! . . |.  . . .
| | | | | | | | | |
output:
@         @
| @ ! @ @ |.  @ @ @
| | | | | | | | | |
input: 14,
   // nothing
output: :(
input: 15,
.   ..  . .  ". .
| . ||  | |  || !
output: 9 // more candles required; 15 - (6 unlit) = 9 (thanks to @AndersKaseorg for catching my mistakes (plural)!)
input: 7,
.
/        ~
|        \  .
/  &   " /  |
\  | @ | | . . . . .
\  | | | | | 1 l I |
output: 
&
/        ~
|        \  .
/  &   " /  |
\  | @ | | ^ . . . 🔥
\  | | | | | 1 l I |
input: 5,
. .             |
i Q no candl es . |3-.
output: :(

排行榜

这是一个堆栈片段,用于按语言生成常规排行榜和获胜者概述。

为确保您的答案显示出来,请使用以下Markdown模板以标题开头。

# Language Name, N bytes

N您提交的文件大小在哪里。如果您提高了分数,则可以将旧分数保留在标题中,方法是将它们打掉。例如:

# Ruby, <s>104</s> <s>101</s> 96 bytes

如果您想在标头中包含多个数字(例如,因为您的分数是两个文件的总和,或者您想单独列出解释器标志罚分),请确保实际分数是标头中的最后一个数字:

# Perl, 43 + 2 (-p flag) = 45 bytes

您还可以将语言名称设置为链接,然后该链接将显示在页首横幅代码段中:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes


哦,这是我的生日。
Conor O'Brien

10
生日快乐!
级圣河

@steveverrill,谢谢^ _ ^
Conor O'Brien

我们可以假设输入用空格填充以形成矩形吗?
Downgoat

Answers:


4

Haskell,⌊(269字节-15)·0.9⁵⌋= 149

h('.',0)(e:f,c,o)=(f,2:c,e:o)
h(k,b)(f,c,o)|Just x<-lookup k$zip"\\|/"[4,0..]=(f,x+2*div b 2:c,k:o)|0<1=(f,2:c,k:o)
g l(f,c,o)=foldr h(f,[],'\n':o)$zip l c
d("",_,o)=o
d('@':_,_,_)=":("
d(f,_,_)=show$length f
f n=d.foldr g(take n$'@':cycle"🔥^&\"~",repeat 1,"").lines

示例运行:

*Main> putStr s
.
/        ~
|        \  .
/  &   " /  |
\  | @ | | . . . . .
\  | | | | | 1 l I |
*Main> putStr (f 3 s)
^
/        ~
|        \  .
/  &   " /  |
\  | @ | | 🔥 . . . @
\  | | | | | 1 l I |

假定作者的评论之一允许每行输入至少与上一行一样长。


看来我们有了新的第一名!
Conor O'Brien 2015年

5

Python 2,带有奖励的529字节,303

  • 假定第一行上有一个整数。
  • 不假定间距一致。不假设蜡烛栏为空。

战略:

  • 获取输入作为列表。
  • 反向并将其映射到列列表。
  • 测试和操作。
  • 将其映射回行,将其反转,合并各行。

import re;R=raw_input;C=str.count;G=lambda x:[y if y else' 'for y in x];H=lambda x:[''.join(G(v))for v in map(None,*x)];F=re.findall;t,r,i,g,d=0,u"🔥~\"&^@",[],r'^[|/\\]+[%s](?=\s|$)',R()
while d:i+=[d];d=R()
c=int(F('\d+',i.pop(0))[0]);i=i[::-1];m=H(i)
t+=sum(1 for x in m if F(g%r,x))
for p,n in enumerate(m):
 try:b=F(g%'\.',n)[0]
 except:continue
 if C(b,'/')==C(b,'\\')and t<c:t+=1;m[p]=re.sub('\.',r[0],n,1)
 if len(r)>1:r=r[1:]
m='\n'.join(H(m)[::-1])
d=":("if t<1 else`c-t`+" more candles required"if t<c else m;print d

测试:

5,
*      *               *
  *        *
*
                 *
@     @       @     @
|     |   .   |     |
|     |   |   |     |

*      *               *
  *        *            
*                       
                 *      
@     @       @     @   
|     |   🔥  |     |   
|     |   |   |     |   

3,
. . .       
| \ |

1 more candles required

3,  
. . .

. .      .
| |      |

. . .     

🔥 ~      "
| |      |

您能否编辑以澄清奖金前后的分数,以及字节数是否带有注释?
2015年

我正在尝试进一步打高尔夫球。我想尝试一个Pyth版本。
2015年

1
凉!谢谢。佩斯(Pyth)祝你好运!^ _ ^
Conor O'Brien 2015年

1
为什么不删除所有注释,换行符等?
RK。

您可以摆脱“需要更多蜡烛”位;假设是这样的数值输出。
Conor O'Brien 2015年

3

JavaScript(ES6),328字节(得分:184)

我试图击败Haskell解决方案,但是考虑到所有需要发生的逻辑,这实际上是一个相当有竞争力的选择。

得分计算为:Math.floor((328-15)*Math.pow(0.9,5)),使用UTF-8编码在文件中计数的字节,并通过测试和确认io.js --harmony_arrow_functions

解决方案

eval("(n,s)=>{q=x=>x[0].map((_,c)=>x#[c]));h='Q';c=5;t=n;m=q(s.split('\\n')X#.match(/^ *[Q][\\\\//|]+$/)&&xR\\L==xR/L&&t-->0?xR./,c>1?h[c--]:'@'):x);return t==n?':(':t>0?1+t:q(mX.join('\\n')R@/,'🔥')}"[k='replace'](/[A-Z]/g,x=>({X:"#.split('')))#.join(''))",R:"[k](/\\",Q:'.@^&"~',L:"/g,'').length"}[x]))[k](/#/g,'.map(x=>x'))

要求:数组必须用空格填充为矩形。

说明:所有评估错误都设置了一个变量(该变量保存k在字符串中replace以节省一些字节),并从339字节的字符串中减少了11个字节,我可以将其取消高尔夫:

(num_candles_desired, string) => {
    transpose = array => array[0].map((_, index) => array.map(row => row[index]));
    candle_tips = '.@^&"~';
    c = 5; // decrementing index into candle_tips when > 1.
    candles_left = num_candles_desired;
    transposed_normal_output = transpose(
            string.split('\n').map(line => line.split(''))
        ).map(col_array => col_array.join(''))
         // the next map does the actual logic: finds possible candles with
         // a regex, checks that the \ chars match the / chars in number,
         // then decrements the candles_left index while changing the . to a
         // lit flame.
         .map(col => col.match(/^ *[.@^&"~][\\//|]+$/) 
                   && col.replace(/\\/g,'').length == col.replace(/\//g,'').length
                   && candles_left-- > 0 ? x.replace(/\./, c > 1 ? candle_tips[c--] 
                                                                 : '~')
                                         : x);
    return candles_left == num_candles_desired ? ':('
                  : candles_left > 0 ? 1 + candles_left 
                  : transpose(
                        transposed_normal_output.map(col => col.split(''))
                    ).map(row_array => row_array.join('')).join('\n')
                     // as promised, we include the emoji at least once if we can.
                     // the leading backslash is unnecessary and comes from the
                     // above metaprogramming-compression with eval().
                     .replace(/\@/,'🔥')
}

由于请求了示例I / O,因此这是我运行的测试套件,

Reference example #1, lighting 8 candles...
Input:
.         .        
| . ! . . |.  . . .
| | | | | | | | | |
Output:
~         🔥        
| " ! & ^ |.  @ @ @
| | | | | | | | | |
Reference example #2, lighting 14 candles...
Input:

Output:
:(
Reference example #3, lighting 15 candles...
Input:
.   ..  . .  ". .
| . ||  | |  || !
Output:
9
Reference example #4, lighting 7 candles...
Input:
.                   
/        ~          
|        \  .       
/  &   " /  |       
\  | @ | | . . . . .
\  | | | | | 1 l I |
Output:
~                   
/        ~          
|        \  .       
/  &   " /  |       
\  | 🔥 | | @ . . . @
\  | | | | | 1 l I |
Reference example #5, lighting 5 candles...
Input:
. .             |     
i Q no candl es . |3-.
Output:
:(

一些示例I / OS?
Conor O'Brien 2015年

1
@CᴏɴᴏʀO'Bʀɪᴇɴ添加了。
CR Drost 2015年
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.