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 code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
1 Answer 1
Charcoal, 102 bytes
×ばつ§-|¬η4F4↓§ -⊙EErlKDχ✳κ...κ⌕Eκ›μ 0∧›5LκNoκ-¿η‖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.
×ばつ-4η≔Σηζ
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), ...
×ばつ|4ζ
... and find a vertical stick.
×ばつ§-|¬η4
Jump to the stick's current location on the canvas.
F4↓§ -⊙EErlKDχ✳κ...κ⌕Eκ›μ 0∧›5LκNoκ-
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.
-
2
-
\$\begingroup\$ @l4m2 Thanks; I've implemented a completely different algorithm now. \$\endgroup\$Neil– Neil2022年01月25日 11:46:58 +00:00Commented Jan 25, 2022 at 11:46
-
1
-
\$\begingroup\$ @tsh Sorry, I got bit by Charcoal behaviour. It's very difficult to use
if
because theelse
is implicit (verbose mode lets you drop the else but it's still there really) so if I don't want anelse
then I use afor
loop instead, but then that reallocates the implicitfor
variables... anyway, should be fixed now. \$\endgroup\$Neil– Neil2022年01月26日 08:58:31 +00:00Commented Jan 26, 2022 at 8:58