15
\$\begingroup\$

Remove one stick and output the stack updated.

Description

You can remove only sticks on top( completely drawn).

You can assume:

  • there are 5 sticks on the table.
  • there's one and only one stick you can remove.
  • sticks are recognizable : there aren't overlapping sticks on the same orientation and there aren't sticks joined which form a longer line.
  • input should be not ambiguous, there should be only one valid solution as pointed out by @Arnauld comment, for example this input is not valid
  • Trailing /leading spaces doesn't matter.

Sticks are 4 chars long and are placed vertically or horizontally.

   |          ————
   |
   |  
   |

Test cases [Before, after]

["      
 |||  
 —|—— 
 |||  
 |||— 
      ",
"      
 | |  
 ———— 
 | |  
 |—|— 
      "],
["       
 |||   
 —|——  
 |||   
 |||—— 
       
       ",
"       
 | |   
 ———— 
 | |   
 |—|—— 
       
       "],
["        
 |||    
 —|——   
 |||    
 |||——— 
        
        ",
"        
 | |    
 ————   
 | |    
 | |——— 
        
        "],
["       
    |  
 ———|  
 |———  
 |  |  
 |———— 
       ",
"       
    |  
 ———|  
 |———  
 |  |  
 |     
       "],
["          
   |      
   |      
   |      
  ————    
  | |     
  | |     
  |———    
    |     
          ",
"          
   |      
   |      
   |      
  ||      
  | |     
  | |     
  |———    
    |     
          "],
["         
         
   |     
  ————   
   ——|—  
   |———  
     |   
         
         
         ",
"         
         
   |     
   | |   
   ——|—  
   |———  
     |   
         
         
         "],
["        
        
 | |    
 ————|  
 | |——— 
 | | |  
     |  
        
        ",
"        
        
 | |    
 | | |  
 | |——— 
 | | |  
     |  
        
        "],
["         
         
 ———|    
  ——|—   
   —|——  
    |——— 
         
         
         ",
"         
         
 ————    
  ————   
   ————  
    ———— 
         
         
         "],
["         
   |     
  —||—   
   ||    
  ————   
  | |    
  |      
  |      
         ",
"         
   |     
  —||—   
   ||    
  |||    
  | |    
  |      
  |      
         "],
["        
        
 |——— | 
 |  | | 
 |  | | 
 ———| | 
        
        ",
"        
        
 |———   
 |  |   
 |  |   
 ———|   
        
        "]

Input specifications
You can add trailing and leading whitespaces / lines as your wish, , I hope this can help focusing on the task by avoiding data bounds checking.

here is a small program to get input data in some formatting. In the header section you can change some characters like array bounds and others.
Data is in the form of a square matrix and the image is surrounded by whitespaces.

Rules

  • Input/output can be given by any convenient method.
  • You can print it to STDOUT, return it as a function result or error message/s.
  • Either a full program or a function are acceptable.
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Sandbox

\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

Charcoal, 102 bytes

WS⊞υιυ≔ΦLυ№§υι×-⁴η≔Σηζ¿η‖T↗«≔Eθ⭆υ§λκυ≔ΣΦLυ№§υι×|⁴ζ»Jζ⌕§υζק-|¬η⁴F⁴↓§ -⊙EErlKDχ✳κ…κ⌕Eκ›μ ⁰∧›⁵Lκ№κ-¿η‖T↗

Try it online! Link is to verbose version of code. Takes input as a list of strings containing spaces, minus signs or pipes. Explanation:

WS⊞υι

Input the game.

υ

Output the game to the canvas.

≔ΦLυ№§υι×-⁴η≔Σηζ

Try to locate a horizontal stick.

¿η

If one was found, then...

‖T↗

... reflect the canvas transforming between minus signs and pipes, ...

«

Otherwise, ...

≔Eθ⭆υ§λκυ

... transpose the game array (sadly without transforming), ...

≔ΣΦLυ№§υι×|⁴ζ

... and find a vertical stick.

»Jζ⌕§υζק-|¬η⁴

Jump to the stick's current location on the canvas.

F⁴↓§ -⊙EErlKDχ✳κ…κ⌕Eκ›μ ⁰∧›⁵Lκ№κ-

Overwrite the pipes of the (possibly transformed) stick with spaces, unless there are at most four consecutive non-blank characters (including the pipe itself) including at least one minus sign on at least one side of the pipe, in which case overwrite with a minus sign instead.

¿η‖T↗

Transform the canvas back if necessary before it's implicitly printed.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Fail it turns into a 5 char long stick \$\endgroup\$
    – l4m2
    Jan 25, 2022 at 2:04
  • \$\begingroup\$ @l4m2 Thanks; I've implemented a completely different algorithm now. \$\endgroup\$
    – Neil
    Jan 25, 2022 at 11:46
  • 1
    \$\begingroup\$ I want try this testcase, but I don't know how to make it work. Should I padding some more spaces around? \$\endgroup\$
    – tsh
    Jan 26, 2022 at 2:22
  • \$\begingroup\$ @tsh Sorry, I got bit by Charcoal behaviour. It's very difficult to use if because the else is implicit (verbose mode lets you drop the else but it's still there really) so if I don't want an else then I use a for loop instead, but then that reallocates the implicit for variables... anyway, should be fixed now. \$\endgroup\$
    – Neil
    Jan 26, 2022 at 8:58

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.