19
\$\begingroup\$

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:

color coded thingy animated thingy

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 by r rows, where c ≥ 1 and r ≥ 2.
  • Each strand will...
    • start at the last row of the head (row r - 1).
    • have a length l where l ≥ 2.
  • Strands may be parsed with the following method:
    1. 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.)
    2. 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.
    3. 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.

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 , so the shortest code in bytes will win!

\$\endgroup\$
7
  • \$\begingroup\$ Are there atoms '-' in the bottom row? Point 2 seems to say yes, point 1 seems to say no. \$\endgroup\$
    – edc65
    Oct 19, 2014 at 12:55
  • \$\begingroup\$ @edc65 No, there are not. (Edited to clarify) \$\endgroup\$
    – Doorknob
    Oct 19, 2014 at 12:58
  • \$\begingroup\$ I think there is a mistake in the test case. On the fourth row from the bottom, the - in the third strand from the left points at the | in the fourth strand from the left \$\endgroup\$
    – feersum
    Oct 19, 2014 at 21:49
  • \$\begingroup\$ @feersum Correct. The path is determined by what points to the -, 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\$
    – Doorknob
    Oct 19, 2014 at 22:41
  • \$\begingroup\$ I know it is determined by later pieces pointing to earlier ones. My argument is that the - should be joined to the fourth strand, not the third. \$\endgroup\$
    – feersum
    Oct 19, 2014 at 22:42

1 Answer 1

12
\$\begingroup\$

JavaScript (E6) 195 212 222 232

Using a recursive function R to find the path of each strand and marking the positions starting from 1 for the farest from the root. Then it's easy, in a second pass, to substitute the lower marked position with blanks.

F=(h,t,
   n=[...h],c=h.search('\n'),
   R=p=>[1,-1,c+2,c+1,c].some((d,i)=>n[p-d]=='--\\|/'[i]?n[p-=d]=1:0)&&R(p)+(R[p]=l++)
  )=>
  n.map((v,p)=>R[p]<t?' ':h[p],n.map((a,p)=>!h[p+c]&a>' '&&R(p,l=0))).join('')

Test in FireFox/FireBug console

head = "\\         /-- -|\n /   -\\  |  |  |\n\\      \\ \\  \\  |\n --\\    | \\  | |\n    / |--| / / |\n /|-  //--| /  |\n| |  ||   //\\  |\n| \\  /|  //  \\ |";
console.log(F(head,0))
console.log(F(head,1))
console.log(F(head,3))
console.log(F(head,7))
console.log(F(head, 1337))

Output

\         /-- -|
 /   -\  |  |  |
\      \ \  \  |
 --\    | \  | |
    / |--| / / |
 /|-  //--| /  |
| |  ||   //\  |
| \  /|  //  \ |

          /-   |
 /    \  |     |
\      \ \  \  |
 --\    | \  | |
    / |--  / / |
  |-  //--| /  |
| |  ||   //   |
| \  /|  //  \ |


         |      
         \     |
 --\    | \    |
    / |--  / / |
  |-  //  | /  |
  |  ||   //   |
| \  /|  //  \ |






  |-  /   |     
  |  |    /    |
| \  /|  //  \ |








| \  /|  //  \ |    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.