[Question]
  [
Input:
```
1
      X                                
      X                                
      X                                
      X      XX    XXXXXX     X X X    
      X      XX    XXXXXX     X X X    
XXX   X      XX    XXXXXX     X X X    
XXX   X      XX    XXXXXX     X X X    
```
Output:
```
      X.                               
      X..                              
      X...                             
      X....  XX.   XXXXXX.    X.X.X.   
      X..... XX..  XXXXXX..   X.X.X..  
XXX.  X......XX... XXXXXX...  X.X.X... 
XXX.. X......XX....XXXXXX.... X.X.X....
```
Input:
```
2
         XX
         XX
         XX
         XX
         XX
     XX  XX
     XX  XX
     XX  XX
     XX  XX
```
Output:
```
        .XX
       ..XX
      ...XX
     ....XX
    .....XX
   ..XX..XX
  ...XX..XX
 ....XX..XX
.....XX..XX
```
Specification:
* You must take as input
	1. A flag signifying whether the light is coming from the top left or top right. This can be `1` or `2`, `-1` or `1`, `0` or `65536`, or whatever is convenient for you, as long as both flags are integers.
	2. Rows composed of either `X` or , all of the same length in characters (i.e. padded with )
		+ All `X`s will either be on the last row or have an `X` under them (meaning no floating buildings)
* You must output the rows (buildings) with shadows added. This is done with the following procedure:
	+ If the light is coming from the top left, draw a right triangle of `.`s with the same height and width as the height of the building, starting from one space past its right edge and going rightwards.
	+ Otherwise, if it's from the top right, do the same thing but start from one space past its left edge and pointing left.
	+ Remember, do not alter `X`s by changing them to `.`s; leave them as they are.
	+ There will always be "room" for your shadows, i.e. if there's a 3-space tall building at the end there will be at least 3 spaces of padding after it.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes will win!
  
[Answer]
# Perl - 85
```
BEGIN{$d=-<>}$d?s/X /X./g:s/ X/.X/g;s/ /substr($p,$+[0]+$d,1)eq'.'?'.':$&/ge;$p=$_;
```
EDIT: I totally forgot about the `-p` flag this needs to be run with. Added 2 to char count.  
The flag specified at the first line is `0` for shadows going left and `2` for shadows going right.
[Answer]
### GolfScript, 67 characters
```
n%(~:S\zip\%.0=\{.' '3$);+{{\(@[\].~<=}%+}:M~'X'/'.'*@@M}%S%zip\;n*
```
1/-1 for shadows going right/left. Run the example [online](http://golfscript.apphb.com/?c=OyIxCiAgICAgIFggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICBYICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgWCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgIFggICAgICBYWCAgICBYWFhYWFggICAgIFggWCBYICAgIAogICAgICBYICAgICAgWFggICAgWFhYWFhYICAgICBYIFggWCAgICAKWFhYICAgWCAgICAgIFhYICAgIFhYWFhYWCAgICAgWCBYIFggICAgClhYWCAgIFggICAgICBYWCAgICBYWFhYWFggICAgIFggWCBYICAgICIKCm4lKH46U1x6aXBcJS4wPVx7LicgJzMkKTsre3tcKEBbXF0ufjw9fSUrfTpNfidYJy8nLicqQEBNfSVTJXppcFw7bioK&run=true):
```
      X.                               
      X..                              
      X...                             
      X....  XX.   XXXXXX.    X.X.X.   
      X..... XX..  XXXXXX..   X.X.X..  
XXX.  X......XX... XXXXXX...  X.X.X... 
XXX.. X......XX....XXXXXX.... X.X.X....
```
[Answer]
# Python 3 - 233
Well, that turned out longer than expected...
1 for shadows going right, -1 for shadows going left.
```
d,x=int(input()),[1]
while x[-1]:x+=[input()]
x,o,l,h=list(zip(*x[1:-1]))[::d],[],0,len(x)-1
for i in x:o+=[''.join(i[:len(i)-l])+''.join(i[len(i)-l:]).replace(' ','.')];l=max(l-1,i.count('X'))
for i in zip(*o[::d]):print(''.join(i))
```
EDIT: Didn't see the either side padding in the rules. Ehehe. ^^'
[Answer]
# JavaScript - 14
```
eval(prompt())
```
The flag on the first line is `for(p='';l=prompt();)console.log(p=l.replace(/ /g,function(a,b){return p[b+1]=='.'||p[b]=='.'||l[b+1]=='X'?'.':a}));` for shadows facing the left or `for(p='';l=prompt();)console.log(p=l.replace(/ /g,function(a,b){return p[b-1]=='.'||p[b]=='.'||l[b-1]=='X'?'.':a}));` for shadows to the right.
This *might* abuse the "whatever is convenient for you" rule for the flag :P
---
**Edit:** without abuse (127):
```
c=prompt();for(p='';l=prompt();)console.log(p=l.replace(/ /g,function(a,b){return p[b+c]=='.'||p[b]=='.'||l[b+c]=='X'?'.':a}));
```
The flag for this is `1` or `-1`
[Answer]
## Python 2.7 - 229
```
p,s,M,J,L=input(),__import__('sys').stdin.readlines(),map,''.join,len
n,s,r,f=L(s),M(str.strip,M(J,zip(*s[::-1]))),0,[]
for l in s[::p]:f,r=f+[(l+'.'*(r-L(l))+' '*n)[:n]],max(r-1,L(l))
print'\n'.join(M(J,zip(*f[::p])[::-1]))
```
**Ungolfed Version**
```
def shadow(st, pos):
    _len = len(st)
    st = map(str.strip, map(''.join,zip(*st[::-1])))
    prev = 0
    res = []
    for line in st[::[1,-1][pos-1]]:
        res +=[(line+'.'*(prev-len(line)) + ' '*_len)[:_len]]
        prev = max(prev - 1, len(line))
    return '\n'.join(map(''.join,zip(*res[::[1,-1][pos-1]])[::-1]))
```
]