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