13
\$\begingroup\$

Lets say I was writing something, and I accidentally wrote it in the wrong box:

+-------+--------+
| Verbs | Nouns  |
+-------+--------+
| Truck |        |
| eat   | Banana |
|       | Car    |
+-------+--------+

So I, being lazy, merely draw an arrow to the space it should be:

+-------+--------+
| Verbs | Nouns  |
+-------+--------+
|[Truck]--->     |
| eat   | Banana |
|       | Car    |
+-------+--------+

Your job is to make it look pretty - turn it into this:

+-------+--------+
| Verbs | Nouns  |
+-------+--------+
|       | Truck  |
| eat   | Banana |
|       | Car    |
+-------+--------+

Rules

  • Phrases or words are surrounded by brackets ([...])
  • Multiline entries are denoted as [...] (multiline removed), newline, [...]--...-->. So the arrow always comes out of the bottom right bracket

For example:

+-----------+----------+
|   Verbs   |  Nouns   |
+-----------+----------+
|           |          |
|[is a noun]->         |
| eat       | Banana   |
|           | Car      |
+-----------+----------+

Turns into:

+-----------+----------+
|   Verbs   |  Nouns   |
+-----------+----------+
|           |          |
|           | is a noun|
| eat       | Banana   |
|           | Car      |
+-----------+----------+
  • Boxes are bounded by pluses ("+"), and it should be assumed that arrows will not pass over a plus
  • There will always be room at the end of an arrow for the word or phrase to fit
  • Order matters - the word must appear where the arrow points to, though vertical alignment is not important
  • Arrows can turn by 90 degrees at a time, but only between "/" and "\" (see below)

For example:

    +-----------+----------+
    |   Verbs   |  Nouns   |
    +-----------+----------+
    | [Truck]--\|          |
  /------------/|        <--\
  | | eat       | Banana   ||
  | |  /--\     | Car      ||
  | +--|--|-----+----------+|
  \----/  \-----------------/

The Competition

Any input is acceptable (text file, STDIN, etc.)
any readable output is acceptable (text file, STDOUT, etc)

Here are some more examples:

This is code golf, so shortest code wins!

Some test cases:

   +--------+----------+-------------+
   | Colors |  Foods   |  Countries  |
   +--------+----------+-------------+
   | red    | pizza    | Switzerland |
 /-->     /---->       |             |
 | |[kale]/ | hot dogs | Brazil      |
 | |     <----------------------\    |
 | | orange |[yellow]\ | [green]/    |
 | +--------+--------|-+-------------+
 \-------------------/


/------------------------------------------\
|                                          |
|  +------------------+----------+-------+ |
|  | frog             |          |       | |
|  | forge            | dog      | eagle | |
|  | foundation       | diligent | ease  | |
|  |[Brother]--\      | down     | elf   | |
|  |           |      |[egg]--\  |[fort]---/
\---->         |      |       |  |       |  
   |           \-\    |       \----->    |  
   +-------------|----+----------+       |  
   |   Boy       \-->            |       |  
   |   Bull                      |       |  
   |   Browser                   |       |  
   +-----------------------------+-------+ 


             /--------------------------------------\
             |                     /---------------\|
/------------|---------------------/               ||
|            |               /---------------\     ||
|  +---------|-+-----------+-|------+--------|+    ||
\---->       | |  clarinet | \>     | [drums]/|    ||
   | [viola]\\--->         |        +---------+    ||
   |        \----->        |        |         |/---/|
   +-----------+ [violin]\ |        | [cello]--/    |
   |        <------------/ |        +---------+     |
   |           +-----------+      <----------\|     |
   |           |           |        |  [tuba]/|     |
   |           | [piano]\  |        |         |     |
   |        <-----------/  |        |         |     |
   |           +-----------+--------+         |     |
   |           |                    |[trumpet]------/
   | [flute]----->                  |         |      
   |           |     saxaphone      |         |      
   +-----------+--------------------+---------+ 
\$\endgroup\$
17
  • 5
    \$\begingroup\$ This is a nice challenge, it just seems bloody hard! \$\endgroup\$
    – Beta Decay
    Commented Sep 13, 2014 at 14:38
  • 2
    \$\begingroup\$ While attempting to write box identification, I've noted that it's possible for it to be ambiguous how many boxes there are if you have enough arrows crossing a boundary, for example, How many boxes are there in this layout pastebin.com/xyBjTAwK ? Can we have some guarantee that this won't occur, or an explanation of expected behaviour for the situation. \$\endgroup\$ Commented Sep 14, 2014 at 13:23
  • 2
    \$\begingroup\$ @VisualMelon You can assume that won't happen. There will never be a case where 2 pluses are in the next row/column and not be connected. Also, about the brackets, that was a flaw on my part. You can assume that anything in brackets moves. \$\endgroup\$ Commented Sep 14, 2014 at 13:38
  • 1
    \$\begingroup\$ All I ask is that it outputs a table that looks like the original table, except with the values moves. In other words, as long as it looks like a table (e.g. no random line breaks), I don't care about things I can't see (e.g. trailing spaces/line breaks) \$\endgroup\$ Commented Sep 14, 2014 at 21:11
  • 1
    \$\begingroup\$ On the order, if the top of the item is put right where the arrow is pointing, then it should be in the right place. For example, "Brother" would go to the right of "Boy". You can assume that the arrow is pointing to the first row of the destination of a multirow item. On what qualifies an item - Anything surrounded by brackets (even ><|^ and whitespace) is considered part of the item. If there are brackets above it without an arrow coming out, then it is part of a multiline item. \$\endgroup\$ Commented Sep 15, 2014 at 11:10

2 Answers 2

8
\$\begingroup\$

Python, 700 681 676 667 characters

Not fully golfed yet, but wanted to put up an answer.

import os
D=map(list,os.read(0,9999).split('\n'))
Z=Q=[-1,0,1,0,-1]
J=''.join
def T(y,x,d,_):c=D[y][x];D[y][x]=' ';d={'\\':3-d,'/':1-d}.get(c,d)%4;j=(y+Q[d],x+Q[d+1],d,c=='>');return c in'<>'and j or T(*j)
def F(y,x,d):
 if y<Y or x<L:
    if D[y][x]=='+':
     if J(D[y]).find('+',x+1)+1:F(y,x+1,1)
     if any((l[x:]+[''])[0]=='+'for l in D[y+1:]):F(y+1,x,2)
    else:D[y][x]='--||'[d];F(y+Q[d],x+Q[d+1],d)
i=0
for l in D:
 I=l.index
 if'+'in l:
    if Z:P,K,L,Z=i,I('+'),J(l).rfind('+'),0
    Y=i
 while'['in l:
    s,e=map(I,'[]');y,x,_,R=T(i,e+1,1,1);W=l[s+1:e]
    if R:D[y][x:x+e-s-1]=W
    else:D[y][x-e+s+2:x+1]=W
    l[s:e+1]=' '*(e-s+1)
 i+=1
F(P,K+1,1);F(P+1,K,2)
for l in D:print J(l)

Strategy:

I turn the input into a matrix. Then I scan it line by line, finding [s. For every [, I find the matching ], then I call the trace function T with the spot immediately to the right of the ]. The trace function follows the line, replacing the path with ' 's as it goes, and returns where the word should go. Then I erase the word in the old location and put the word in the new location.

Finally, I call F, which recursively reconstructs the boxes.

Tests:

reorg_test1... True
Input:
+-------+--------+
| Verbs | Nouns  |
+-------+--------+
|[Truck]--->     |
| eat   | Banana |
|       | Car    |
+-------+--------+


Output:
+-------+--------+
| Verbs | Nouns  |
+-------+--------+
|       |   Truck|
| eat   | Banana |
|       | Car    |
+-------+--------+



reorg_test2... True
Input:
+-----------+----------+
|   Verbs   |  Nouns   |
+-----------+----------+
|           |          |
|[is a noun]->         |
| eat       | Banana   |
|           | Car      |
+-----------+----------+


Output:
+-----------+----------+
|   Verbs   |  Nouns   |
+-----------+----------+
|           |          |
|           | is a noun|
| eat       | Banana   |
|           | Car      |
+-----------+----------+



reorg_test3... True
Input:
    +-----------+----------+
    |   Verbs   |  Nouns   |
    +-----------+----------+
    | [Truck]--\|          |
  /------------/|        <--\
  | | eat       | Banana   ||
  | |  /--\     | Car      ||
  | +--|--|-----+----------+|
  \----/  \-----------------/


Output:
    +-----------+----------+
    |   Verbs   |  Nouns   |
    +-----------+----------+
    |           |          |
    |           |   Truck  | 
    | eat       | Banana   | 
    |           | Car      | 
    +-----------+----------+ 




reorg_test4... True
Input:
   +--------+----------+-------------+
   | Colors |  Foods   |  Countries  |
   +--------+----------+-------------+
   | red    | pizza    | Switzerland |
 /-->     /---->       |             |
 | |[kale]/ | hot dogs | Brazil      |
 | |     <----------------------\    |
 | | orange |[yellow]\ | [green]/    |
 | +--------+--------|-+-------------+
 \-------------------/


Output:
   +--------+----------+-------------+
   | Colors |  Foods   |  Countries  |
   +--------+----------+-------------+
   | red    | pizza    | Switzerland |
   | yellow |   kale   |             |
   |        | hot dogs | Brazil      |
   |green   |          |             |
   | orange |          |             |
   +--------+----------+-------------+




reorg_test5... True
Input:
/------------------------------------------\
|                                          |
|  +------------------+----------+-------+ |
|  | frog             |          |       | |
|  | forge            | dog      | eagle | |
|  | foundation       | diligent | ease  | |
|  |[Brother]--\      | down     | elf   | |
|  |           |      |[egg]--\  |[fort]---/
\---->         |      |       |  |       |  
   |           \-\    |       \----->    |  
   +-------------|----+----------+       |  
   |   Boy       \-->            |       |  
   |   Bull                      |       |  
   |   Browser                   |       |  
   +-----------------------------+-------+ 


Output:


   +------------------+----------+-------+  
   | frog             |          |       |  
   | forge            | dog      | eagle |  
   | foundation       | diligent | ease  |  
   |                  | down     | elf   |  
   |                  |          |       |  
   |  fort            |          |       |  
   |                  |          |   egg |  
   +------------------+----------+       |  
   |   Boy           Brother     |       |  
   |   Bull                      |       |  
   |   Browser                   |       |  
   +-----------------------------+-------+ 



reorg_test6... True
Input:
             /--------------------------------------\
             |                     /---------------\|
/------------|---------------------/               ||
|            |               /---------------\     ||
|  +---------|-+-----------+-|------+--------|+    ||
\---->       | |  clarinet | \>     | [drums]/|    ||
   | [viola]\\--->         |        +---------+    ||
   |        \----->        |        |         |/---/|
   +-----------+ [violin]\ |        | [cello]--/    |
   |        <------------/ |        +---------+     |
   |           +-----------+      <----------\|     |
   |           |           |        |  [tuba]/|     |
   |           | [piano]\  |        |         |     |
   |        <-----------/  |        |         |     |
   |           +-----------+--------+         |     |
   |           |                    |[trumpet]------/
   | [flute]----->                  |         |      
   |           |     saxaphone      |         |      
   +-----------+--------------------+---------+ 


Output:




   +-----------+-----------+--------+---------+      
   |  cello    |  clarinet |   drums|         |      
   |           |  trumpet  |        +---------+      
   |           |   viola   |        |         |      
   +-----------+           |        |         |      
   |  violin   |           |        +---------+      
   |           +-----------+  tuba  |         |      
   |           |           |        |         |      
   |           |           |        |         |      
   |   piano   |           |        |         |      
   |           +-----------+--------+         |      
   |           |                    |         |      
   |           |  flute             |         |      
   |           |     saxaphone      |         |      
   +-----------+--------------------+---------+ 
\$\endgroup\$
1
\$\begingroup\$

Python3, 584 bytes:

E=enumerate
U=lambda b,x,y:0<=x<len(b)and 0<=y<len(b[0])
def f(b):
 q=[(x,y,'','',0,1)for x,r in E(b)for y,l in E(r)if'['==l]
 while q:
  x,y,w,l,X,Y=q.pop(0)
  if(u:=b[x][y]).isalpha():w+=u
  if u=='/':X,Y=[-X,-Y][::-1]
  if u=='\\':X,Y=[X,Y][::-1]
  if u in'><':
   for i,a in E([w,w[::-1]][u=='<']):b[x][y+Y*i]=a
   continue
  q+=[(x+X,y+Y,w,u,X,Y)]
  b[x][y]=' '
  if'-'==u and U(b,x+1,y)and U(b,x-1,y)and' 'not in(Q:={b[x-1][y],b[x+1][y]})and{'|','+'}&Q:b[x][y]='|'
  if'|'==u and U(b,x,y-1)and U(b,x,y+1)and' 'not in(Q:={b[x][y+1],b[x][y-1]})and{'-','+'}&Q:b[x][y]='-'
 return b

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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