[Question]
  [
Given the topography of land in ASCII picture format, figure out where lakes would go and fill them in. Assume an infinite amount of rain.
## example
**input**
```
         #               
         ##              
      # ####             
#    #########           
##  ###########          
## #############   ####  
## ##############  ##### 
################# #######
#########################
#########################
```
**output**
```
         #               
         ##              
      #@####             
#@@@@#########           
##@@###########          
##@#############@@@####  
##@##############@@##### 
#################@#######
#########################
#########################
```
The input will contain only spaces and `#` marks. Each line will be the same length. The output should be the identical `#` pattern with spaces where water would accumulate filled in with `@` marks.
The bottom input row will always be all # marks. There will be no holes or overhangs in the land. Shortest code wins.
  
[Answer]
## sed -r, 27 24 (27 with `-r`)
24 (27):
```
:;s/(#|@) ( *#)/\1@\2/;t
```
27 (30):
```
:e;s/([#@]) ( *#)/\1@\2/;te
```
Competes with the better of the two perl solutions
[Answer]
## Perl, 25
```
s/# +#/$_=$&;y| |@|;$_/ge
```
[Answer]
## Perl (>= v5.9.5), 24 chars
Run with `perl -p`:
```
1while s/#.*\K (?=\S)/@/
```
[This requires Perl 5.9.5 or later to use the special escape `\K`.](http://perldoc.perl.org/perl595delta.html#%5cK-escape)
[Answer]
## Windows PowerShell, 36 ~~74~~ ~~138~~
```
$input-replace'(?<!^ *) (?! *$)','@'
```
[Answer]
## [Retina](https://github.com/mbuettner/retina/), 10 bytes
Retina is (much) newer than this challenge. But this solution is too neat not to post it:
```
T` `@`#.*#
```
[Try it online.](http://retina.tryitonline.net/#code=VGAgYEBgIy4qIw&input=ICAgICAgICAgIyAgICAgICAgICAgICAgIAogICAgICAgICAjIyAgICAgICAgICAgICAgCiAgICAgICMgIyMjIyAgICAgICAgICAgICAKIyAgICAjIyMjIyMjIyMgICAgICAgICAgIAojIyAgIyMjIyMjIyMjIyMgICAgICAgICAgCiMjICMjIyMjIyMjIyMjIyMgICAjIyMjICAKIyMgIyMjIyMjIyMjIyMjIyMgICMjIyMjIAojIyMjIyMjIyMjIyMjIyMjIyAjIyMjIyMjCiMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMKIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIw)
This is simply a transliteration stage which replaces spaces with `@`, but the operation is restricted to matches of `#.*#`, i.e. characters which are surrounded by land on both sides.
[Answer]
## Ruby 1.8, 30 characters
```
#!ruby -p
gsub(/# +#/){$&.tr" ","@"}
```
If anyone has an idea why this doesn't work in Ruby 1.9 (tested with 1.9.2p0 and 1.9.2p204), even though the documentation says [it should work](http://ruby-doc.org/core/classes/Kernel.html#M001425), let me know!
[Answer]
# Python, ~~95~~ 92 bytes
```
for s in S.split('\n'):b=s.find('#');e=s.rfind('#');print s[:b]+s[b:e].replace(' ','@')+s[e:]
```
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 15 bytes
```
aR:`#.*#`_TRs'@
```
Takes input as a multiline string via command-line argument: [Try it online!](https://tio.run/##K8gs@P8/McgqQVlPSzkhPiSoWN3h////CjCgrIAKuBAyylhllIESaFJcykgyKHJcIJ4yAqDKKCujSkEoDBmYESAZNABTiCmjTFAGAA "Pip – Try It Online") (Alternately, specify the `-rn` flags and change the first `a` to `g`, and you can give input via stdin: [Try it online!](https://tio.run/##K8gs@P8/PcgqQVlPSzkhPiSoWN3h/38FGFBWQAVcCBllrDLKQAk0KS5liHooQJFRRpJAlgLJKCujSkEoDBmYESAZNABTiCmjTFDmv25RHgA "Pip – Try It Online"))
Same idea as the Retina answer: replace every match of the regex `#.*#` with the result of transliterating space to `@` in the match. Pip can't match Retina's terseness for a pure regex problem--but it's not everyday that you can tie with Jelly, after all.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~17~~ 16 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
|εγć?D¨ð'@:sθJJ,
```
[Try it online.](https://tio.run/##MzBNTDJM/f@/5tzWc5uPtNu7HFpxeIO6g1XxuR1eXjr//yvAgLICKuBCyChjlVEGSqBJcYFNUYYBZBkQT1kZixRIRlkZVQpCYcjAjADJoAGYQkwZZYIyAA)
**Explanation:**
```
|            # Take all input-lines as list
 ε           # For each line:
  γ          #  Split the line into chunks of consecutive equal characters
             #   i.e. " ##   # " → [' ','##','   ','#',' ']
   ć         #  Split into head and the rest of the list
             #   i.e. [' ','##','   ','#',' '] → ['##','   ','#',' '] and ' '
    ?        #  Print this head
   D         #  Duplicate the rest of the list
    ¨        #  Remove the last element
             #   i.e. ['##','   ','#',' '] → ['##','   ','#']
     ð'@:    #  Replace every space with a "@"
             #   i.e. ['##','   ','#'] → ['##','@@@','#']
     sθ      #  Swap so the duplicated list is at the top, and take the last item as is
             #   i.e. ['##','   ','#',' '] → ' '
         JJ  #  Join the lists and individual items in the list together to a single string
             #   i.e. ['##','@@@','#'] and ' ' → "##@@@# "
           , #  Print with trailing new-line
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 20 bytes
```
□⟑Ġvṅḣ$₴:Ṫ\@Ḟ$¤ptJṅ,
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLilqHin5HEoHbhuYXhuKMk4oK0OuG5qlxcQOG4niTCpHB0SuG5hSwiLCIiLCIgICAgICAgICAjICAgICAgICAgICAgICAgXG4gICAgICAgICAjIyAgICAgICAgICAgICAgXG4gICAgICAjICMjIyMgICAgICAgICAgICAgXG4gIyAgICMjIyMjIyMjIyAgICAgICAgICAgXG4jIyAgIyMjIyMjIyMjIyMgICAgICAgICAgXG4jIyAjIyMjIyMjIyMjIyMjICAgIyMjIyAgXG4jIyAjIyMjIyMjIyMjIyMjIyAgIyMjIyMgXG4jIyMjIyMjIyMjIyMjIyMjIyAjIyMjIyMjXG4jIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjXG4jIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIl0=)
## How?
```
□⟑Ġvṅḣ$₴:Ṫ\@Ḟ$¤ptJṅ,
□                    # All inputs wrapped in a list
 ⟑                   # Open an apply lambda, apply for each:
  Ġvṅ                # Group consecutive identical characters
     ḣ               # Head extract, push a[0], a[1:]
      $              # Swap
       ₴             # Print without a trailing newline
        :Ṫ           # Duplicate and remove the last item
          \@Ḟ        # Replace all spaces with "@"
             $       # Swap
              ¤p     # Prepend an empty string for the case that the list is empty
                t    # Last item
                 J   # Join top two things on the stack together
                  ṅ  # Join by nothing
                   , # Print with trailing newline
```
[Answer]
# Javascript (ES6), 44 bytes
```
x=>x.replace(/# +#/g,s=>s.replace(/ /g,"@"))
```
[Answer]
# Javascript, 107 bytes
```
var f=function(x){return x.replace(/# +#/g, function(x){return "#"+new Array(x.length-1).join("@")+"#";})};
```
Ungolfed:
```
var f = function(x) {
    return x.replace(/# +#/g, function(x){
        return "#" + new Array(x.length - 1).join("@") + "#";
    })
};
```
[Answer]
# Python, 108 106 92 bytes
```
import re
n=1
while n: S,n=re.subn('# +#',lambda m:'#'+'@'*len(m.group(0)[2:])+'#',S)
print S
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 15 bytes
```
Ỵ”@ẹ”#ṂrṀḟƲ$¦€Y
```
[Try it online!](https://tio.run/##y0rNyan8///h7i2PGuY6PNy1E0gpP9zZVPRwZ8PDHfOPbVI5tOxR05rI/w93dD3cOe1w@///6urqXAowoKyACpBklLHKKAMl0KS4lCHqoQBFRhlJAlkKJKOsjCoFoTBkYEaAZNAATCGmjDJhGWA4AAA "Jelly – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 28 bytes
```
'# +#'⎕R{'@'@(' '∘=)⍵.Match}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///X11ZQVtZ/VHf1KBqdQd1Bw11BfVHHTNsNR/1btXzTSxJzqj9n/aobcKj3r5HXc2Petc86t1yaL3xo7aJQC3BQc5AMsTDM/h/mgKQ5RXs76egrqQAA8oKqCAmDyGljF1KGSiDJheTpwzRAQWoUspIMshyYCllZVQ5CIUpBTMELIUGYCqxSCkTllJSBwA "APL (Dyalog Unicode) – Try It Online")
]