This is my head:
\ /-- -|
/ -\ | | |
\ \ \ \ |
--\ | \ | |
/ |--| / / |
/|- //--| / |
| | || //\ |
| \ /| // \ |
It consists of exactly eight hairs. My hair is too long. Please cut each individual strand to a length I specify.
Input
The main attraction in this section is the actual head of hair. Here's a graphical, color-coded representation, along with an animation, for the lazy:
And here is a full specification for what a set of hairs is:
- The individual hairs, which we will call strands, will be made out of the
/
,\
,|
, and-
ASCII characters, henceforth known as atoms. - The entire head of hear (all of the strands combined) will be
c
columns byr
rows, wherec
≥ 1 andr
≥ 2. - Each strand will...
- start at the last row of the head (row
r
- 1). - have a length
l
wherel
≥ 2.
- start at the last row of the head (row
- Strands may be parsed with the following method:
- Start at the bottom of the strand. This will be a
/
,|
, or\
atom, which we will call the root. (Strands are parsed from left to right, ordered by the root.) - Find the atom that points towards the root.
- A
|
atom points up and down. A-
atom points left and right (but can never point to the root since only roots can be in the bottom row). A/
atom points left-down and up-right, and a\
atom does the opposite. - There will always be exactly one atom that points towards the root atom.
- A
- Find an unused atom (one that is not yet part of any strand) that points towards this atom and is also not below this atom (hair can't grow downwards, but sideways is okay).
- If there are zero, you have reached the end of the strand!
- If there is one, this is the next atom of the strand. Repeat step 3 with this atom. (This atom is now marked as "used" for the purposes of step 3, since it's part of a strand.)
- There will never be multiple unused atoms here at any point in the head.
- Start at the bottom of the strand. This will be a
The input format will be:
- A head of hair. Can be input as-is (multiline input with literal newlines), or with a delimiter of your choice in place of newlines. Note that the head will always be a rectangle; i.e. trailing whitespace will be added as necessary (the weird straight line hair at the right is just to eliminate extra trailing whitespace to avoid confusion).
- A number ≥ 1 specifying how much hair to cut. This will be described in detail in the next section.
You may accept input to a function, use STDIN/STDOUT, etc. (anything reasonable).
Output
Your output will be the head of hair with the haircut applied. To give a haircut, simply reduce the length of each strand of hair by the amount specified (which will always be 1 or greater), starting from the endpoint of the hair moving towards the root. However, always leave the root intact!
Here's a simple example. Say the input for the head is
\
/
|
|
With a second input of 2
, you would cut two atoms from the strand and get a result of
|
|
And with an input of 42
, the output would just be
|
Note that whitespace before and after the head is completely irrelevant; you can trim extra newlines or you could add more padding. It doesn't matter, as long as the head remains intact. (You may do whatever you want with trailing whitespace as well.)
Test Cases
For all test cases, the example presented at the very beginning of this post will be used.
Haircut length = 1:
/- |
/ \ | |
\ \ \ \ |
--\ | \ | |
/ |-- / / |
|- //--| / |
| | || // |
| \ /| // \ |
Length of 3:
|
\ |
--\ | \ |
/ |-- / / |
|- // | / |
| || // |
| \ /| // \ |
Length of 7:
|- / |
| | / |
| \ /| // \ |
Length of 1337:
| \ /| // \ |
Scoring
This is code-golf, so the shortest code in bytes will win!
-
in the third strand from the left points at the|
in the fourth strand from the left \$\endgroup\$-
, not what it points to. For example, the|
doesn't point to the/
in the first strand, but it is pointed to by the/
. (Yes, I know it's confusing; I had to redo the GIF several times because I confused even myself!) An easier way to understand this might be just to look at the top of the second strand. \$\endgroup\$-
should be joined to the fourth strand, not the third. \$\endgroup\$