タオルを折ります!


19

私はどこかで、技術ではまだできないことの一つに、折り畳み式タオルがあると聞きました1。そのため、このステートメントが偽であることを証明するのは今やあなたの仕事です!

入力として文字列を指定すると、次のように長方形(タオル)で構成され、各タオルを半分に2回折ります。例えば:

+------+    +------+        +--+
|      |    |      |        |  |
|      |    |      |        |  |
|      | -> +------+ ->     +--+
|      |    
|      |    
|      |    
+------+    

タオルを折り畳むとき、最初に折り畳まれ、次に左から右に折り畳まれていることに注意してください。プログラムもこの動作を模倣する必要があります。また、テストケースでは、タオルは同じ場所にありますが、折りたたまれています。

ルール:

  • 入出力の標準的な方法。
  • 標準の抜け穴が適用されます。
  • 入力と出力は文字列でなければなりません。
  • タオルが互いに対して適切な場所にある限り、出力では何でも問題ありません。
  • タオルの各辺の長さは常に2で割り切れると仮定できます。
  • 入力として渡されるタオルは常に長方形になります。
  • タオルは常に分離されますが、可変量で分離される場合があります。

  • なので、最短のコードが勝ちます!

テストケース:

Input:
+------+
|      |
|      |
|      |
|      |
|      |
|      |
+------+
Output:
    +--+
    |  |
    |  |
    +--+




Input:
+--+ +--+ +--+
|  | |  | |  |
|  | |  | |  |
+--+ +--+ +--+

Output:
  ++   ++   ++
  ++   ++   ++


Input:
+----+
|    |
|    |
|    |
|    | ++
+----+ ++

Output:

   +-+
   | |
   +-+

        +

Input:
+--+
+--+     ++
         ||
         ||
         ++
Output:
  ++
          +
          +

1:これはGeobitsとLaikoniによって反証されました。しかし、どこかで聞いたことがあります。


なぜ下票なのですか?修正できるものがあれば、教えてください。
同志SparklePony


@Laikoniそれは技術が何でもできるようだ:
Xcoder氏17

@LuisMendo編集、タオルの間に常にスペースがあります。
同志SparklePony

与えられたタオルは常に水平に並んでいますか?他のタオルの下にタオルはないということですか?
デッドポッサム

回答:


5

網膜、245バイト

m1+`^((.)*(?!\+ )[+|]([- ])*[+|].*¶(?<-2>.)*)([+|][- ]*[+|])
$1$.4$* 
m+`^((.)*) ( *) (.*¶(?<-2>.)*)(?(2)(?!))\|\3\|
$1|$3|$4 $3 
m+`^((.)*)\|( *)\|(?=.*¶(?<-2>.)*(?(2)(?!)) )
$1+$.3$*-+
([+|])\1
 $1
(?!\+ )([+|])([- ])\2(\2*)\3\1
$.3$*   $1$3$1

オンラインでお試しください!

注:一部の行はスペースで終わります。説明:

m1+`                    Repeatedly search from the beginning of input
    ^((.)*              Optional indentation
      (?!\+ )           Towel does not start with a plus and a space
      [+|]([- ])*[+|]   A row of towel
      .*¶               Rest of the line
      (?<-2>.)*         Some indentation on the next line
     )
     ([+|][- ]*[+|])    Another row of towel
$1$.4$*                 Replace the second row with blanks

すべてのタオルの1行おきを削除します(これは、すべてのタオルの高さが均等であるため機能します)。

m+`             Repeatedly search
   ^((.)*)      Optional indentation
    ( *)        Find some space
    (.*¶        Rest of the line
     (?<-2>.)*) Matching indentation on the next line
    (?(2)(?!))  Ensure that the indentation exactly matches
    \|\3\|      Piece of towel
$1|$3|$4 $3     Swap the piece into the space above

取り外したタオルをすべて上に移動し、

m+`                 Repeatedly search
   ^((.)*)          Optional indentation
    \|( *)\|        Piece of towel
    (?=             Followed by
       .*¶          Rest of the line
       (?<-2>.)*    Matching indentation on the next line
       (?(2)(?!))   Ensure that the indentation exactly matches
        )           Nothing on the next line
$1+$.3$*-+          Change the piece into a towel bottom

タオルの底を固定し、効果的に折りたたみます。

([+|])\1    Match a row of towel of width 2, which is just ++ or ||
 $1         Change the first column to a space, which folds it

幅2のタオルを右に折ります。

(?!\+ )     Towel does not start with a plus and a space
([+|])      Start with a + or a |
([- ])\2    Then at least two -s or spaces
(\2*)\3     Then an even number of -s or spaces
\1          Then another + or |
$.3$*       Replace the left half with spaces
$1$3$1      Replace the first character of the right half

残りのタオルを右に折ります。


私はどのように正規表現作品のより多くの詳細な説明に興味がある
KritixiのLithos

@KritixiLithosそのようなもの、または何か特別なものがありましたか?
ニール

はい、ありがとうございます。そして、それ<-2>が.NETバランシンググループであると仮定するのは正しいですか?
KritixiのLithos

@KritixiLithosそれはそれらを使用します:(?<-2>.)*毎回キャプチャをポップ(.)*(?(2)(?!))ますので、同じ回数だけ繰り返されるようにキャプチャが残っていないことを確認しながら、やったより多くの回数を繰り返すことができません。
ニール

3

画像パッケージ付きオクターブ277 272バイト

function x=f(x)
[i,j,v]=find(bwlabel(x-32));a=@(w)accumarray(v,w,[],@max);r=-a(-i);R=a(i);s=-a(-j);S=a(j);x(:)=32;for k =1:nnz(r)
u=(r(k)+R(k)-1)/2;v=(s(k)+S(k)+1)/2;p=v:S(k);x(r(k),p)=45;x(u,p)=45;q=r(k):u;x(q,v)=124;x(q,S(k))=124;x(u,[v S(k)])=43;x(r(k),[v S(k)])=43;end

入力と出力は2D文字配列です。

オンラインでお試しください!またはすべてのテストケースを検証:1234。(endfunctionテストケースでは、関数を後続のコードから分離するためにのみ必要であることに注意してください。関数が独自のファイルに保存されている場合は必要ありません。)

読みやすいバージョンと説明

function x = f(x)
[i,j,v] = find(bwlabel(x-32)); % make background equal to 0 by subtracting 32.
% Then label each connected component (i.e. towel) with a unique integer
% Then get row indices (i) and column indices (j) of nonzero values (v)
a = @(w)accumarray(v,w,[],@max); % helper function. Gives the maximum of w for
% each group given by an integer label in v
r = -a(-i); % upper coordinate of each towel (minimum row index)
R = a(i); % lower coordinate of each towel (maximum row index)
s = -a(-j); % left coordinate of each towel (minimum column index)
S = a(j); % right coordinate of each towel (maximum column index)
x(:) = 32; % remove all towels: fill x with spaces
for k = 1:nnz(r) % for each original towel: create the new, folded towel 
    u = (r(k)+R(k)-1)/2; % new lower coordinate
    v = s(k)+S(k)+1)/2; % new left coordinate
    p = v:S(k); % column indices of horizontal edges
    x(r(k),p) = 45; % fill upper horizontal edge with '-'
    x(u,p) = 45; % fill lower horizontal edge with '-'
    q = r(k):u; % row indices of vertical edges
    x(q,v) = 124; % fill left vertical edge with '|'
    x(q,S(k)) = 124; % fill right vertical edge with '|'
    x(u,[v S(k)]) = 43; % fill lower corners with '+'
    x(r(k),[v S(k)]) = 43; % fill upper corners with '+'
end
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.