ピクセルを接続する


40

このようなテキストが与えられた場合:

# #### ## #
## #  ##  #
   ####  ##

同じテキストを出力しますが、ピクセルを文字で接続し─│┌┐└┘├┤┬┴┼ます。ピクセルに隣人がいない場合は、変更しないでください。

したがって、最後のテキストの出力は次のとおりです。

│ ─┬── ┌─ │
└─ │  ┌┘  │
   └──┘  ─┘
  • 入力をブール配列として受け取ることができます。
  • 入力には常に少なくとも1ピクセルが含まれます。
  • ボックス描画文字を1バイトとしてカウントできます。
  • 入力にスペースが埋め込まれていると想定できます。

テストケース

## #
=>
── #
###
 #
=>
─┬─
 │
##### ##
 # #  #
########
=>
─┬─┬─ ┌─
 │ │  │
─┴─┴──┴─
 # #
#####
 # #
=>
 │ │
─┼─┼─
 │ │
# # # # #
 # # # #
# # # # #
 # # # #
# # # # #
=>
# # # # #
 # # # #
# # # # #
 # # # #
# # # # #
#####
#####
#####
#####
#####
=>
┌┬┬┬┐
├┼┼┼┤
├┼┼┼┤
├┼┼┼┤
└┴┴┴┘

これはであるため、最短のコードが優先されます。


2
ハッシュ文字をピクセルとして使用する必要がありますか?入力をブール配列として受け取ることはできますか?
ローハンジュンジュンワラ

後続のスペースまたは改行がありますか?
Sanchises

btw:-|r7LJE3TW+ブロック文字の適切な1バイト文字置換です。
タイタス

回答:


7

ゼリー60 52 51 50 49 48 バイト

ṖḤ0;+Ḋ×
“µ³Q~E!G⁸ṗṫ\’ḃ61+9471Ọ⁾# j
ZÑ€4×Z++Ñ€ị¢Y

@ Dennisのおかげで1バイト節約できました

入力は、1と0のブール配列です。各列と各行を反復処理して、サイズ3の各中置の頭と尾を2進数のペアから小数に変換し、各中置の中心で乗算します。次に、それをそれ自体と合計して、へのインデックスを見つけます'#───│┌┐┬│└┘┴│├┤┼ '

オンラインでお試しください!ケース2)(ケース3)(ケース4

説明

これは、Jでの私の答えと同じ考えに依存していますが、各3x3サブアレイで処理する代わりに、同じインデックステーブルを取得しながら、各行と各列を処理します。

バイトの半分以上がボックス文字のリストの生成に費やされています'#───│┌┐┬│└┘┴│├┤┼ '。文字列リテラルはJellyで始まり、そのターミネーターに応じて異なる意味を持ちます。ここで、ターミネータとは、文字列がJelly コードページに従って各文字のコードポイントとして解析され、250桁のベースから10進数に変換されることを意味します。

“µ³Q~E!G⁸ṗṫ\’ => 10041542192416299030874093
(bijective base 61) => [1, 1, 1, 3, 13, 17, 45, 3, 21, 25, 53, 3, 29, 37, 61]
(add 9471 and convert to char) => '───│┌┐┬│└┘┴│├┤┼'

次に、その小数を全単射の基数61の数字のリストに変換し、それぞれ9471ずつインクリメントしてボックス文字の範囲に移動し、Pythonを使用してそれぞれを変換しますchr。次に、文字リテラル”#を先頭に追加し、スペースを追加します

ṖḤ0;+Ḋ×  Helper link - Input: 1d list A
Ṗ        Get all of A except the last value
 Ḥ       Double each value in it
  0;     Prepend a 0
    +    Add elementwise with
     Ḋ     All of A except the first value
      ×  Multiply elementwise by A

“µ³Q~E!G⁸ṗṫ\’ḃ61+9471Ọ⁾# j  Nilad. Represents '#───│┌┐┬│└┘┴│├┤┼ '
“µ³Q~E!G⁸ṗṫ\’               Get the code points of each char in the string and
                            convert from a list of base 250 digits to decimal
             ḃ61            Convert that to a list of digits in bijective base 61
                +9471       Add 9400 to each
                     Ọ      Convert from ordinals to chars, gets '───│┌┐┬│└┘┴│├┤┼'
                      ⁾#    A pair of chars ['#', ' ']
                         j  Join the pair using the box characters

ZÑ€4×Z++Ñ€ị¢Y  Input: 2d list M
Z              Transpose
 р            Apply the helper link to each row of the transpose (each column of M)
   4×          Multiply each by 4
     Z         Transpose
      +        Add elementwise with M
       +       Add elementwise with
        р       The helper link applied to each row of M
          ị¢   Use each result as an index to select into the nilad
            Y  Join using newlines
               Return and print implicitly

15

J82 72 66バイト

(ucp' #───│┌┐┬│└┘┴│├┤┼'){~]+]*3 3((2#.1 7 3 5{,);._3)0,.~0,.0,~0,]

入力は、1と0のブールテーブルです。ルールでは、ボックス文字はそれぞれ3バイトではなく1バイトとしてカウントされ、それがここで適用されていると述べています。

使用法

   f =: (ucp' #───│┌┐┬│└┘┴│├┤┼'){~]+]*3 3((2#.1 7 3 5{,);._3)0,.~0,.0,~0,]
   m =: 1 0 1 1 1 1 0 1 1 0 1 , 1 1 0 1 0 0 1 1 0 0 1 ,: 0 0 0 1 1 1 1 0 0 1 1
   m { ' #'
# #### ## #
## #  ##  #
   ####  ##
   f m
│ ─┬── ┌─ │
└─ │  ┌┘  │
   └──┘  ─┘
   ' #' {~ m =: 5 5 $ 1
   f m
┌┬┬┬┐
├┼┼┼┤
├┼┼┼┤
├┼┼┼┤
└┴┴┴┘
   ' #' {~ m =: 5 9 $ 1 0
# # # # #
 # # # # 
# # # # #
 # # # # 
# # # # #
   f m
# # # # #
 # # # # 
# # # # #
 # # # # 
# # # # #

説明

最初に、入力のすべての側面に0が埋め込まれます。

   ] m =: 1 0 1 1 1 1 0 1 1 0 1 , 1 1 0 1 0 0 1 1 0 0 1 ,: 0 0 0 1 1 1 1 0 0 1 1
1 0 1 1 1 1 0 1 1 0 1
1 1 0 1 0 0 1 1 0 0 1
0 0 0 1 1 1 1 0 0 1 1
   (0,.~0,.0,~0,]) m
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 1 1 1 0 1 1 0 1 0
0 1 1 0 1 0 0 1 1 0 0 1 0
0 0 0 0 1 1 1 1 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0

次に、サイズ3の各サブアレイが選択されます

   3 3 <;._3 (0,.~0,.0,~0,]) m
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│
│0 1 0│1 0 1│0 1 1│1 1 1│1 1 1│1 1 0│1 0 1│0 1 1│1 1 0│1 0 1│0 1 0│
│0 1 1│1 1 0│1 0 1│0 1 0│1 0 0│0 0 1│0 1 1│1 1 0│1 0 0│0 0 1│0 1 0│
├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│0 1 0│1 0 1│0 1 1│1 1 1│1 1 1│1 1 0│1 0 1│0 1 1│1 1 0│1 0 1│0 1 0│
│0 1 1│1 1 0│1 0 1│0 1 0│1 0 0│0 0 1│0 1 1│1 1 0│1 0 0│0 0 1│0 1 0│
│0 0 0│0 0 0│0 0 1│0 1 1│1 1 1│1 1 1│1 1 0│1 0 0│0 0 1│0 1 1│1 1 0│
├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│0 1 1│1 1 0│1 0 1│0 1 0│1 0 0│0 0 1│0 1 1│1 1 0│1 0 0│0 0 1│0 1 0│
│0 0 0│0 0 0│0 0 1│0 1 1│1 1 1│1 1 1│1 1 0│1 0 0│0 0 1│0 1 1│1 1 0│
│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│0 0 0│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

次に、各サブアレイ内の5つの値のみが考慮されます

┌───┐
│xAx│
│CED│
│xBx│
└───┘

ABCDは、各サブアレイを平坦化し、インデックスで選択することにより選択されます1 7 3 5。これらの値Eは、インデックス4で乗算されます。その後、2進数のリストから10進数に変換され、で増分されEます。x値は必要ありません。

   3 3 (4&{([+2#.*)1 7 3 5&{)@,;._3 (0,.~0,.0,~0,]) m
 5 0 2  8 4 3  0  6 3 0  5
10 3 0 13 0 0  6 11 0 0 13
 0 0 0 10 4 4 11  0 0 2 11

これは、以下の表に従って描画するキャラクターを選択するためのインデックスとして使用されます(ゴルフ用に少し並べ替えられています)。最後の列は、各サブ配列の出力値をボックス文字に一致させます。

 0  (space)  0
 1  #        1
 2  ┌        6
 3  ┬        8
 4  ┐        7
 5  ├        14
 6  ┼        16
 7  ┤        15
 8  └        10
 9  ┴        12
10  ┘        11
11  │        5, 9, 13
12  ─        2, 3, 4

また、Jでは、文字列' #───│┌┐┬│└┘┴│├┤┼'は8ビット文字を使用して、必要な17文字に対して長さ47(各バイト)を持たせます。コマンドはucpそれを16ビット文字に変換し、長さ17にすることができます。


13

JavaScript(ES6)、155 121 103 102文字

let f =
    
s=>s.replace(/#/g,(c,p)=>'#│─┘─└─┴││┐┤┌├┬┼'[t=x=>s[p+x]==c,8*t(w=s.search`
`+1)+4*t(1)+2*t(-1)+t(-w)])

console.log(f(
  '# #### ## #\n' +
  '## #  ##  #\n' +
  '   ####  ##'
));

編集:ETHproductionsの助けを借りて18バイトを保存編集:
replace()の1番目のパラメーターを使用して1バイトを保存'#'

使い方

#入力文字列で見つかったすべての文字を繰り返し処理します。それぞれについて#t()関数を使用して、その隣人も文字であるかどうかをテストします。

t = x => s[p + x] == c  // where c = '#'

機能のパラメタxt()現在の位置に関して隣人のオフセットですp。-1 / + 1を使用して、左/右隣をテストし、-w / + wを上/下隣をテストします(ここwで、行の幅、つまり最初の改行の位置+ 1)。

次のコンパスに従って、各ネイバーには異なる重み(1、2、4、または8)が割り当てられます。

  1
2 + 4
  8

各重みの組み合わせは、[0 .. 15]の一意の値につながります。たとえば、上部の隣人と右側の隣人の両方が設定されている場合、合計は1 + 4 = 5になり、このテーブルを使用して変換されます。

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
#  │  ─  ┘  ─  └  ─  ┴  │  │  ┐  ┤  ┌  ├  ┬  ┼

したがって、'#│─┘─└─┴││┐┤┌├┬┼'[weight_sum]期待されるキャラクターにつながります。


ハ、私たちは基本的に同じ考えを持っていました;)
ETHproductions

@ETHproductions-かなりそうです。^^
アーナルド

本当に、本当に素晴らしいテクニックです。あなたは私をしっかりとアウトゴルフしました:)
ETHproductions

あなたはそうのような2つのバイトを保存することができます:s=>(w=s[0].length+1,s=s.join`\n`).replace(/#/g,(_,p)=>'#│─┘─└─┴││┐┤┌├┬┼'[t=x=>s[p+x]>' ',t(-w)+2*t(-1)+4*t(1)+8*t(w)])
ETHproductions

そして、それは複数行の文字列に戻すために、実際にはるかに短いです:s=>s.replace(/#/g,(_,p)=>'#│─┘─└─┴││┐┤┌├┬┼'[t=x=>s[p+x]>' ',t(-w)+2*t(-1)+4*t(1)+8*t(w)],w=s.indexOf`\n`+1)
ETHproductions

8

パイソン2.7、318 315バイト(270 267文字)

私はこれをさらにゴルフすることができると確信しています(特に、その厄介な最初のコメントを取り除きたいです)が、ここに私のエントリがあります:

#encoding:utf-8
f=lambda t:(lambda l,s:'\n'.join(''.join((u'┼├┤│┬┌┐│┴└┘│───#'[(s==l[i][j-1])+2*(s==l[i][j+1])+4*(i<1 or s==l[i-1][j])+8*(i>len(l)-2 or s==l[i+1][j])],s)[s==l[i][j]]for j in range(len(l[i])-1))for i in range(len(l))))([l+' 'for l in t.split('\n')],' ')

全体がどのように機能するかの説明は次のとおりです。

#encoding:utf-8 # Dammit, Python. This adds an extra 16 bytes!
f=lambda t:( # main lambda function
    lambda l,s: # inner lambda so we can pass it "local constants" (see last line)
        '\n'.join( # join each line
            ''.join( # join each char within the line
                (u'┼├┤│┬┌┐│┴└┘│───#'[ # string of all possible characters, indexed by binary 0-15 based on adjacent chars
                    (s==l[i][j-1])+ # left
                    2*(s==l[i][j+1])+ # right
                    4*(i<1 or s==l[i-1][j])+ # up ('i<1' just yields zero in case this is the first line, so that we don't get index problems)
                    8*(i>len(l)-2 or s==l[i+1][j])], # down ('i>len(l)-2' is same as above)
                s)[s==l[i][j]] # if original is space, choose space, else choose the previously chosen box-drawing char
                for j in range(len(l[i])-1)) # do this process for each char (excluding last, which is a space)
            for i in range(len(l))) # do this for each line
    )([l+' ' for l in t.split('\n')],' ') # call the inner lambda with two parameters: first, the text split into lines; second, a space char (actually makes code shorter)

編集:前にいくつかのスペースを削除しました for ... in ...


9
代わりにUnicodeエンコーディングの問題を解決する必要があるのPython 3を使用して、私は...と思う
バイト司令

6

JavaScript(ES6)、150 139 133 131文字

a=>a.map((q,y)=>q.replace(/#/g,(c,x)=>"#│─┘─└─┴││┐┤┌├┬┼"[g=(X,Y=0)=>(a[Y+y]||[])[X+x]==c,g(0,1)*8+g(1)*4+g(-1)*2+g(0,-1)])).join`
`

入力を文字列の配列として受け取りますf(["###", " # "])

テストスニペット


5

アルパカ、414 + 2 = 416バイト

neighbourhoodV(^ v < >);states" ";statep"#"toA when4inV p,toB when3inV p andvs,toC when3inV p and^s,toD when3inV p and>s,toE when3inV p and<s,toF when2inV p and>s andvs,toG when2inV p andvs and<s,toH when2inV p and<s and^s,toI when2inV p and^s and>s,toJ when^p orvp,toK when<p or>p;stateA"┼";stateB"┴";stateC"┬";stateD"┤";stateE"├";stateF"┘";stateG"└";stateH"┌";stateI"┐";stateJ"│";stateK"─".

-fIフラグが必要です。

このソリューションは非常に多くのバイトを使用しますが、セルラーオートマトンを使用するという点で独特です。ALPACAは通常メタ言語として使用されますが、ここではプログラミング言語として使用しています。

ゴルフされていないバージョン:

neighbourhood V (^ v < >);
state s " ";
state p "#" to A when 4 in V p,
to B when 3 in V p and v s,
to C when 3 in V p and ^ s,
to D when 3 in V p and > s,
to E when 3 in V p and < s,
to F when 2 in V p and > s and v s,
to G when 2 in V p and v s and < s,
to H when 2 in V p and < s and ^ s,
to I when 2 in V p and ^ s and > s,
to J when ^ p or v p,
to K when < p or > p;
state A "┼";
state B "┴";
state C "┬";
state D "┤";
state E "├";
state F "┘";
state G "└";
state H "┌";
state I "┐";
state J "│";
state K "─".

4

PHP、203バイト

これはおそらくより短い方法で行うことができます。

while($s=fgets(STDIN))$f[]=$s;foreach($f as$y=>&$s)for($x=strlen($s);$x--;)if($s[$x]>$b=" ")$s[$x]="#───│┘└┴│┐┌┬│┤├┼"[($s[$x-1]>$b)+2*($s[$x+1]>$b)+4*($f[$y-1][$x]>$b)+8*($f[$y+1][$x]>$b)];echo join($f);

STDINから入力を読み取ります。で実行し-rます。


4

Python 3、149バイト

def f(s):S=' ';w=s.find('\n')+1;t=lambda i:(s+w*S)[i]>S;return[[c,'#│─┘─└─┴││┐┤┌├┬┼'[t(p-w)+2*t(p-1)+4*t(p+1)+8*t(p+w)]][c>S]for p,c in enumerate(s)]

のような入力を受け取り、のような##\n #\n出力を返します['─', '┐', '\n', ' ', '│', '\n']


3

R、199212バイト

編集:コードスニペットではなく、関数になりました。

入力はm1と0の行列です。これは非常にくてハッキーです。

function(m){
v=strsplit(" #─│┘│┐│┤──└┴┌┬├┼","")[[1]]
d=dim(m)+1
n=array(0,dim=d+1)
n[2:d[1],2:d[2]]=m
for(i in 0:(d[1]-2)){for(j in 0:(d[2]-2))cat(v[1+(p<-n[2+i,2+j])*sum(2^(0:3)*n[1:3+i,1:3+j][1:4*2])+p]);cat("\n")}
}

いくつかのテスト:

> m = matrix(c(1, 1, 1, 0, 1, 0), nrow=2, byrow=TRUE)
> v=strsplit(" #─│┘│┐│┤──└┴┌┬├┼","")[[1]]
> d=dim(m)+1
> n=array(0,dim=d+1)
> n[2:d[1],2:d[2]]=m
> for(i in 0:(d[1]-2)){for(j in 0:(d[2]-2))cat(v[1+(p<-n[2+i,2+j])*sum(2^(0:3)*n[1:3+i,1:3+j][1:4*2])+p]);cat("\n")}
─┬─
 │ 
> m = matrix(rep(1, 16), ncol=4)
> v=strsplit(" #─│┘│┐│┤──└┴┌┬├┼","")[[1]]
> d=dim(m)+1
> n=array(0,dim=d+1)
> n[2:d[1],2:d[2]]=m
> for(i in 0:(d[1]-2)){for(j in 0:(d[2]-2))cat(v[1+(p<-n[2+i,2+j])*sum(2^(0:3)*n[1:3+i,1:3+j][1:4*2])+p]);cat("\n")}
┌┬┬┐
├┼┼┤
├┼┼┤
└┴┴┘

提出は、完全なプログラムまたは機能でなければなりません。既存の変数からの読み取りは、入力の許可された形式ではありません。
TuxCrafting

ユニコード文字のいくつかをRでうまく動作させるにはどうすればいいですか?x = "\ U253C"は機能しますが、x = "┼"は機能しません。
Vlo

@TùxCräftîñg:今すぐ修正!
rturnbull

@Vlo utf-8は、私のネイティブOSエンコーディングです。x = "┼"私にとってはうまくいきます。
rturnbull

3

Perl、89 88バイト

の+2を含みます-0p。特殊文字は1バイトとしてカウントされますが、実際に単一文字として表示するには、-Cオプションも追加するのが最善です。

行がすべて同じ長さになるようにスペースを埋めてSTDINに入力します。

perl -C connect.pl
# #### ## #
## #  ##  #
   ####  ##
^D

connect.pl

#!/usr/bin/perl -0p
/
/;$n=".{@-}";s%#%substr"#───│└┘┴│┌┐┬│├┤┼",/\G##/+2*/#\G/+4*/#$n\G/s+8*/\G#$n#/s,1%eg

1

MATL、102文字

ネイバーに値(1、2、4、または8)を割り当てます。それらの合計は、描画文字を含む文字列の文字と一致します。まだ改善の余地はたくさんあると思いますが、大まかなドラフトには:

' #│││─┌└├─┐┘┤─┬┴┼' % String to be indexed
wt0J1+t4$(          % Get input, and append zeros at end of matrix (solely to avoid
                    %  indexing a non-existent second row/column for small inputs)
ttttt               % Get a whole load of duplicates to work on
3LXHY)              % Select rows 2:end from original matrix (one of the many dupes)
      w4LXIY)       % Select rows 1:end-1 from the 'destination' summing matrix)
             +HY(   % Add the neighbors below, store in 'destination' matrix
tIY)b3LY)2*+IY(     % +2* the neighbors above    +-------------------------------+
tHZ)b4LZ)4*+HZ(     % +4* the neighbors right    |(note: H and I contain 1:end-1 |
tIZ)b3LZ)8*+IZ(     % +8* the neighbors left     |  and 2:end respectively)      |
HH3$)               % Select the original matrix +-------------------------------+
*                % Make sure only cells that originally had a # get replaced
  1+)            % Add one because the original string is one-indexed. Index using ).

改善すべき点:

  • 合計部分全体を、マトリックスの回転したコピーで動作する何らかのループに置き換えることができます
  • 全体を捨てて、マトリックスを処理する単一のループに基づいて何かを作成します
  • モジュラーインデックスを使用して、元の配列から平坦化されたベクトルを操作し ます(?)

オンラインでお試しください!(ボックス描画文字をサポートしていない場合があります)

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