DCSS停尸房文件解析器


9

在此挑战中,您需要解析来自Roguelike游戏《地牢爬行石汤》的停尸间文件,并将其输出到STDOUT。

这些太平间文件是什么?

死后,将生成一个文本文件,其中包含该字符的数据。您可以看到角色拥有的装备,在最近几回合中发生了什么以及他杀死了多少怪物。

您可以在此处找到一个太平间文件示例

挑战

您的工作是制作一个程序,以从STDIN中获取这些文件之一,进行解析,然后将数据输出至STDOUT。

为了使这一挑战变得容易一些,您只需解析第一段文本。(直到......为止The game lasted <time> (<turns> turns).

您需要解析并输出以下信息:

  • 版本号。
  • 比分。
  • 角色名称,标题,种族和阶级。
  • 角色等级。
  • 死亡/胜利的原因。
  • 运行的转数持续。

例:

Dungeon Crawl Stone Soup version <version number> character file.

<score> <name> the <title> (level <level>, 224/224 HPs)
         Began as a <race> <class> on Mar 16, 2015.
         Was the Champion of the Shining One.
         <cause of death/victory>

         The game lasted 16:11:01 (<turns> turns).

测试用例

测试用例1-胜利

输入文件

输出示例-胜利:

Version: 0.16.0-8-gd9ae3a8 (webtiles)
Score: 16059087
Name: Ryuzilla the Conqueror
Character: Gargoyle Berserker
Level: 27
Cause of Death/Victory: Escaped with the Orb and 15 runes on Mar 17 2015!
Turns: 97605

测试案例2-死亡

输入文件

输出示例-死亡:

Version: 0.16-a0-3667-g690a316 (webtiles)
Score: 462
Name: 8Escape the Ruffian
Character: Bearkin Transmuter
Level: 6
Cause of Death/Victory: Slain by an orc wielding a +0 trident (3 damage) on level 4 of the Dungeon.
Turns: 3698

规则

  • 这是 因此最短的代码胜出。
  • 如果出现平局,则最早的答案将获胜。
  • 没有标准漏洞。
  • 文件输入必须来自STDIN
  • 输出必须发送到STDOUT
  • 输出之前的标签(例如Turns:)是可选的。

启发式的示例代码

DCSS中的停尸房文件生成代码


输出实际上是否需要包含像这样的线标签Version:或足以包含以相同顺序输出信息的信息,每行一个?
马丁·恩德

@MartinBüttner标签是可选的。
DJgamer98

种族和阶级总是一个字吗?
马丁·恩德

@MartinBüttner有些种族和阶级是两个词,例如Vine Stalker,Abyssal Knight和Deep Elf。
DJgamer98

2
是否有这种停尸房文件格式的规范,或仅这些示例?
圣保罗Ebermann

Answers:


3

Perl,151个字节

148个代码+ 3个开关(-0, -l, -p)。我相信这可以改善:)

接收来自STDIN的输入,并在接收到EOF时打印结果。

perl -lp0e 's/\.{3}|\s/ /g;y/ //s;$_=join$\,(/(\d.*?).{15}\..(\d+).(.+?).\(.+?(\d+).+?\b(?:a|an) (.+?) o.+? ([^.!]+[.!])[^.!]*?(\d+)[^(]+\)..\3/)[0..2,4,3,5..7]'

取消高尔夫:

use strict;
use warnings;

# set the input record separator to undef (the -0 switch)
$/=undef;
# read the text (the -l switch)
$_=<STDIN>;

# replace all '...' and spaces by a ' '
s/\.{3}|\s/ /g;
# squeeze all contiguous spaces into a single space
y/ //s;
# collect the captured groups into @p
my @p=
/(\d.*?).{15}\..      # version is the first string starting with a digit and ending 15 characters before the period
 (\d+).               # points is the next string with only digits
 (.+?).\(.+?          # name starts after a gap of one character
 (\d+).+?\b(?:a|an)\s # level is inside the next open paranthesis
 (.+?)\so.+?\s        # race, class occur after the 'a' or 'an' and end before ' o' i.e. (' on')
 ([^.!]+[.!])[^.!]*?  # cause of death is the a sentence ending with '.' or '!'
 (\d+)[^(]+\)..\3     # turns is the next sentence with digits within parantheses, followed by 2 characters and the player's name
/x;
$_=join"\n",@p[0..2,4,3,5..7]; # the level and race lines need to be swapped

# print the output (the -p switch)
print $_;

ideone.com


3

F#,377个字节

open System.Text.RegularExpressions
let s=System.String.IsNullOrWhiteSpace>>not
let m f=Regex.Match((f+"").Split[|'\r';'\n'|]|>Seq.filter s|>Seq.take 8|>Seq.reduce(fun a z->a+z.Trim()), ".*n (.*) c.*\.([0-9]+) (.*) \(l.* (.*),.*a (.*) o.*\.(?:(S.*)|W.*(E.*)).*.T.*\((.*) .*\).").Groups|>Seq.cast<Group>|>Seq.skip 1|>Seq.map(fun z ->z.Value)|>Seq.filter s|>Seq.iter(printfn"%s")

3

Javascript(ES6), 297 230字节

目前,这是一个测试驱动的正则表达式。

它只是替换了不需要的信息并保留了重要的内容。

它创建一个仅返回所需文本的匿名函数。

_=>_.replace(/^.+version(.*) character file\.([\n\r]+)(\d+)([^\(]+) \([^\d]+( \d+),.+\n\s+.+as a(.+) on.+\n\s+(?:Was.+One\.\n)?((?:.|\n)+[!.])\n(?:.|\n)+\((\d+)(?:.|\n)+$/,'$1\n$3\n‌​$4\n$6\n$5\n$7\n$8').replace(/\s+(\.{3} ?)?/,' ')

这不是野兽吗?


感谢sysreq提出的有关标签是可选标签的提示。那节省了我67个字节


您可以在以下位置测试resulgar表达式:https ://regex101.com/r/zY0sQ0/1


标签是可选的;您可以通过省略它们来节省很多字节。

1
@sysreq什么...?
Ismael Miguel


2
我的意思是_=>_.replace(/^.+version(.*) character file\.([\n\r]+)(\d+)([^\(]+) \([^\d]+( \d+),.+\n\s+.+as a(.+) on.+\n\s+(?:Was.+One\.\n)?((?:.|\n)+[!.])\n(?:.|\n)+\((\d+)(?:.|\n)+$/,'$1\n$3\n$4\n$6\n$5\n$7\n$8').replace(/\s+(\.{3} ?)?/,' ')仅230个字节就可以接受的解决方案
2015年

1
@sysreq很抱歉花了这么长时间说了什么。我一直在看帖子,但我在平板电脑上。您不知道在平板电脑中进行任何操作都会多么痛苦。我已经用您的无标签版本替换了我的代码。非常感谢您的提示。
Ismael Miguel

2

Python3,472个字节

我以为我可以使这个更短。不过,我击败自己的提交并不奇怪。像这样运行它python3 dcss.py morgue-file.txt

import sys
n="\n"
s=" "
f=open(sys.argv[1],'r').read().split(n)[:11]
m=range
a=len
d=","
for i in m(a(f)):
 f[i]=f[i].split(s)
 for x in m(a(f[i])):
  f[i][x]=f[i][x].strip()
h=f[0]
g=f[10]
k=f[2]
def r(j,u):
 j=list(j)
 while u in j:
  j.remove(u)
 return"".join(j)
def l(x):
 c=s
 for i in m(a(x)):
  c+=x[i]+s
 return c.strip()
print(h[6]+s+h[7]+n+k[0]+n+g[0]+s+g[1]+s+g[2]+n+r(g[3],"(")+s+r(g[4],")")+n+r(k[5],d)+n+r(l(f[4])+l(f[5])+l(f[6])+l(f[7]),".")+n+r(g[17],d))

2

围棋,589个 502 489 487字节

package main;import(."fmt";."io/ioutil";"os";."strings");func d(z,ch string)string{return Map(func(r rune)rune{if IndexRune(ch,r)<0{return r};return -1},z)};func main(){x:=Split;f,_:=ReadFile(os.Args[1]);n:="\n";l:=" ";m:=",";h:=".";q:=x(string(f),n)[:11];k:=x(q[0],l);y:=x(q[10],l);u:=x(q[2],l);g:="";for _,e:=range Fields(d(q[4],n+h)+l+d(q[5],n+h)+l+d(q[6],n+h)+l+d(q[7],n+h)){g=g+e+l};Print(k[6]+l+k[7]+n+u[0]+n+y[0]+l+y[1]+l+y[2]+n+d(y[3]+l+y[4],"()")+n+d(u[5],m)+n+g+n+d(y[17],m))}

在运行go fmt,之后go fixgo vet这是“非联机”版本:

package main

import (
    . "fmt"
    . "io/ioutil"
    "os"
    . "strings"
)

func d(z, ch string) string {
    return Map(func(r rune) rune {
        if IndexRune(ch, r) < 0 {
            return r
        }
        return -1
    }, z)
}
func main() {
    x := Split
    f, _ := ReadFile(os.Args[1])
    n := "\n"
    l := " "
    m := ","
    h := "."
    q := x(string(f), n)[:11]
    k := x(q[0], l)
    y := x(q[10], l)
    u := x(q[2], l)
    g := ""
    for _, e := range Fields(d(q[4], n+h) + l + d(q[5], n+h) + l + d(q[6], n+h) + l + d(q[7], n+h)) {
        g = g + e + l
    }
    Print(k[6] + l + k[7] + n + u[0] + n + y[0] + l + y[1] + l + y[2] + n + d(y[3]+l+y[4], "()") + n + d(u[5], m) + n + g + n + d(y[17], m))
}

编辑:使用点导入有很大帮助。

很不言自明,但我可以解释是否需要。这是我的第一个“真实” Go程序,我仍然是codegolf的初学者,因此欢迎您提出提示!

编辑:您说过“从STDIN中获取文件”,则可以通过运行go install <foldername>然后运行该脚本(如果已安装),<binaryname> morgue-file.txt或者go run main.go morgue.txt

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.