# PPCG Senior Prank

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__/


## 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.

# 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).

• I think this returns the wrong output for a test. – Neil Mar 11 '16 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.