DCSS遺体安置所ファイルパーサー


9

この課題では、ローグライクゲームのDungeon Crawl Stone Soupの遺体安置所ファイルを解析して、STDOUTに出力する必要があります。

これらの遺体安置所のファイルは何ですか?

死ぬと、そのキャラクターのデータが入ったテキストファイルが生成されます。キャラクターが持っていた装備、最後の数ターンに何が起こったのか、そして彼が殺したモンスターの数がわかります。

あなたはここで死体安置所ファイルの例を見つけることができます

チャレンジ

あなたの仕事は、これらのファイルの1つを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:それとも、1行に1つずつ同じ順序で情報を出力するのに十分ですか?
Martin Ender 2015年

@MartinBüttnerラベルはオプションです。
DJgamer98 2015年

レースとクラスは常にそれぞれ1つの単語になりますか?
マーティンエンダー2015年

@MartinBüttnerVine Stalker、Abyssal Knight、Deep Elfのように、いくつかの種族とクラスは2つの単語です。
DJgamer98 2015年

2
この遺体安置所のファイル形式の仕様はありますか、それともこれらの例だけですか?
パウロEbermann

回答:


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バイト節約できました。


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バイトで受け入れ可能な解決策である

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

Go、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 fmtgo fixgo vetここでは「ungolfed」バージョンは次のとおりです。

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

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