椅子を分析する


11

この課題は、DowngoatのAdjust your chairに基づいています

チャレンジ

新しい椅子を調整しました!それはあなたにぴったりです。ただし、ホイールはきしむ音がするので、修理担当者が調整を変更することを知っています。問題は、定規がないため、プログラムを作成して測定する必要があることです。 修理の人はとても長く待つことができます。そのため、コードはできるだけ短くする必要があります。
調節可能な椅子の例

O
|
|
| _
| |
|_|_
  |
  |
  O

5,3,2,2,1

O
|  _
|  |
|__|__
   |
   |
 __|__
 OOOOO

3,5,2,3,5

O
|      _
|______|______
 ______|______
 OOOOOOOOOOOOO

2,13,1,1,13

椅子パーツ

椅子にはさまざまなコンポーネントがあります。

O  <- Headrest
|
|  <- Backrest
|  _  <- Armrest
|  |
|__|__ <- Seat
   |  
   |   <- Leg
  _|_
  OOO  <- Wheels

椅子の詳細な説明

椅子のパーツは次のとおりです。


ヘッドレスト:背もたれの上に常に1つのヘッドレストがあります

O
|

背もたれ:の数|背もたれの高さです

O
|
|

座席:の数_座席幅|、アームレストの中央にあります。

__|__

アームレスト:の数|アームレストの高さです。これは座席の中央に挿入されます。

_
|
|

脚:の数|脚の高さです

|
|

ホイール:ホイールは脚の下の中央にあります。それらが複数ある場合、中央のホイールを除くすべてがその_上の行にあります。

_ _
OOO

出力

椅子があれば、さまざまな変数を出力します。

出力は次の順序である必要があります。

  1. 背もたれの高さ
  2. シート幅は常に奇数
  3. アームレストの高さは常に背もたれの高さよりも低い
  4. 脚の高さ
  5. ホイール数は常にシート幅以下で常に奇数

出力には末尾の改行が含まれるか、関数の場合は配列/リスト形式になります。

リーダーボード

回答が表示されるようにするには、次のマークダウンテンプレートを使用して、見出しから回答を開始してください。

# Language Name, N bytes

N提出のサイズはどこですか。スコアを改善する場合、古いスコアを打つことで見出しに残すことができます。例えば:

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

ヘッダーに複数の数字を含める場合(たとえば、スコアが2つのファイルの合計であるか、インタープリターフラグペナルティーを個別にリストする場合)、実際のスコアがヘッダーの最後の数字であることを確認します。

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

言語名をリンクにして、リーダーボードスニペットに表示することもできます。

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

肘掛けは測定されていますか?
Jアトキン

回答:


3

Lua、187バイト

もう少しゴルフができるかもしれないと思うが、今のところこれはいいと思う。また、出力をコンマ区切りにする必要がある場合は修正できますが、これでも要件の要件を満たします。

また、入力は一度に1行で入力されます。ホイールが入力されたら、Enterキーを押して空の行を入力し、入力を確定します。

c={}i=1t=""while c[i-1]~=""do c[i]=io.read():gsub("%s+","")t=t..c[i]a=(not a and c[i]:find("_")and i or a)s=(not s and c[i]:find("_|_")and i or s)i=i+1 end print(s-1,c[s]:len()-1,s-a,#c-s-2,c[#c-1]:len())

非ゴルフ

c={}
i=1
while c[i-1]~=""do 
    c[i]=io.read():gsub("%s+","")          --remove spaces
    a=(not a and c[i]:find"_"and i or a)   --armrest position
    s=(not s and c[i]:find"_|_"and i or s) --seat position
    i=i+1
end
print(s-1, c[s]:len()-1, s-a, #c-s-2, c[#c-1]:len())

(位置は上から下に測定されるため、上の「O」は位置1であり、車輪は最大の位置です。

  • 背もたれの高さは、座席の位置から1を引いたもので、上部の「O」を補正します。
  • 座席のサイズは、座席の位置にある弦の長さから1を引いたもので、背もたれを補正します。
  • アームレストの高さは、座席の位置からアームレストの位置を引いたものです。
  • 脚の高さは、椅子の高さ(#c)-座席の位置-2で、車輪と座席を補正します。
  • ホイールカウントは、最終文字列の長さです。

3

Groovy、161バイト!!!

わーい!!最後じゃない!!

f={s->a=s.split(/\n/)
b=a.findIndexOf{it.contains('|_')}
d=b-a.findIndexOf{it.contains('_')}
print"$b,${a[b].count('_')+1},$d,${a.size()-b-2},${s.count('O')-1}"}

ゴルフをしていない:

f={String s ->
    split = s.split(/\n/)
    bottomOfChairBack = split.findIndexOf {it.contains('|_')}
    armHeight = bottomOfChairBack-split.findIndexOf {it.contains('_')}
    width = split[bottomOfChairBack].count('_')+1
    height = split.size() - bottomOfChairBack - 2

    wheelCount = s.count('O')-1
    return [bottomOfChairBack, width, armHeight, height, wheelCount]
}

ungolfedプログラムのテスト:

assert f('''O
|
|
| _
| |
|_|_
  |
  |
  O''') == [5, 3, 2, 2, 1]

assert f('''O
|  _
|  |
|__|__
   |
   |
 __|__
 OOOOO''') == [3,5,2,3,5]

assert f('''O
|  _
|  |
|__|__
   |
   |
 __|__
 OOOOO''') == [3,5,2,3,5]

assert f('''O
|      _
|______|______
 ______|______
 OOOOOOOOOOOOO''') == [2,13,1,1,13]

2

Pyth、57 54 53 50バイト

おそらくさらにゴルフすることができます。単一文字列トリックのissacgのおかげで-3バイト。

=kjb.z
=H/k+b\|
-/k\_=G-/k\O2
--/k\|H=Nt/k+bd
N
hG

説明:

=kjb.z
=k              Assign k
     z          Input
  jb.           Join list by newlines

=H/k+b\|
=H              Assign H
  / +b\|        Count occurrences of "\n|"
   k            In input
                (Implicit: print backrest height)

-/k\_=G-/k\O2
     =G         Assign G
       -/k\O2   The number of wheels minus 1
-/k\_           Count the number of "_"
                (Implicit: print seat width)

--/k\|H=Nt/k+bd
       =N       Assign N
          /k+bd Count the number of lines starting with " "
         t      Subtract 1 (N is now the leg height)
  /k\|          Count the number of "|"
 -    H         Subtract the "|" for the backrest
-               Subtract leg height
                (Implicit: print armrest height)

N               Print leg height

hG              Print the number of wheels

1
1文字の文字列を作成するには、を使用します\。だから、"_"=\_
isaacg

痛い、方法は私がこれを打つつもりですません。)
Jアトキン

2

Perl、93 + 2 = 95 90 + 1 = 91 83 + 1 = 84バイト

どうやら出力はコンマ区切りを必要としません

perl -n chair.pl chairInput(フラグの1Bペナルティ)で呼び出します。

END{print$b,2+$u-$o,$a,$.-$b-2,$o-1}$u+=s/_//g;$o+=s/O//g;s/^\|//&&$b++&&/\|/&&$a++

ゴルフをしていない:

END{         # Put the END block first to save 1 ;
    print
        $b,   
    2+$u-$o,
    $a,
    $.-$b-2, # $. is the number of lines total
    $o-1
}
$u+=s/_//g; # count _s incrementally
$o+=s/O//g; # count Os incrementally
s/^\|// && $b++ # it's backrest if it starts with |
    && /\|/ && $a++ # and it's armrest if it has another one

前のバージョン:

で呼び出す perl -0n chair.pl < chairInput

s/^\|//&&$b++?/\|/&&$a++:$h++for split"
",$_;$,=",";print$b,2+s/_//g-($o=s/O//g),$a,$h-3,$o-1

説明:

s/^\|// && $back++   # the backrest is all lines starting with |
    ? /\|/ && $arm++ # the armrest is all of those lines with another |
    : $height++      # otherwise it counts for the seat height
    for split"
",$_;       # literal newline for 1 byte saved
$,=",";     # output separator
print
    $back,
    2+s/_//g-($o_count=s/O//g),  # you can find the seat size
                                 # from the different between the number
                                 # of Os and _s
    $arm,
    $height-3,
    $o_count-1

1

Python 3、160 158バイト

このコードは動作しますが、のみ、以下の条件に:1)armrest height > 0そうでない場合は_、カウントブレークと2)seat width > 1それ以外のアームレストブロック幅-1席と_カウント休憩。

def f(s):
 a=s.split("\n");x=[];y=[];l=len(a)
 for i in range(l):
  m=a[i].count("_")
  if m:x+=i,;y+=m,
 return x[1],y[1]+1,x[1]-x[0],l-x[1]-2,s.count("O")-1
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.