Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We're graduating to a full site soon, and there's only one thing left to do before graduation: pull a senior prank! I think we should do a variation on the classic "fill a hallway with cups of water" gag.

Challenge

Your program will read in text and output that text, covered in upside-down cups of water. An upside-down cup of water looks like this: /~\
These cups can only be placed in whitespace in the input, and can only be placed so that all three characters of the cup are directly above a non-whitespace character (otherwise the water would spill out!). Cups cannot be stacked on top of other cups. Cups must be placed in every available opening, and it is assumed that every input is surrounded by an infinite field of whitespace.

You may assume that the input is padded to a rectangle with spaces.

We need to pull the prank off quickly and without anyone noticing, so fewest bytes in each language wins.

Test Cases

Input:

     ____________________________________________
    /   ___    /   ___    /   ______/   ________/
   /   /__/   /   /__/   /   /     /   /_______
  /   _______/   _______/   /     /   //__    /
 /   /      /   /      /   /_____/   /___/   /
/___/      /___/      /_________/___________/

Output:

     /~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\
     ____________________________________________
    /   ___    /   ___    /   ______/   ________/
   /   /__//~\/   /__//~\/   /     /   /_______
  /   _______/   _______/   //~\  /   //__    /
 //~\/      //~\/      //~\/_____//~\/___//~\/
/___/      /___/      /_________/___________/

Input:

 L
LOL  ROFL:ROFL:LOL:ROFL:ROFL
 L\\        ____I____
    ========    |  |[\
            \___O==___)
            ___I_I__/

Output:

 L   /~\/~\/~\/~\/~\/~\/~\
LOL  ROFL:ROFL:LOL:ROFL:ROFL
 L\\/~\/~\  ____I____
    ========/~\ |  |[\
            \___O==___)
            ___I_I__/
share|improve this question
1  
This might be the time to resurrect some of those 2D pattern matching languages... – mbomb007 Mar 8 at 18:10

Retina, 41 bytes

Byte count assumes ISO 8859-1 encoding.

(?<=(.)*)(?=.*¶(?>(?<-1>.)*)\S{3})   
/~\

Note that the first line has three trailing spaces.

Requires the input to be padded to a rectangle.

Try it online!

Explanation

This is fairly standard vertical matching:

(?<=(.)*)

This counts the characters preceding the match by capturing that many characters into group 1.

(?=.*¶(?>(?<-1>.)*)\S{3})

This checks that there are three non-space characters on at the same position in the next line. This is done by popping from group 1 until its empty with (?<-1>.)* and preventing backtracking with the atomic group (?>...).

Finally we match the actual spaces. Those are simply replaced with the literal string /~\. Conveniently, matches are found from left to right and cannot overlap.

share|improve this answer

JavaScript (ES6), 163 bytes

a=>(z=a.split`
`,z.unshift(z[0].replace(/./g,' ')),z).map((b,i)=>b.replace(/   /g, (c,j)=>(!z[i+1]||!z[i+1][j+2]||/ /.test(z[i+1].slice(j,j+3))?c:'/~\\'))).join`
`

Quickly hacked together solution, can definitely be golfed down. Adds a blank line above, finds triple spaces, and replaces with a cup if the next line does not contain any spaces. Makes the allowed assumption that input will be rectangular. Any backspaces in the input need escaping (as one would expect with JS).

share|improve this answer
    
I think this returns the wrong output for a test. – Neil Mar 11 at 23:54

JavaScript (ES6), 109 bytes

s=>s.replace(/.*\n/,m=>(t=m).replace(/./g,' ')+m).replace(eval(`/   (?=[^]{${t.length-3}}[^ ]{3})/g`),"/~\\")

As well as requiring rectangular input, also assumes the first line ends with a newline, even if it is the only line of input. Uses a dynamically generated lookahead to ensure that it finds three spaces "above" three non-spaces, so as not to get confused by holes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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