10
\$\begingroup\$

Given

  • a matrix a of characters from u=" ╶╺╵└┕╹┖┗╴─╼┘┴┶┚┸┺╸╾━┙┵┷┛┹┻╷┌┍│├┝╿┞┡┐┬┮┤┼┾┦╀╄┑┭┯┥┽┿┩╃╇╻┎┏╽┟┢┃┠┣┒┰┲┧╁╆┨╂╊┓┱┳┪╅╈┫╉╋"
  • the coordinates of a submatrix as x,y,w,h (left, top, width>1, height>1)
  • a thickness t of 1 (as in ) or 2 (as in )

render an inner border for the submatrix with the specified thickness, taking into account existing lines.

x=4;y=1;w=2;h=3;t=2;
a=[' ┌───┐',
   '┌┼┐  │',
   '│└┼──┘',
   '└─┘   ']

// output
r=[' ┌───┐',
   '┌┼┐ ┏┪',
   '│└┼─╂┨',
   '└─┘ ┗┛']

When overwriting a line fragment, the new thickness should be the greater of the old thickness and t.

This isn't about input parsing or finding the Kolmogorov complexity of Unicode, so you may assume a,u,x,y,w,h,t are available to you as variables. Also, you may put the result in a variable r instead of returning or outputting it, as long as r is of the same type as a.

If your language forces you to put code in functions (C, Java, Haskell, etc) and your solution consists of a single function, you can omit the function header and footer.

Larger test:

x=4;y=1;w=24;h=4;t=1;
a=['┏┱─────┐         ┌┐     ┎──┲━┓',
   '┠╂─────┘         ││     ┃  ┗━┛',
   '┃┃               ││     ┃     ',
   '┠╂──┲━━┓  ┏━━━━┓ ││    ┌╂┰┐   ',
   '┃┃  ┗━━┩  ┃    ┃ └╆━┓  └╂┸┘   ',
   '┃┃     │  ┃    ┃  ┃ ┃   ┃     ',
   '┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     ']

// output
r=['┏┱─────┐         ┌┐     ┎──┲━┓',
   '┠╂──┬──┴─────────┼┼─────╂──╄━┛',
   '┃┃  │            ││     ┃  │  ',
   '┠╂──╆━━┓  ┏━━━━┓ ││    ┌╂┰┐│  ',
   '┃┃  ┗━━╃──╂────╂─┴╆━┱──┴╂┸┴┘  ',
   '┃┃     │  ┃    ┃  ┃ ┃   ┃     ',
   '┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     ']
\$\endgroup\$
  • \$\begingroup\$ will the input always hold 0 <= x < x + w < width(a) and 0 <= y < y + h < height(a)? \$\endgroup\$ – tsh Aug 29 '17 at 10:30
  • \$\begingroup\$ @tsh yes, input will be valid \$\endgroup\$ – ngn Aug 29 '17 at 10:42
  • \$\begingroup\$ Bah, my default font is dodgy - it shows some of those characters with the wrong heaviness unless you zoom in a lot. \$\endgroup\$ – Neil Aug 29 '17 at 11:51
  • \$\begingroup\$ @Neil I'm sorry about that. One workaround is to paste the examples in an editor where you can choose the font. \$\endgroup\$ – ngn Aug 29 '17 at 12:04
  • 1
    \$\begingroup\$ About your bounty - it is impossible to award three 150-rep bounties. You have to double the rep count every time you start another bounty on the same question. \$\endgroup\$ – MD XF Sep 5 '17 at 20:42
2
\$\begingroup\$

JavaScript, 218 bytes

(a,x,y,w,h,t,u)=>a.map((l,j)=>l.map((c,i)=>u[(g=(a,b)=>a?g(a/3|0,b/3|0)*3+Math.max(a%3,b%3):b)(u.indexOf(c),t*((j==y||j==y+h-1)*((i>x&&i<x+w)*9+(i>=x&&i<x+w-1))+(i==x||i==x+w-1)*((j>y&&j<y+h)*3+(j>=y&&j<y+h-1)*27)))]))

a should be taken as array of array of char.

f =

(a,x,y,w,h,t,u)=>a.map((l,j)=>l.map((c,i)=>u[(g=(a,b)=>a?g(a/3|0,b/3|0)*3+Math.max(a%3,b%3):b)(u.indexOf(c),t*((j==y||j==y+h-1)*((i>x&&i<x+w)*9+(i>=x&&i<x+w-1))+(i==x||i==x+w-1)*((j>y&&j<y+h)*3+(j>=y&&j<y+h-1)*27)))]))

x=4;y=1;w=24;h=4;t=1;
a=['┏┱─────┐         ┌┐     ┎──┲━┓',
   '┠╂─────┘         ││     ┃  ┗━┛',
   '┃┃               ││     ┃     ',
   '┠╂──┲━━┓  ┏━━━━┓ ││    ┌╂┰┐   ',
   '┃┃  ┗━━┩  ┃    ┃ └╆━┓  └╂┸┘   ',
   '┃┃     │  ┃    ┃  ┃ ┃   ┃     ',
   '┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     '].map(x => [...x])
u=" ╶╺╵└┕╹┖┗╴─╼┘┴┶┚┸┺╸╾━┙┵┷┛┹┻╷┌┍│├┝╿┞┡┐┬┮┤┼┾┦╀╄┑┭┯┥┽┿┩╃╇╻┎┏╽┟┢┃┠┣┒┰┲┧╁╆┨╂╊┓┱┳┪╅╈┫╉╋";

output = f(a,x,y,w,h,t,u).map(x => x.join('')).join('\n');
document.write(`<pre lang="en" style="font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,sans-serif;">${output}</pre>`)

\$\endgroup\$
  • \$\begingroup\$ as mentioned above, you are allowed to replace (a,x,y,w,h,t,u)=>... with r=... \$\endgroup\$ – ngn Aug 30 '17 at 7:42
2
+75
\$\begingroup\$

Python 3, 226 201 197 bytes

n,m=x+w-1,y+h-1
r=[*map(list,a)]
R=range
for i in R(x,x+w):
 for j in R(y,y+h):A,B=j in(y,m),i in(x,n);r[j][i]=u[sum(3**o*max((i<n*A,y<j*B,x<i*A,j<m*B)[o]*t,u.index(a[j][i])//3**o%3)for o in R(4))]

Try it online!

Ungolfed:

n,m=x+w-1,y+h-1
r=[*map(list,a)]
for i in range(x,x+w):
 for j in range(y,y+h):
  p=u.index(a[j][i])
  c=(p%3,p%9//3,p%27//9,p//27)
  A,B=j in(y,m),i in(x,n)
  P=(i<n*A,y<j*B,x<i*A,j<m*B)
  l=sum(max(P[o]*t,c[o])*3**o for o in range(4))
  r[j][i]=u[l]
\$\endgroup\$
  • \$\begingroup\$ (p%3,p%9//3,p%27//9,p//27)[o]p//3**o%3 saves a bunch of bytes. Then max(…)*3**o for3**o*max(…)for saves one more. And then you can eke one more out by inlining the 3** and shuffling P around to index it with o%5-1, yielding: sum(o*max((i<n*A,j<m*B,y<j*B,x<i*A)[o%5-1]*t,p//o%3)for o in(1,3,9,27)) \$\endgroup\$ – Lynn Sep 1 '17 at 16:12
  • \$\begingroup\$ Err, that last step is a bad idea. Instead you can do R=range and get it down to 201 \$\endgroup\$ – Lynn Sep 1 '17 at 16:15
1
\$\begingroup\$

JavaScript (ES6), 174 bytes

r=a.map((l,j)=>l.map((c,i)=>u[c=u.indexOf(c),g=n=>c/n%3<t&&g(n,c+=n),j==y|j==h&&(i>=x&i<w&&g(1),i>x&i<=w&&g(9)),i==x|i==w&&(j>=y&j<h&&g(27),j>y&j<=h&&g(3)),c]),w+=x-1,h+=y-1)

a=`
┏┱─────┐         ┌┐     ┎──┲━┓
┠╂─────┘         ││     ┃  ┗━┛
┃┃               ││     ┃     
┠╂──┲━━┓  ┏━━━━┓ ││    ┌╂┰┐   
┃┃  ┗━━┩  ┃    ┃ └╆━┓  └╂┸┘   
┃┃     │  ┃    ┃  ┃ ┃   ┃     
┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     
`.match(/.+/g).map(s=>[...s]);
x=4;
y=1;
w=24;
h=4;
t=1;
u=" ╶╺╵└┕╹┖┗╴─╼┘┴┶┚┸┺╸╾━┙┵┷┛┹┻╷┌┍│├┝╿┞┡┐┬┮┤┼┾┦╀╄┑┭┯┥┽┿┩╃╇╻┎┏╽┟┢┃┠┣┒┰┲┧╁╆┨╂╊┓┱┳┪╅╈┫╉╋";
r=a.map((l,j)=>l.map((c,i)=>u[c=u.indexOf(c),g=n=>c/n%3<t&&g(n,c+=n),j==y|j==h&&(i>=x&i<w&&g(1),i>x&i<=w&&g(9)),i==x|i==w&&(j>=y&j<h&&g(27),j>y&j<=h&&g(3)),c]),w+=x-1,h+=y-1)
;document.write(`<pre style="font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,sans-serif">${r.map(s=>s.join``).join`\n`}</pre>`);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.