fork download
  1. E=enumerate
  2. U=lambda b,x,y:len(b)>x>-1<y<len(b[0])
  3. def f(b):
  4. q=[(x,y,'','',0,1)for x,r in E(b)for y,l in E(r)if'['==l]
  5. while q:
  6. x,y,w,l,X,Y=q.pop(0)
  7. if(u:=b[x][y]).isalpha():w+=u
  8. if'/'==u:X,Y=[-X,-Y][::-1]
  9. if'\\'==u:X,Y=[X,Y][::-1]
  10. if u in'><':
  11. for i,a in E([w[::-1],w][u>'<']):b[x][y+Y*i]=a
  12. else:q+=(x+X,y+Y,w,u,X,Y),;b[x][y]='|'if('-'==u)*U(b,x+1,y)and U(b,x-1,y)>(' 'in(Q:={b[x-1][y],b[x+1][y]}))and{'|','+'}&Q else'-'if('|'==u)*U(b,x,y-1)and U(b,x,y+1)>(' 'in(Q:={b[x][y+1],b[x][y-1]}))and{'-','+'}&Q else' '
  13. return b
  14.  
  15. s1 = """
  16. +-------+--------+
  17. | Verbs | Nouns |
  18. +-------+--------+
  19. |[Truck]---> |
  20. | eat | Banana |
  21. | | Car |
  22. +-------+--------+
  23. """
  24. s2 = """
  25. +-----------+----------+
  26. | Verbs | Nouns |
  27. +-----------+----------+
  28. | | |
  29. |[is a noun]-> |
  30. | eat | Banana |
  31. | | Car |
  32. +-----------+----------+
  33. """
  34. s3 = """
  35. +-----------+----------+
  36. | Verbs | Nouns |
  37. +-----------+----------+
  38. | [Truck]--\| |
  39. /------------/| <--\
  40. | | eat | Banana ||
  41. | | /--\ | Car ||
  42. | +--|--|-----+----------+|
  43. \----/ \-----------------/
  44. """
  45. s4 = """
  46. +--------+----------+-------------+
  47. | Colors | Foods | Countries |
  48. +--------+----------+-------------+
  49. | red | pizza | Switzerland |
  50. /--> /----> | |
  51. | |[kale]/ | hot dogs | Brazil |
  52. | | <----------------------\ |
  53. | | orange |[yellow]\ | [green]/ |
  54. | +--------+--------|-+-------------+
  55. \-------------------/
  56. """
  57. s5 = """
  58. /------------------------------------------\
  59. | |
  60. | +------------------+----------+-------+ |
  61. | | frog | | | |
  62. | | forge | dog | eagle | |
  63. | | foundation | diligent | ease | |
  64. | |[Brother]--\ | down | elf | |
  65. | | | |[egg]--\ |[fort]---/
  66. \----> | | | | |
  67. | \-\ | \-----> |
  68. +-------------|----+----------+ |
  69. | Boy \--> | |
  70. | Bull | |
  71. | Browser | |
  72. +-----------------------------+-------+
  73. """
  74. s6 = """
  75. /--------------------------------------\
  76. | /---------------\|
  77. /------------|---------------------/ ||
  78. | | /---------------\ ||
  79. | +---------|-+-----------+-|------+--------|+ ||
  80. \----> | | clarinet | \> | [drums]/| ||
  81. | [viola]\\\\---> | +---------+ ||
  82. | \-----> | | |/---/|
  83. +-----------+ [violin]\ | | [cello]--/ |
  84. | <------------/ | +---------+ |
  85. | +-----------+ <----------\| |
  86. | | | | [tuba]/| |
  87. | | [piano]\ | | | |
  88. | <-----------/ | | | |
  89. | +-----------+--------+ | |
  90. | | |[trumpet]------/
  91. | [flute]-----> | |
  92. | | saxaphone | |
  93. +-----------+--------------------+---------+
  94. """
  95. def to_board(s):
  96. return [[*i] for i in filter(None, s.split('\n'))]
  97.  
  98. def solution(b):
  99. for i in f(to_board(b)):
  100. print(''.join(i))
  101.  
  102. solution(s1)
  103. solution(s2)
  104. solution(s3)
  105. solution(s4)
  106. solution(s5)
  107. solution(s6)
Success #stdin #stdout 0.1s 14140KB
stdin
Standard input is empty
stdout
+-------+--------+
| Verbs | Nouns  |
+-------+--------+
|       |  Truck |
| eat   | Banana |
|       | Car    |
+-------+--------+
+-----------+----------+
|   Verbs   |  Nouns   |
+-----------+----------+
|           |          |
|           |isanoun   |
| eat       | Banana   |
|           | Car      |
+-----------+----------+
    +-----------+----------+  
    |   Verbs   |  Nouns   |  
    +-----------+----------+  
    |           |          |  
    |           |    Truck |  
    | eat       | Banana   |  
    |           | Car      |  
    +-----------+----------+  
                              
   +--------+----------+-------------+
   | Colors |  Foods   |  Countries  |
   +--------+----------+-------------+
   | red    | pizza    | Switzerland |
   |yellow  |  kale    |             |
   |        | hot dogs | Brazil      |
   | green  |          |             |
   | orange |          |             |
   +--------+----------+-------------+
                                      
                                             
                                             
   +------------------+----------+-------+   
   | frog             |          |       |   
   | forge            | dog      | eagle |   
   | foundation       | diligent | ease  |   
   |                  | down     | elf   |   
   |                  |          |       |   
   | fort             |          |       |   
   |                  |          |  egg  |   
   +------------------+----------+       |   
   |   Boy          Brother      |       |   
   |   Bull                      |       |   
   |   Browser                   |       |   
   +-----------------------------+-------+   
                                                        
                                                        
                                                       
                                                         
   +-----------+-----------+--------+---------+         
   | cello     |  clarinet |  drums |         |         
   |           | trumpet   |        +---------+         
   |           |  viola    |        |         |         
   +-----------+           |        |         |         
   |   violin  |           |        +---------+       
   |           +-----------+   tuba |         |         
   |           |           |        |         |         
   |           |           |        |         |           
   |    piano  |           |        |         |          
   |           +-----------+--------+         |             
   |           |                    |         |        
   |           | flute              |         |         
   |           |     saxaphone      |         |       
   +-----------+--------------------+---------+