圣诞老人的决定


29

圣诞老人的决定:

在此挑战中,您将帮助圣诞老人确定名单上的某个人是顽皮还是好人,然后得到coaltoys

但不幸的是,Santa的组织混乱,在他的某些条目中naughtynicename字段的顺序错误。

输入值

输入将采用以下可互换格式:

  • 该人的名字(只能包含一个冒号a-zA-Z0-9
  • 这个词naughty后面直接跟一个冒号和一个非负整数,代表圣诞老人抓到你调皮的次数
  • 这个词nice后面直接跟一个冒号和一个非负整数,表示圣诞老人抓住你的好时光的次数

它们之间用单个空格(ASCII 32)隔开。

此外,该名称在名称Santa Claus->的各部分之间将没有空格SantaClaus

奖金:

  • (25%):他是圣诞老人,所以他需要检查列表两次,并确保没有重复项。(在这种情况下,它只会获得用户拥有的第一个分数)

例:

Quill naughty:4 nice:0
naughty:0 Doorknob nice:3
naughty:2 Quill nice:6
nice:3 balpha naughty:3
pops nice:4 naughty:2

输出:

输出应包括:

此人的姓名,后跟:

  • 如果中还有更多要点naughty,则coal
  • 如果还有更多要点nice,则toys
  • 但是如果naughtynice相等,那么needs more data

    输出示例:

  • 有组织奖金和重复搬家奖金:

Quill coal
Doorknob toys
balpha needs more data
pops toys
  • 没有奖金:

Quill coal
Doorknob toys
Quill toys
balpha needs more data
pops toys

最低字节数获胜!


4
测试用例中也有错字。您拼错了我们光荣的mod DorkNoob的名字:^)
FryAmTheEggman 2015年

9
@FryAmTheEggmanಠ_ಠ
门把手

2
不,乖不乖为有效名称
奎尔

1
那是个好主意……我想总是会有下一次……
Quill 2015年

1
“ balpha需要更多数据”听起来不错。
亚当·戴维斯

Answers:


4

Pyth,68个字节-25%= 51

V.zI-hA.g}\:kcNdY=+YGjd+G@c"needs more data
coal
toys"b._-Fmsecd\:SH

在线尝试:演示


5

朱莉娅176个 169字节

s->for l=split(s,"\n") M(r)=parse(matchall(r,l)[1]);g=M(r"e:\K\d+");b=M(r"y:\K\d+");println(replace(l,r" *\w+:\d+ *","")," ",g>b?"toys":b>g?"coal":"needs more data")end

这是一个匿名函数,它接受字符串并将结果打印到STDOUT。要给它起个名字,例如f=s->...

取消高尔夫:

function santa(s::AbstractString)
    # Split the input on newlines and process each line separately
    for l in split(s, "\n")
        # Define a function to get the number from the result of a
        # regular expression match
        M(r) = parse(matchall(r, l)[1])

        # Goodness
        g = M(r"e:\K\d+")

        # Badness
        b = M(r"y:\K\d+")

        # Get the name by replacing the naughty and nice specifications
        # with empty strings and print the line to STDOUT
        println(replace(l, r" *\w+:\d+ *", ""), " ",
                g > b ? "toys" : b > g ? "coal" : "needs more data")
    end
end


3

红宝石,144 123 155 * 0.75 = 116.25字节

->s{d={}
s.split("
").map{|l|
a=l.split
b=a.grep /:/
i,j,v=(b.sort*'').scan(/\d+/)+a-b
d[v]||(d[v]=0
puts v+' '+['needs more data','coal','toys'][i<=>j])}}

感谢histocrat的建议grep方法。

164 * .75 = 123字节

->s{d={}
s.split("
").map{|l|
a=l.split
b=a.select{|t|t[?:]}
i,j,v=(b.sort*'').scan(/\d+/)+a-b
d[v]||(d[v]=0
puts v+' '+['needs more data','coal','toys'][i<=>j])}}

144字节

->s{puts s.split("
").map{|l|b=(a=l.split).select{|t|t[?:]};i,j=(b.sort*'').scan(/\d+/);(a-b)[0]+' '+['needs more data','coal','toys'][i<=>j]}}

不打高尔夫球

->s{
  d={}
  s.split("
  ").map{ |l|
    a = l.split
    b = a.grep /:/
    i, j, v = (b.sort * '').scan(/\d+/) + a-b
    d[v] ||
      (d[v]=0
       puts v + ' ' + ['needs more data','coal','toys'][i<=>j]
      )
  }
}

用法:

# Assign the anonymous function to a variable
f = ->s{d={}
s.split("
").map{|l|
a=l.split
b=a.grep /:/
i,j,v=(b.sort*'').scan(/\d+/)+a-b
d[v]||(d[v]=0
puts v+' '+['needs more data','coal','toys'][i<=>j])}}

f["Quill naughty:4 nice:0
naughty:0 Doorknob nice:3
naughty:2 Quill nice:6
nice:3 balpha naughty:3
pops nice:4 naughty:2"]

Quill coal
Doorknob toys
balpha needs more data
pops toys

.select{|t|t[?:]}可以golfed到.grep(/:/)
histocrat

@histocrat哇,我完全忘记了这种方法。谢谢您:)
Vasu Adari 2015年

3

Perl,138 113 105 103 102 96-25%= 72

包括+1 -p

s/ *\w*(.):(\d+) */$$1=$2,()/eg;$$_++?$_='':s/\n/$".('needs more data',toys,coal)[$e<=>$y].$&/e

少打高尔夫球:

s/ *\w*(.):(\d+) */$$1=$2,()/eg;    # strip naughty/nice, set $a to naughty, $i to nice
                                    # $_ is now the input name followed by \n
$$_++ ? $_='' :                     # only output once per name
s/\n/                               # replace newlines with:
  $".                               # a space,
  ('needs more data',toys,coal)     # one of these strings,
  [$e<=>$y]                         # indexed by -1, 0 or 1
  .$&                               # and the matched newline.
/ex                                 # (/x only for legibility)

  • 更新113
    • 通过使用1个字母作为变量名nice或将其保存为25个字节naughty
    • 通过修复名称为last时的错误来释放5个字节
  • 更新105通过使用<=>索引输出字符串列表来节省8个字节。
  • 更新103使用正则表达式添加输出字符串可节省2个字节
  • 更新102通过使用nicenaughty代替2nd的最后一个字母来保存1个字节。
  • 更新96通过更改$$_ ? ... : ($$_++, ...)为保存6个字节$$_++ ? ... : ...
    (为什么我以前没有看到)。

2

JavaScript(ES6),174个字节-25%的奖励= 130.5分

s=>s.split`
`.map(l=>l.split` `.map(p=>(m=p.match(/\w:\d+/))?(n=+m[0].slice(2),m>"f")?b=n:c=n:a=p)&&d[a]?"":d[a]=a+" "+(b>c?`coal
`:b<c?`toys
`:`needs more data
`),d={}).join``

说明

s=>
  s.split`
`.map(l=>                   // for each line l of Santa's list
    l.split` `.map(p=>      // for each word p in l
      (m=p.match(/\w:\d+/)) // m = "y:x" for naughty, "e:x" for nice or null for name
        ?(n=+m[0].slice(2), // n = number at end of match
          m>"f")?b=n:c=n    // if naughty matched b = n, if nice matched c = n
        :a=p                // if no match, a = name
    )
    &&d[a]?"":              // if the name has been used before, add nothing to output
    d[a]=                   // else set d[name] to true

      // Add the appropriate text to the output
      a+" "+(b>c?`coal
`:b<c?`toys
`:`needs more data
`),

    // NOTE: This line is executed BEFORE the code above it in the map function...
    d={}                    // d = list of names that have been output
  )
  .join``                   // return the list of outputs as a string

测试



2

Lua,329字节-25%奖励= 246.75

a={...}p={}u=" "k=ipairs for i=1,#a/3 do p[i]={}end for i,v in k(a)do p[math.floor((i+2)/3)][(v:find("y:")and 3)or(v:find("e:")and 2)or 1]=v:gsub("%a+%:","")end for i,v in k(p)do d=tonumber b,g,n=d(v[3]),d(v[2]),v[1]if(not u:find(" "..n.." "))then u=u..n.." "print(n..(g<b and" coal"or g>b and" toys"or" needs more data"))end end

稍后将以非公开版本和说明进行编辑,此刻有点累。所有输入均通过命令行输入,以空格分隔。


2

Python 2,206字节-25%= 154.5

s=[]
x=[0,0]
for p in zip(*(iter(input().split()),)*3):
 for w in p:
  j=w.find(':')+1
  if j:x[j<6]=int(w[j:])
  else:N=w
 b,g=x
 if N not in s:print N,['needs more data','coal','toys'][(b>g)-(g>b)];s+=[N]

2

JavaScript(ES6)120(160-25%)

使用模板字符串的匿名函数,有4个重要的换行符,并且包括在字节数中

l=>l.split`
`.map(r=>k[r=r.replace(/\S+:(\d+)/g,(a,c)=>(t-=a[1]<'i'?c:-c,''),t=0).trim()]?'':k[r]=r+(t>0?` toys
`:t<0?` coal
`:` needs more data
`),k={}).join``
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.