text:
ASCII ART edge detection
As the title says,  I was thinking to contest in which one must detect edges of an ASCII art.
The code should accept a B/W ASCII art as input.
A B/W ASCII art is defined as (by me) an ASCII art with only one kind of non-white-spaces character (in our case: an asteriks *). And as output produce a standard ASCII art (all ASCII characters are accepted) which should remember the contourn of the first.
The purpose of using more than one character in the output is to make some edges ssmoother.
For instance, one could let this input
     ***
   ****
 ******
******
******
 ******
   ****
     ***

could became:
      ___
    _/   )
  _/    /
 /      |
|      /
|      \
 \      |
  `\     |
     \___)

The input \n separated string as input. Each line has a maximum of 80 characters. The number of rows is not specified.
This is my sample Python3 program:
import fileinput as f
import re as r
import copy as c
a,s,p='*',' ','+'
def read(n):
    s=[list(' '*n)]
    for l in f.input():
        if(len(l)>n):l=l[:n]
        k=list(r.sub('[^ ^\%c]'%a,'',' '+l+' '))
        s.append(k+[' ']*(n-len(k)))
    s.append([' ']*n)
    return s
def np(s):
    s=c.deepcopy(s)
    for l in s[1:-1]:
        for w in l[1:-1]: print(w,end='')
        print()
def grow(i):
    o=c.deepcopy(i)
    for x in range(1,len(o)-1):
        for y in range(1,len(o[x])-1):
            if(i[x][y]==a): o[x-1][y-1]=o[x-1][y+1]=o[x-1][y]=o[x+1][y]=o[x+1][y-1]=o[x+1][y+1]=o[x][y+1]=o[x][y-1]=a

    return o
def diff(i,o):
    c=[]
    for x in range(0,len(i)):
        l=[]
        for y in range(0,len(i[x])):
            if(i[x][y]==a and o[x][y]==s): l.append(p)
            else: l.append(s)
        c.append(l)
    return c
I=read(80)
np(diff(grow(I),I))

Input:
Here below I put both input of the programs. It is an 80x70 ASCII ART. It means it has 70 lines of 80 characters, each separated by \n.
This input file contains only spaces and the asterisk *, it has a maximum of 80 columns as every ASCII art.
If you find a nicer art let me know!
                                              *************
                                          *****          *****
                                     ******                  ***
                                    ***                         ****
                             *********                             **
                          ***********                               **
                     ******   *******                                **
                 *****       *******      ***                         **
              ****          ********     *****                          *
             **            *********     *****                    *****  *
           ***            *********     *******                  ******  **
          **             **********     *******                  ******   **
         **              **********    *******                  ********   *
        *               ***********   ******                    ********   *
       **              ************   *****                     ********    *
       *               ************    ***                       ********   *
      *               *************                               ******    *
     *                *************                                 ***     *
    **                *************                                         *
    *                **************                                         *
   **                *************                                         **
   *                 *************                                         **
  **                *************                                          ***
 ***                *************                                          ****
 **                 ************                                           ****
 **                *************                                           ****
 **                *************           *****                           ****
 **                *************          **   **          **              ****
 **                 ************          *     *         ** **            ****
 *                  ************          **   **        **   **           ****
 *                  *************        *******         **   ***          ****
 *                  ************          *****           *******          ****
 *                   ************         ***               *****          ****
**     *             *************                          ****          *****
**    ***            **************                                      *****
*    *****            *************                                     ******
** *******             **************                                  *******
**********             ***************              *                *********
**********              *****************          ***             ***********
***********              *******************                    **************
***********               **********************            ******************
************              *****************     **     ***********************
*************             ******************      ****     *******************
**************            ******************              ********************
****************           ******************              *******************
***************           *******************              *******************
****************           ******************              ******************
******************         ******************             *******************
*******************         *****************             *******************
*********************      ******************           ********************
*********************************************          *********************
**********************************************       ***********************
************************     *****************      ************************
 **********************       ******************* **************************
 *********************        *********************************************
 *********************        ****************************  ***************
 ********************         **************************    ***************
 ********************         *********************         ***************
 *******************          ********************         ****************
 ******************           *****************            ****************
 *****************             ****************            ***************
 *****************             ****************            ***************
 *****************             *****************           ***************
  ****************             *****************           ***************
   **************              ******************          ***************
                                 ****************          ****************
                                  **************            ***************
                                                             **************
                                                              ************

Possible output:
A possible output could be:
                                         +++++             ++++
                                    ++++++     ++++++++++     +++
                                   ++      +++++        +++++   +++++
                            ++++++++   +++++                ++++    ++
                         ++++         ++                       ++++  ++
                    ++++++           ++                           ++  ++
                +++++      +++       +   +++++                     ++  ++
             ++++     +++++++       ++  ++   ++                     ++  ++
            ++    +++++   ++        +   +     +                  +++++++ ++
          +++  ++++      ++         +  ++     ++                ++     ++ ++
         ++   ++        ++         ++  +       +                +      ++  ++
        ++  +++         +          +  ++       +               ++      +++  +
       ++  ++          ++          + ++       ++               +        +++ +
      ++ +++          ++           + +      +++                +        + + ++
      +  +            +            + +     ++                  +        ++++ +
     ++ ++           ++            + ++   ++                   ++        + + +
    ++ ++            +             +  +++++                     ++      ++ + +
   ++ ++             +             +                             +++   ++  + +
   +  +             ++             +                               +++++   + +
  ++ ++             +              +                                      ++ +
  +  +              +             ++                                      +  +
 ++ ++             ++             +                                       +  ++
++  +              +             ++                                       +   ++
+   +              +             +                                        +    +
+  ++             ++            ++                                        +    +
+  +              +             +         +++++++                         +    +
+  +              +             +        ++     ++        ++++            +    +
+  +              +             +        +  +++  +       ++  +++          +    +
+  +              ++            +        + ++ ++ +      ++  +  ++         +    +
+ ++               +            ++      ++  +++  +      +  +++  ++        +    +
+ +                +             +      +       ++      +  +++   +        +    +
+ +                +            ++      ++     ++       ++       +        +    +
+ +   +++          ++            ++      +   +++         +++     +       ++    +
  +  ++ ++          +             ++     +++++             +    ++      ++     +
  + ++   ++         +              +                       ++++++      ++     ++
 ++++     +         ++             +++                                ++      +
  +       +          ++              ++            +++              +++       +
          +           +               ++++        ++ ++           +++         +
          ++          ++                 ++++     +   +        ++++           +
           +           ++                   +++++ +++++    +++++              +
           ++           ++                      +++   ++++++                  +
            ++           +                 +++++  +++++                       +
             ++          +                  +  +++    +++++                   +
              +++        +                  ++   ++++++  +                    +
                +        ++                  +           ++                   +
               ++        +                   +            +                   +
                +++      ++                  +           ++                  ++
                  ++      +                  +           +                   +
                   +++    ++                 +         +++                   +
                     ++++++                  +        ++                    ++
                                             ++     +++                     +
                                              +    ++                       +
                        +++++                 ++++++                        +
+                      ++   ++                   +                          +
+                     ++     +                                             ++
+                     +      +                            ++               +
+                    ++      +                          ++++               +
+                    +       +                     ++++++ ++               +
+                   ++       +                    ++      +                +
+                  ++        +                 ++++       +                +
+                 ++         ++                +          +               ++
+                 +           +                ++         +               +
+                 +           +                 +         +               +
++                +           +                 ++        +               +
 ++              ++           +                  +        +               ++
  ++++++++++++++++            +++                +        +                +
                                ++              ++        ++               +
                                 ++++++++++++++++          ++              +
                                                            ++            ++
                                                             ++++++++++++++

This is also the output produced by the script above. Of course it is not the best output and I'm sure  one can easily produce a smoother one.
It is a popularity-contest since, I hope votes will be proportional to elegance and quality of outputs! (Also I guess good results acan be obtained combinig aplay and gimp-cli)
There are no strict rules on how the output should be..just use your fantasy!

Please define the input that will be used in all the answers to compare them. The panda is too large IMHO.

Does the length of each line of the input need to be constant? The first line of the panda is shorter than the other lines (spaces are missing at the end).

Does the input file contains only one character and spaces?

Thanks for your questions, I don't think the panda is too large: it has 80 column as every ASCII art. I didn't said it, but the max of columns should be 80. Yes the input file contains only the asterisks and the space. Thanks again, I added these info in the question.

That's a panda? I thought it was snoopy....

In the output example of the panda, a few characters are missing on the left.

GolfScript
I thought golfing is popular on this site, so a GolfScript submission always fits for a popularity contest.
n%.0=,' '[*]\1$++{0\0++{42=}%[{.}*]);2/}%{.}*]);2/{zip{~+2base' /\-\|/ /\| -   '=}%n+}%

You can try the code with your own ascii art here. Note that it is required that all lines have the same length.
Example:
                                              /------------\
                                          /---  ---------   --\
                                     /----  ---/         \---  -\
                                    /   ---/                 \--\---\
                             /------   /                        \--  \
                          /--         /                            \  \
                     /----  --       |                              \  \
                 /---  ----/ /       /    /--\                       \  \
              /--  ---/     /       |    /    \                       \-\\
             /  --/        /        |    |    |                   /----\\\\
           /-  /          /         /   /      \                 /     | | \
          /  -/          /         |    |      |                 |     | \  \
         /  /            |         |   /       /                /       \ \ |
        //-/            /          |  /      -/                 |       |  ||
       / |             /           |  |     /                   |       |  \\\
       | /             |           |  \    /                    \        \  ||
      ///             /            |   \--/                      \       /  ||
     ///              |            |                              \-    /   ||
    / |               |            |                                \--/    ||
    | /              /             |                                        ||
   / |               |             /                                       / |
   | /               |            |                                        | |
  / |               /             /                                        |  \
 /  |               |            |                                         |   \
 |  /               |            /                                         |   |
 | |               /            |                                          |   |
 | |               |            |          /----\                          |   |
 | |               |            |         /  --  \         /-\             |   |
 | |               \            |         | /  \ |        /  \-\           |   |
 | /                |           |         | \  / |       /  /\  \          |   |
 ||                 |            \       /   --  /       | |  |  \         |   |
 ||                 |            /       \      /        \  --   |         |   |
 ||                 \            \        |   -/          \-     |         |   |
/ |    /\            |            \       \--/              |    /        /    |
| |   /  \           |             \                        \---/        /     /
| /  /    \          \             |                                    /     |
| \/-     |           \             -\                                 /      |
|         |            |              \             /\               /-       |
|         |            \               --\         /  \            /-         |
|          \            \                 --\      \--/         /--           |
|          |             \                   ---\           /---              |
|           \             |                 ----\-\    /----                  |
|            \            |                 \   \-\---\\---                   |
|             \           |                 |     \---/   /                   |
|              -\         \                  \            \                   |
|               /         /                  |             |                  |
|               \         \                  |             |                  /
|                -\        |                 |            /                  |
|                  \       \                 |            |                  |
|                   -\     /                 |          /-                   /
|                     -----                  |         /                    |
|                                             \      /-                     |
|                        ----                 |     /                       |
\                       /    \                 --\/-                        |
 |                     /      |                                             /
 |                    |       |                            -               |
 |                    /       |                          -/ |              |
 |                   |        |                     ----/   |              |
 |                   /        |                    /       /               |
 |                  /         |                 --/        |               |
 |                 /          \                |           |               /
 |                |            |               |           |              |
 |                |            |                \          |              |
 \                |            |                |          |              |
  \               /            |                 \         |              |
   \-------------/             \-                |         |               \
                                 \               /         \               |
                                  \-------------/           \              |
                                                             \             /
                                                              \-----------/

Golfscript is always relevant.

Ha it makes the panda look enraged +1 for using more than 1 character.

The Python code, primarily relies on regex to do the task.
def edge_detect(st):
    width = len(max(st.split('\n'), key=len))
    #Pad the image with extra blank lines above and below
    st = '{{0: <{0}}}\n{{1}}\n{{0: <{0}}}'.format(width).format('', st.rstrip('\n'))
    return re.sub(r"""                     #Match a non space character
                      (?<=\s.{{{0}}})[^\s]|#When the char above is a space
                      [^\s](?=.{{{0}}}\s) |#When the char below is a space
                      (?<=\s)[^\s]|        #When the previous char is a space
                      [^\s](?=\s)          #When the following char is a space
                   """.format(width),
                   '+',                    #Replace it with edge character '+'
                   st,
                   flags=re.X|re.S).replace('*',' ')  # And finally replace all
                                           # non-space character with spaces

*Output
>>> print edge_detect(st)
                                              +++++++++++++
                                          +++++          +++++
                                     ++++++                  +++
                                    +++                         ++++
                             +++++++++                             ++
                          ++++      +                               ++
                     ++++++   +    ++                                ++
                 +++++       ++    +      +++                         ++
              ++++          ++     +     ++  +                          +
             ++            ++     ++     +   +                    +++++  +
           +++            ++      +     ++    +                  ++   +  ++
          ++             ++       +     +    ++                  +    +   ++
         ++              +        +    ++  +++                  ++     +   +
        +               ++        +   ++  ++                    +      +   +
       ++              ++         +   +  ++                     +      +    +
       +               +          +    +++                       +     ++   +
      +               ++          +                               +   ++    +
     +                +           +                                 +++     +
    ++                +           +                                         +
    +                ++          ++                                         +
   ++                +           +                                         ++
   +                 +          ++                                         ++
  ++                ++          +                                          + +
 +++                +          ++                                          +  +
 ++                 +          +                                           +  +
 ++                ++          +                                           +  +
 ++                +           +           +++++                           +  +
 ++                +           +          ++   ++          ++              +  +
 ++                 +          +          +     +         ++ ++            +  +
 +                  +          +          ++   ++        ++   ++           +  +
 +                  +          ++        ++  +++         ++   + +          +  +
 +                  +          +          + +++           + +++ +          +  +
 +                   +          +         +++               +  ++          +  +
++     +             +           +                          ++++          ++ ++
++    +++            +            +                                      ++  +
+    ++  +            +           +                                     ++   +
++ +++   +             +            +                                  ++    +
+  +     +             +             +              +                +++     +
+        +              +              ++          +++             +++       +
+         +              +                ++                    ++++         +
+         +               +               ++++++            +++++            +
+          +              +               +     ++     ++++++                +
+           +             +                +      ++++     +                 +
+            +            +                +              ++                 +
+             ++           +                +              +                 +
+             +           ++                +              +                ++
+              +           +                +              +                +
+                +         +                +             ++                +
+                 +         +               +             +                ++
+                   +      ++               +           +++                +
+                     ++++++                +          ++                  +
+                      +++++                 +       +++                   +
+                     ++     +               +      ++                     +
 +                   ++       +                ++ +++                     ++
 +                   +        +                   +      ++               +
 +                  ++        +                        +++  +             +
 +                  +         +                   ++++++    +             +
 +                 ++         +                  ++         +             +
 +                ++          +               ++++         ++             +
 +               ++           +               +            +             ++
 +               +             +              +            +             +
 +               +             +              +            +             +
 +               +             +               +           +             +
  +             ++             +               +           +             +
   ++++++++++++++              +                +          +             +
                                 +             ++          +              +
                                  ++++++++++++++            +             +
                                                             +           ++
                                                              ++++++++++++

Another one
print edge_detect(st)
     +++
   ++ +
 ++   +
+    +
+    +
 ++   +
   ++ +
     +++

Python
Using gradient operation to identify edges:
from numpy import gradient, zeros
import matplotlib.pylab as plt
b = open("file").readlines()
bi = zeros((len(b),len(b[0])))
e = enumerate
for i,l in e(b):
    for j,c in e(l):
        if(c=="*"): bi[i][j]=1
g = gradient(bi,.5,.5)
g = (abs(g[0])+abs(g[1]))>=1.
plt.subplot(2,1,1)
plt.imshow(bi,cmap='Greys'), plt.show()
plt.subplot(2,1,2)
plt.imshow(g*1,cmap='Greys'), plt.show()

Output for the banana and panda:

To make an ASCII Output, the part of the plots should be replaced with:
r = range
s = ""
for i in r(len(b)):
    for j in r(len(b[0])):
        if(g[i][j]!=0):
            s+="*"
        else:
            s+=" "
    s +="\n"
print s

ASCII Output:
Banana:
   *** **
 **** **
***   **
*    **
*    **
***   **
 **** **
   *** **

Panda:
                                          *************** ****
                                     ******   *************  ***
                                    ***   *****          ***** *****
                             ******** *****                  ****  **
                          ********** **                         ******
                     ******  *      **                             ****
                 *****    *****     **    ***                       ****
              ****   ****** **     **    *****                       ****
             **  *****     **      **   **   **                   **** * *
           *******        **       **   **   **                  **********
          ** **          **       **   **     **                **    ******
         *****          **        **   **     **                **    ** ****
        ****            **        **  **    ***                **      ** * *
       ****            **         ** **    ***                 **      ** ***
      ****            **          ** **   **                   **      **  ***
      ***             **          **  *****                     **      ** * *
     * *             **           **   ***                       ***   **  * *
    ***              **           **                              ******   * *
   ****              **           **                                ***    * *
   * *              **            **                                       * *
  ****              **           **                                       ****
  * *               **           **                                       ****
 ****              **           **                                        ** **
** **              **           **                                        **  **
****               **          **                                         **  **
****              **           **          *****                          **  **
****              **           **         **   **          **             **  **
****              **           **        *********        *****           **  **
****               **          **        * *   * *       *******          **  **
* *                **          **        *********      **** ****         **  **
* *                **           **      **  *****       ******* **        **  **
* *                **          **        **  ***         ** **  **        **  **
* *    *            **          **       ******           ***   **        **  **
***   ***           **           **       ***              ******        **   **
 **  ** **          **            **                        ****        **   **
** ***   **          **           ***                                  **    **
 ****    **           **           ***              *                ***     **
  *      **           **             ****          ***             ****      **
         **            **             ******      *****         *****        **
          **            **               *******   ***      *******          **
          **             **                *   ***     *********             **
           **            **               **************   *                 **
            **           **                **   ***  *******                 **
             ***         **                **     ****   **                  **
              ***         **                **            **                 **
              **         **                 **            **                 **
               ***        **                **            **                **
                ***       **                **           **                 **
                  ***      **               **          ***                 **
                   *********                **         ***                 **
                     ******                 **       ***                   **
                        *****                **     ***                    **
*                      *******               **** ***                      **
**                    **     **               ******                       **
**                   **      **                  *        **              **
**                   **      **                         *****             **
**                  **       **                    ******* **             **
**                  **       **                   ******   **             **
**                 **        **                ****       **              **
**                **         **               ****        **              **
**               **           **              **          **             **
**               **           **              **          **             **
**               **           **               **         **             **
 **              **           **               **         **             **
  ****************            ***               **        **             **
   **************              ***              **        **              **
                                 ****************          **             **
                                  **************            **            **
                                                             **          **

A banana? It's a boomerang for me. :)

Mathematica
Assuming that panda contains the original string, the following obtains the pixels of the contour edges and replaces them with "+".
t = (Most /@ Partition[Take[Characters@panda, {61, 5636}], 82]) /. {"*" -> 1, " " -> 0};
Grid[ImageData[EdgeDetect[Image[t]]] /. {1 -> "+", 0 -> " "}, Spacings -> 0]

How it works
Partition[Take[Characters@panda, {61, 5636}], 82] breaks the string up into lines of characters.
Most removes the new line characters.
Image[t]]] converts the matrix of 0's and 1's into an image.
EdgeDetect finds the edges of the image.
ImageData obtains the binary matrix of the image.
 /. {1 -> "+", 0 -> " "} replaces each 0 and 1 with the appropriate character.
Grid displays the Ascii art.

Java
This checks lines and rows if there's anything on top, left, right or bottom and based on that picks a value from array holding symbol that's used to generate new art using edges!
Example output:
                                              /^^^^^^^^^^^\
                                          /^^^/          \_^^\
                                     /^^^^/                  \^\
                                    / /                         /^^\
                             /^^^^^^ /                             \\
                          /^^_      |                               \\
                     /^^^^/   |     /                                \\
                 /^^^/       /     |      /^\                         \\
              /^^/          /      |     /   \                          /
             //            /       /     |   |                    /^^^\  /
           /^/            /       |     /     \                  /    |  \\
          //             /        |     |     /                  |    |   \\
         //              |        |    /    _/                  /      \   |
        /               /         |   /    /                    |      |   \
       //              /          |   \   /                     \      |    /
       \               |          |    \_/                       \      \   |
      /               /           |                               \_   /    |
     /                |           |                                 \_/     |
    //                |           |                                         |
    |                /            /                                         |
   //                |           |                                         /|
   |                 |           /                                         ||
  /|                /           |                                          | \
 / /                |           /                                          |  \
 ||                 |          |                                           |  |
 ||                /           |                                           |  |
 ||                |           |           /^^^\                           |  |
 ||                \           |          //   \\          /\              |  |
 |/                 |          |          |     |         // /\            |  |
 |                  |          |          |\   //        /|   |\           |  |
 |                  |           \        /  ^^^/         \|   | \          |  |
 |                  \          |          |  _/           \^^^  |          |  |
 |                   |          \         \_/               |   /          |  |
/|     /             |           \                          \__/          /   /
|/    / \            \            \                                      /   |
|    /   \            \           |                                     /    |
|\ /^    |             |           ^\                                  /     |
| ^      |             \             \              /                /^      |
|        |              \             ^^\          /_\             /^        |
|         \              \               ^^\                    /^^          |
|         |               |                _^^^\            /^^^             |
|          \              |               |     /\     /^^^^                 |
|           \             |                \      /^^\     |                 |
|            \            \                |              /                  |
|             ^\           |                \              |                 |
|             |           /                 |              |                 /
|              \           |                |              |                |
|               ^\         \                |             /                 |
|                 \         |               |             |                 /
|                  ^\      /                |           /^                 |
|                    ^^^^^^                 |          /                   |
|                       _____                \       /^                    |
\                      /     \               |      /                      |
 |                    /       |               ^^\ /^                       /
 |                   |        |                  ^        __              |
 |                   /        |                         _/  |             |
 |                  |         |                    ____/    |             |
 |                  /         |                   /         |             |
 |                 /          |                __/         /              |
 |                /           \               |            |              /
 |               |             |              |            |             |
 |               |             |              |            |             |
 \               |             |               \           |             |
  \              /             |               |           |             |
   \____________/              \_               \          |             |
                                 \              /          \              \
                                  \____________/            \             |
                                                             \            /
                                                              \__________/
     /^\
   /^ |
 /^   /
/    |
\    |
 \_   \
   \_ |
     \_\

Code:
package com.ruuhkis.asciiedge;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ASCIIEdge {

    public static void main(String[] args) {
        try {
            if (args.length > 0) {
                for (String arg : args) {
                    new ASCIIEdge().detach(new FileInputStream(new File(arg)));
                }
            } else {
                new ASCIIEdge().detach(new FileInputStream(new File("art.txt")));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void detach(InputStream is) throws IOException {
        BufferedReader bw = new BufferedReader(new InputStreamReader(is));

        StringBuilder artBuilder = new StringBuilder(100);

        int longestLineLength = 0;
        int numLines = 0;

        String line = null;

        while ((line = bw.readLine()) != null) {
            artBuilder.append(line + System.getProperty("line.separator"));

            if (line.length() > longestLineLength) {
                longestLineLength = line.length();
            }

            numLines++;

        }

        bw.close();

        char[][] artBuffer = new char[numLines][longestLineLength];

        String[] lines = artBuilder.toString().split(
                System.getProperty("line.separator"));

        for (int i = 0; i < lines.length; i++) {
            lines[i].getChars(0, lines[i].length(), artBuffer[i], 0);
            for (int j = lines[i].length(); j < longestLineLength; j++) {
                artBuffer[i][j] = ' ';
            }
        }

        detach(artBuffer, longestLineLength, numLines);
    }

    private void detach(char[][] artBuffer, int longestLineLength, int numLines) {
        char[][] outputBuffer = new char[numLines][longestLineLength];
        for (int i = 0; i < numLines; i++) {
            for (int j = 0; j < longestLineLength; j++) {
                ;

                int horizontalEdge = isHorizontalEdge(artBuffer[i], j,
                        longestLineLength);
                int verticalEdge = isVerticalEdge(artBuffer, i, j, numLines);

                char[][] result = { { '/', '^', '\\' }, { '|', ' ', '|' },
                        { '\\', '_', '/' } };

                outputBuffer[i][j] = result[verticalEdge + 1][horizontalEdge + 1];
                ;

            }

            System.out.println(outputBuffer[i]);
        }
    }

    private int isVerticalEdge(char[][] chars, int row, int col, int numLines) {
        boolean canBeEdge = chars[row][col] != ' ';
        boolean upperEdge = row - 1 < 0 || chars[row - 1][col] == ' ';
        boolean lowerEdge = row + 1 >= numLines || chars[row + 1][col] == ' ';

        return (canBeEdge && (upperEdge || lowerEdge)) ? upperEdge ? -1 : 1 : 0;
    }

    private int isHorizontalEdge(char[] chars, int index, int length) {
        boolean canBeEdge = chars[index] != ' ';
        boolean leftEdge = index - 1 < 0 || chars[index - 1] == ' ';
        boolean rightEdge = index + 1 > length - 1 || chars[index + 1] == ' ';
        return (canBeEdge && (leftEdge || rightEdge)) ? leftEdge ? -1 : 1 : 0;
    }

}

Ps. first timer here, don't be rough :D
Pps. that Panda looks like its sad :(

I assure you no pandas were harmed during the making of this ASCII art

Python (ascii -> image -> edge filter -> ascii)
I kind of cheated, I converted the ascii text to an image and ran PIL edge detect filter on it. Then, I inverted the image and converted back to ascii text:
from PIL import Image, ImageFilter, ImageOps
import random
from bisect import bisect

greyscale = [" "," ",".,-","_ivc=!/|\\~","gjez2]/(YL)t[+T7Vf","mdK4ZGbNDXY5P*Q","W8KMA","#%$"]
zonebounds=[36,72,108,144,180,216,252]

f=open('input.txt', 'r')
lines=f.readlines()
f.close()

width=82
height=len(lines)

im = Image.new("RGB", (width,height), "white")
pixels = im.load()

y=0
for line in lines:
    x=0
    for px in line:
        if px != ' ' and x < width:
            pixels[x,y] = (0,0,0)
        x+=1
    y+=1

im=im.resize((width, height),Image.BILINEAR)
im=im.convert("L")
im=im.filter(ImageFilter.FIND_EDGES)
im=ImageOps.invert(im)

str=""
for y in range(0,im.size[1]):
    for x in range(0,im.size[0]):
        lum=255-im.getpixel((x,y))
        row=bisect(zonebounds,lum)
        possibles=greyscale[row]
        str=str+possibles[random.randint(0,len(possibles)-1)]
    str=str+"\n"
print str

Here is the result:
$#$$#%$$$$$$%%$$$%$$$$%$###%%#%%%$##$$$%%#$#%#              $%%%%$$%$###$$%%%##%##
%                                   $%%$$$     %#$%$$$#$$     $%#               #
%                                  %%      $%$#$        $$$$%   %#%#%           #
%                           %$#$%%$$   #%$$%                %%%%    %$          #
$                        #$$$         %$                       $###  #%         $
%                   #%$#$#           $%                           %#  #$        #
#               #%$%#      $##       $   $%%$%                     $$  $%       #
%            $%#%     $##%$%$       ##  $%   %#                     $$  %$      $
%           %$    %$%$$   %#        $   #     #                  $#$#$$$ $$     %
%         $%#  $##%      ##         #  #%     %$                #$     #% %%    %
%        #$   #$        %$         %$  #       %                $      $$  $$   #
#       $%  $%#         %          #  ##       #               $%      %#%  $   #
%      #%  #%          %#          # $$       $%               $        $$# $   %
%     #% $$%          %%           % $      $%#                #        % $ ##  $
%     $  %            #            # %     %%                  %        #$#$ #  #
%    $% $%           #%            % %$   #%                   %%        # $ %  %
$   $$ %$            %             #  %#$%%                     ##      $$ # $  %
#  $% #$             $             $                             #$$   $%  % $  $
%  %  %             #%             #                               %$#%#   $ $  %
# $$ #%             $              #                                      ## #  %
$ %  #              $             #$                                      #  $  %
%$$ ##             %#             %                                       $  $$ #
$$  %              #             $%                                       #   #$%
$   $              %             %                                        %    $#
$  %#             #%            $%                                        #    #$
%  %              #             $         #%$##%$                         %    %#
$  $              #             $        $%     $#        %#%%            #    %%
#  %              #             #        %  $%$  %       %%  %%#          %    $#
%  #              #%            #        # ## $# #      %#  %  %$         $    #$
$ $%               %            $%      $%  $$$  %      $  #$#  %#        %    #$
# %                $             $      #       $%      $  #$%   %        $    $$
% %                $            $%      %#     ##       $$       $        #    ##
% #   ###          %#            #%      $   %%$         #$%     #       %#    %%
  #  %% $#          #             ##     ##%$#             #    %#      $$     %#
  % %%   $$         %              %                       ##$%$#      %%     #%%
 #%$%     #         #$             ##%                                %#      % %
  %       %          %%              $#            %#%              $%$       $ %
          $           #               $$$%        #$ %$           %##         $ #
          #%          $%                 %$#%     %   $        %%$#           # $
           %           ##                   #%##$ #$#$$    %#%%%              % %
           $%           $%                      $##   %##$$%                  # %
            %#           %                 ##$##  ##$#$                       % #
             $%          #                  %  %#$    $$#%#                   # %
              #%%        %                  ##   %$##$%  #                    # $
                %        %$                  $           %$                   % %
               %%        #                   $            %                   $ #
                %%#      #%                  #           ##                  $$ $
                  %$      $                  %           #                   %  %
                   %##    #%                 $         %#$                   #  #
                     %#$##%                  $        #%                    %$  $
                                             $$     #%$                     $   #
                                              #    ##                       #   $
                        #%%$%                 $%#%$%                        %   $
#                      $%   %$                   #                          %   $
#                     %#     #                                             $#   %
%                     #      %                            $#               $    $
%                    %#      #                          #$#%               $    %
$                    $       #                     %%%$#$ #$               %    #
$                   $%       $                    %%      $                $    %
#                  #$        %                 #%#%       %                $    %
%                 $%         ##                #          %               #$    $
%                 #           #                %$         %               #     #
$                 #           $                 #         #               %     %
%$                #           $                 %$        %               $     #
##%              #%           %                  #        $               %$    %
# ##%#%$$#%#%$$#%#            #%$                #        %                #    %
#                               ##              ##        $#               $    #
%                                $##$#%$#$$$#$$%%          $$              #    %
$#%#$#$$#$##$%#$%#$$$$%#$#%%#$%#$$#%%%%$%%$%$#%$##%%$#$$##%#$%            #$%$$$$$

Sources cited (for greyscale image to ascii conversion):
http://stevendkay.wordpress.com/2009/09/08/generating-ascii-art-from-photographs-in-python/

I like it! I once tried to do it only using commands: convert (to convert ASCII art to image); GIMP-command-line (to perform edge detection); and aplay (to render the image in ASCII art). but failed:p (and welcome to Codegolf)

Thanks! I thought to myself, how can I leverage all the work that has already been conducted on image convolution with minimum rework. I also wanted to output more than one type of character to "blend" the text lines, this gives it a more retro feel. Ascii artists used to draw with text paint programs and use different symbols to make patterns like this. On a related note, there was also a version of Quake that redrew the pixels in ascii. It was very surreal.

k4
this is a lazy version that just finds the edges, it doesn't try to find the prevailing shape and pick an appropriate character
the "algo" is just to find all cells that differ from the one to the left or above, then use array-language magic to turn the boolean matrix back into ASCII art
there's a minor hack to pad the beginning of everything with an extra space, or it would consider the left and top to be "edges" -- this way it still does, but i can just drop that off later
$ cat o.k
f:{(1_~~':" ",)'x}
g:{(f x)|+f@+x}
h:{-1" +"g@0:`$,/$x;}

h@*.z.x
\\
$

more or less the same output as the sample in the spec
here it is on the boomerang/banana:
$ cat b.txt
     ***
   ****
 ******
******
******
 ******
   ****
     ***
$ q o.k -q b.txt
     +++
   ++  +
 ++    +
+     +
+     +
++    ++
 +++   +
   +++ +
$

Python
This is very simple Python version of "highpass" filter :). It checks whether a pixel is at least surrounded by its 4 sides. This is one of my first Python codes ever so please be subtle...
import sys
s = open("ascii.txt",'r').read().split("\n")
def px(x,y):
  try:
    v = s[x][y]
  except IndexError:
    v = ' '
  return v
def r(x,y):
  return '{:<3}'.format(s[x][y-1:y+2])
def v(x,y):
  return (px(x,y)==' ' or r(x,y)=='***' and px(x-1,y)=='*' and px(x+1,y)=='*') and ' ' or '*'
for row in range(len(s)):
  for cell in range(len(s[row])):
    sys.stdout.write(v(row,cell))
  print

Output:
                                              *************
                                          *****          *****
                                     ******                  ***
                                    * *                         ****
                             ******* *                             **
                          ****      *                               **
                     ******   *     *                                **
                 *****       *     *      ***                         **
              ****          *      *     *   *                          *
             **            *       *     *   *                    *****  *
           ***            *       *     *     *                  *    *  **
          **             *        *     *     *                  *    *   **
         **              *        *    *    **                  *      *   *
        *               *         *   *    *                    *      *   *
       **              *          *   *   *                     *      *    *
       *               *          *    ***                       *      *   *
      *               *           *                               **   *    *
     *                *           *                                 ***     *
    **                *           *                                         *
    *                *            *                                         *
   **                *           *                                         **
   *                 *           *                                         **
  **                *           *                                          * *
 * *                *           *                                          *  *
 **                 *          *                                           *  *
 **                *           *                                           *  *
 **                *           *           *****                           *  *
 **                *           *          **   **          **              *  *
 **                 *          *          *     *         ** **            *  *
 *                  *          *          **   **        **   **           *  *
 *                  *           *        *  ****         **   * *          *  *
 *                  *          *          *  **           ****  *          *  *
 *                   *          *         ***               *   *          *  *
**     *             *           *                          ****          *   *
**    * *            *            *                                      *   *
*    *   *            *           *                                     *    *
** **    *             *           **                                  *     *
* *      *             *             *              *                **      *
*        *              *             ***          ***             **        *
*         *              *               ***                    ***          *
*         *               *                *****            ****             *
*          *              *               *     **     *****                 *
*           *             *                *      ****     *                 *
*            *            *                *              *                  *
*             **           *                *              *                 *
*             *           *                 *              *                 *
*              *           *                *              *                *
*               **         *                *             *                 *
*                 *         *               *             *                 *
*                  **      *                *           **                 *
*                    ******                 *          *                   *
*                       *****                *       **                    *
*                      *     *               *      *                      *
 *                    *       *               *** **                       *
 *                   *        *                  *        **              *
 *                   *        *                         **  *             *
 *                  *         *                    *****    *             *
 *                  *         *                   *         *             *
 *                 *          *                ***         *              *
 *                *           *               *            *              *
 *               *             *              *            *             *
 *               *             *              *            *             *
 *               *             *               *           *             *
  *              *             *               *           *             *
   **************              **               *          *             *
                                 *              *          *              *
                                  **************            *             *
                                                             *            *
                                                              ************

Can you please add the output?

Wow, thanks for formatting. Couldn't find Python highlighting. Output added.

You're welcome. :) AFAIK, the highlighting requires to use the file extension of the language: <!-- language: lang-py -->

R
For each point in the matrix, hide the character if it's surrounded by asterisks on right, left, top and bottom. This is naive but works very well.
text = scan(commandArgs(T)[1], what='raw', sep="\n", quiet=T)

height = length(text); width = max(sapply(text, nchar))

mat_2 = mat = matrix(unlist(strsplit(text, '')), height, width, byrow=T)

for (y in 1:nrow(mat_2)) {
    for (x in 1:ncol(mat_2)) {
        if (
            ((x < ncol(mat)) && (mat[y, x + 1] == '*')) # right
            &&
            ((x > 1) && (mat[y, x - 1] == '*')) # left
            &&
            ((y > 1) && (mat[y - 1, x] == '*')) # top
            &&
            ((y < nrow(mat)) && (mat[y + 1, x] == '*')) # bottom
        ) {
            mat_2[y,x] = ' '
        }
    }
}

for (a in 1:nrow(mat_2)) {
    cat(paste(mat_2[a,], collapse=""), "\n")
}

Usage: Rscript script.r input_.txt
Output
                                              *************
                                          *****          *****
                                     ******                  ***
                                    * *                         ****
                             ******* *                             **
                          ****      *                               **
                     ******   *     *                                **
                 *****       *     *      ***                         **
              ****          *      *     *   *                          *
             **            *       *     *   *                    *****  *
           ***            *       *     *     *                  *    *  **
          **             *        *     *     *                  *    *   **
         **              *        *    *    **                  *      *   *
        *               *         *   *    *                    *      *   *
       **              *          *   *   *                     *      *    *
       *               *          *    ***                       *      *   *
      *               *           *                               **   *    *
     *                *           *                                 ***     *
    **                *           *                                         *
    *                *            *                                         *
   **                *           *                                         **
   *                 *           *                                         **
  **                *           *                                          * *
 * *                *           *                                          *  *
 **                 *          *                                           *  *
 **                *           *                                           *  *
 **                *           *           *****                           *  *
 **                *           *          **   **          **              *  *
 **                 *          *          *     *         ** **            *  *
 *                  *          *          **   **        **   **           *  *
 *                  *           *        *  ****         **   * *          *  *
 *                  *          *          *  **           ****  *          *  *
 *                   *          *         ***               *   *          *  *
**     *             *           *                          ****          *   *
**    * *            *            *                                      *   *
*    *   *            *           *                                     *    *
** **    *             *           **                                  *     *
* *      *             *             *              *                **      *
*        *              *             ***          ***             **        *
*         *              *               ***                    ***          *
*         *               *                *****            ****             *
*          *              *               *     **     *****                 *
*           *             *                *      ****     *                 *
*            *            *                *              *                  *
*             **           *                *              *                 *
*             *           *                 *              *                 *
*              *           *                *              *                *
*               **         *                *             *                 *
*                 *         *               *             *                 *
*                  **      *                *           **                 *
*                    ******                 *          *                   *
*                       *****                *       **                    *
*                      *     *               *      *                      *
 *                    *       *               *** **                       *
 *                   *        *                  *        **              *
 *                   *        *                         **  *             *
 *                  *         *                    *****    *             *
 *                  *         *                   *         *             *
 *                 *          *                ***         *              *
 *                *           *               *            *              *
 *               *             *              *            *             *
 *               *             *              *            *             *
 *               *             *               *           *             *
  *              *             *               *           *             *
   **************              **               *          *             *
                                 *              *          *              *
                                  **************            *             *
                                                             *            *
                                                              ************

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
Build a minifloat adding machine using NAND logic gates
A minifloat is a binary representation of a floating-point number that has very few bits.
The minifloat in this question will be defined as an 6-bit number m, which has the following representation:

1 bit to repesent the sign of the number. This bit will be 0 if the number is positive, and 1 if the number is negative.
3 bits to represent the exponent of the number, offset by 3 (i.e. an exponent of 110 actually represents a factor of 23, not 26).

An exponent of 000 refers to a subnormal number. The mantissa refers to the fractional part of a number with an integer part of 0 multiplied by a factor of the lowest possible exponent (in this case, 2-2).

2 bits to represent the mantissa of the number. If the exponent is anything other than 000 or 111, the 2 bits represent the fractional part after a 1.

An exponent of 111 represents infinity if the mantissa is 0, and NaN (not a number) otherwise.

In the Wikipedia article, this would be referred to as a (1.3.2.3) minifloat.
Some examples of this minifloat's representation:
000000 =  0.00 = 0
000110 =  1.10 × 2^(1-3) = 0.375
001100 =  1.00 × 2^(3-3) = 1
011001 =  1.01 × 2^(6-3) = 10
011100 = infinity
011101 = NaN
100000 = -0.00 = -0
100011 = -0.11 × 2^(1-3) = -0.1875 (subnormal)
101011 = -1.11 × 2^(2-3) = -0.875
110100 = -1.00 × 2^(5-3) = -4
111100 = -infinity
111111 = NaN

Your task is to build a network of two-input NAND gates that takes 6 inputs representing a minifloat a and 6 inputs representing a minifloat b, and returns 6 outputs representing the minifloat a + b.

Your network must properly add subnormals. For example, 000001 + 000010 must equal 000011, and 001001 + 000010 = 001010.
Your network must add and subtract infinities properly. Anything finite added to an infinity is the same infinity. Positive infinity plus negative infinity is NaN.
A NaN plus anything must equal a NaN, although which NaN it equals is up to you.
How you handle adding positive zero and negative zero to each other is up to you, although zero plus zero must equal zero.

Your network can implement any of the following rounding rules depending on convenience:

Round down (toward negative infinity)
Round up (toward positive infinity)
Round toward zero
Round away from zero
Round to nearest, with halves rounded according to any of the above rules

To simplify things, you may use AND, OR, NOT, and XOR gates in your diagram, with the following corresponding scores:

NOT: 1
AND: 2
OR: 3
XOR: 4

Each of these scores corresponds to the number of NAND gates that it takes to construct the corresponding gate.
The logic circuit that uses the fewest NAND gates to correctly implement all the above requirements wins.

Nice challenge - I'd have to seriously think about this to implement it in code, let alone NAND gates.

830 NANDs
It uses 24 NOTs, 145 ANDs, 128 ORs, 33 XORs. It always rounds towards zero, it may return either -0 or +0 for zero values, and I believe it treats Infinities and NaNs correctly:

± INF ± INF = ± INF
± INF + NaN = ± INF
± INF ∓ INF = NaN
± INF + number = ± INF
NaN + NaN = NaN
NaN + number = NaN

Below I have a coded representation of the circuit. I have little experience annotating these types of things, so I don't really know what the typical way to do this is, but every variable is a Boolean so it's clear to see that it describes a circuit. Another thing, I have neither the know-how nor probably the tenacity to try and make a diagram of this, but if there is any easy to use software out there anyone wants to point out I would be interested in taking a look.
a0,a1,a2,a3,a4,a5 = mini0
b0,b1,b2,b3,b4,b5 = mini1

neg = XOR(a0,b0)
nneg = NOT(neg)

na1 = NOT(a1)
na2 = NOT(a2)
na3 = NOT(a3)

a2_a3 = AND(a2,a3)
a2_na3 = AND(a2,na3)
na2_a3 = AND(na2,a3)
na2_na3 = AND(na2,na3)

a123 = AND(a1,a2_a3)
l0 = AND(a1,a2_na3)
l1 = AND(a1,na2_a3)
l2 = AND(a1,na2_na3)
l3 = AND(na1,a2_a3)
l4 = AND(na1,a2_na3)
l5 = AND(na1,na2_a3)
l6 = AND(na1,na2_na3)

a45 = OR(a4,a5)
a_nan = AND(a123,a45)
a_inf = AND(a123,NOT(a45))

m0 = l0
m1 = OR(l1,AND(l0,a4))
m2 = OR(l2,OR(AND(l1,a4),AND(l0,a5)))
m3 = OR(l3,OR(AND(l2,a4),AND(l1,a5)))
m4 = OR(l4,OR(AND(l3,a4),AND(l2,a5)))
m5 = OR(l5,OR(AND(l4,a4),AND(l3,a5)))
l5_l6 = OR(l5,l6)
m6 = OR(AND(l4,a5),AND(l5_l6,a4))
m7 = AND(l5_l6,a5)

nb1 = NOT(b1)
nb2 = NOT(b2)
nb3 = NOT(b3)

b2_b3 = AND(b2,b3)
b2_nb3 = AND(b2,nb3)
nb2_b3 = AND(nb2,b3)
nb2_nb3 = AND(nb2,nb3)

b123 = AND(b1,b2_b3)
k0 = AND(b1,b2_nb3)
k1 = AND(b1,nb2_b3)
k2 = AND(b1,nb2_nb3)
k3 = AND(nb1,b2_b3)
k4 = AND(nb1,b2_nb3)
k5 = AND(nb1,nb2_b3)
k6 = AND(nb1,nb2_nb3)

b45 = OR(b4,b5)
b_nan = AND(b123,b45)
b_inf = AND(b123,NOT(b45))

n0 = k0
n1 = OR(k1,AND(k0,b4))
n2 = OR(k2,OR(AND(k1,b4),AND(k0,b5)))
n3 = OR(k3,OR(AND(k2,b4),AND(k1,b5)))
n4 = OR(k4,OR(AND(k3,b4),AND(k2,b5)))
n5 = OR(k5,OR(AND(k4,b4),AND(k3,b5)))
k5_k6 = OR(k5,k6)
n6 = OR(AND(k4,b5),AND(k5_k6,b4))
n7 = AND(k5_k6,b5)

first = n0,n1,n2,n3,n4,n5,n6,n7

i7 = n7
i6 = XOR(n6,n7)
carry_6 = OR(n6,n7)
i5 = XOR(n5,carry_6)
carry_5 = OR(n5,carry_6)
i4 = XOR(n4,carry_5)
carry_4 = OR(n4,carry_5)
i3 = XOR(n3,carry_4)
carry_3 = OR(n3,carry_4)
i2 = XOR(n2,carry_3)
carry_2 = OR(n2,carry_3)
i1 = XOR(n1,carry_2)
carry_1 = OR(n1,carry_2)
i0 = XOR(n0,carry_1)
i_sign = OR(n0,carry_1)

n7 = OR(AND(nneg,n7),AND(neg,i7))
n6 = OR(AND(nneg,n6),AND(neg,i6))
n5 = OR(AND(nneg,n5),AND(neg,i5))
n4 = OR(AND(nneg,n4),AND(neg,i4))
n3 = OR(AND(nneg,n3),AND(neg,i3))
n2 = OR(AND(nneg,n2),AND(neg,i2))
n1 = OR(AND(nneg,n1),AND(neg,i1))
n0 = OR(AND(nneg,n0),AND(neg,i0))
n_sign = AND(neg,i_sign)

r7 = XOR(m7,n7)
carry_7 = AND(m7,n7)
hr6 = XOR(m6,n6)
hcarry_6 = AND(m6,n6)
r6 = XOR(hr6,carry_7)
carry_6 = OR(hcarry_6,AND(hr6,carry_7))
hr5 = XOR(m5,n5)
hcarry_5 = AND(m5,n5)
r5 = XOR(hr5,carry_6)
carry_5 = OR(hcarry_5,AND(hr5,carry_6))
hr4 = XOR(m4,n4)
hcarry_4 = AND(m4,n4)
r4 = XOR(hr4,carry_5)
carry_4 = OR(hcarry_4,AND(hr4,carry_5))
hr3 = XOR(m3,n3)
hcarry_3 = AND(m3,n3)
r3 = XOR(hr3,carry_4)
carry_3 = OR(hcarry_3,AND(hr3,carry_4))
hr2 = XOR(m2,n2)
hcarry_2 = AND(m2,n2)
r2 = XOR(hr2,carry_3)
carry_2 = OR(hcarry_2,AND(hr2,carry_3))
hr1 = XOR(m1,n1)
hcarry_1 = AND(m1,n1)
r1 = XOR(hr1,carry_2)
carry_1 = OR(hcarry_1,AND(hr1,carry_2))
hr0 = XOR(m0,n0)
hcarry_0 = AND(m0,n0)
r0 = XOR(hr0,carry_1)
carry_0 = OR(hcarry_0,AND(hr0,carry_1))
r_sign = XOR(n_sign,carry_0)

s7 = r7
s6 = XOR(r6,r7)
carry_6 = OR(r6,r7)
s5 = XOR(r5,carry_6)
carry_5 = OR(r5,carry_6)
s4 = XOR(r4,carry_5)
carry_4 = OR(r4,carry_5)
s3 = XOR(r3,carry_4)
carry_3 = OR(r3,carry_4)
s2 = XOR(r2,carry_3)
carry_2 = OR(r2,carry_3)
s1 = XOR(r1,carry_2)
carry_1 = OR(r1,carry_2)
s0 = XOR(r0,carry_1)

n_r_sign = NOT(r_sign)
r0 = OR(AND(n_r_sign,r0),AND(r_sign,s0))
r1 = OR(AND(n_r_sign,r1),AND(r_sign,s1))
r2 = OR(AND(n_r_sign,r2),AND(r_sign,s2))
r3 = OR(AND(n_r_sign,r3),AND(r_sign,s3))
r4 = OR(AND(n_r_sign,r4),AND(r_sign,s4))
r5 = OR(AND(n_r_sign,r5),AND(r_sign,s5))
r6 = OR(AND(n_r_sign,r6),AND(r_sign,s6))
r7 = OR(AND(n_r_sign,r7),AND(r_sign,s7))

h0 = r0
rest = h0
h1 = AND(r1,NOT(rest))
rest = OR(rest,h1)
h2 = AND(r2,NOT(rest))
rest = OR(rest,h2)
h3 = AND(r3,NOT(rest))
rest = OR(rest,h3)
h4 = AND(r4,NOT(rest))
rest = OR(rest,h4)
h5 = AND(r5,NOT(rest))
rest = OR(rest,h5)
h6 = AND(r6,NOT(rest))
rest = OR(rest,h6)
h7 = AND(r7,NOT(rest))

e0 = OR(h0,OR(h1,h2))
e1 = OR(h0,OR(h3,h4))
e2 = OR(h1,OR(h3,h5))

ne0 = NOT(e0)
ne1 = NOT(e1)
ne2 = NOT(e2)

e0e1 = AND(e0,e1)
e0ne1 = AND(e0,ne1)
ne0e1 = AND(ne0,e1)
ne0ne1 = AND(ne0,ne1)

x0 = AND(e0e1,  ne2)
x1 = AND(e0ne1, e2 )
x2 = AND(e0ne1, ne2)
x3 = AND(ne0e1, e2 )
x4 = AND(ne0e1, ne2)
x5 = AND(ne0ne1,e2 )
x6 = AND(ne0ne1,ne2)

u0 = AND(x0,r1)
u1 = AND(x1,r2)
u2 = AND(x2,r3)
u3 = AND(x3,r4)
u4 = AND(x4,r5)
u5 = AND(x5,r6)
u6 = AND(x6,r6)

v0 = AND(x0,r2)
v1 = AND(x1,r3)
v2 = AND(x2,r4)
v3 = AND(x3,r5)
v4 = AND(x4,r6)
v5 = AND(x5,r7)
v6 = AND(x6,r7)

f0 = OR(u0,OR(u1,OR(u2,OR(u3,OR(u4,OR(u5,u6))))))
f1 = OR(v0,OR(v1,OR(v2,OR(v3,OR(v4,OR(v5,v6))))))
sign = XOR(a0,r_sign)

either_nan = OR(a_nan,b_nan)
either_inf = OR(a_inf,b_inf)
ans_nan = OR(AND(AND(a_inf,b_inf),XOR(a0,b0)),AND(NOT(either_inf),either_nan))
nans_nan = NOT(ans_nan)
ans_inf = AND(nans_nan,OR(either_nan,either_inf))
ans_none = AND(nans_nan,NOT(ans_inf))
nans_none = NOT(ans_none)

result0 = OR(OR(AND(a_inf,a0),AND(b_inf,b0)),AND(ans_none,sign))
result1 = OR( nans_none, AND(ans_none,e0) )
result2 = OR( nans_none, AND(ans_none,e1) )
result3 = OR( nans_none, AND(ans_none,e2) )
result4 = OR( ans_nan, AND(ans_none,f0) )
result5 = OR( ans_nan, AND(ans_none,f1) )

When it's finished, will it round "down" towards zero or towards negative infinity? Just curious.

@JoeZ. I will definitely try to make it round towards zero, and I think that doing so shouldn't be a problem, though I can't be sure without writing it out. It is obviously trivial to make this add two negative numbers (having them round towards zero) so I think it will probably be easier to stick with that.

Well done for coming up with a complete solution. There are some easy optimisations. OR(AND(w,x),AND(y,z)) is NAND(NAND(w,x),NAND(y,z)) saving 4, and you've used the first construction a few times; and your NaN treatment is slightly wrong because Inf + NaN should be NaN.

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/

sort: votes


text:
Shortest representation of an Underload number
Flavour text
The stack-based esolang Underload has some interesting ties to functional programming. One of them is its treatment of the numerical datatype—like the lambda calculus, you represent the natural number N by a function which perform an action N times.
To make things simple, we will only consider only the following subset of Underload commands:

: - This command duplicates the top item on the stack.
* - This command concatenates the top two items on the stack into a single item.

We define an Underload numeral N as a string of : and * which, when executed, consume the top item on the stack, and produce N copies of that item concatenated together. Some examples:

There are no Underload numerals 0, -1, 1/2, π.
The empty string  is the Underload numeral 1, because it leaves the stack untouched.
:* is the Underload numeral 2, because it duplicates the top item, and then concatenates those two copies together into a single item: (A):* = (A)(A)* = (AA).
::** is the Underload numeral 3: (A)::** = (A)(A):** = (A)(AA)* =
(AAA).
:::*** is the Underload numeral 4.
:*:* is also the Underload numeral 4: (A):*:* = (AA):* = (AA)(AA)* = (AAAA).

In general, you will find that, if M and N are the Underload numerals M and N, then :N* is the numeral N+1, and MN is the numeral M×N.
The challenge
Your task is to write the shortest program (taking input on STDIN) or function (taking input via argument) which produces the shortest representation of the Underload numeral for its input as a string. That is to say, if the input is a positive natural number N > 1, you must produce an Underload numeral N whose length in characters is less than or equal to that of every other Underload numeral N.
Sample inputs and outputs: ("Input - OUTPUT.")

1 - .
2 - :*.
5 - ::*:** (2×2+1).
7 - ::*::*** (2×3+1) or :::**:** (3×2+1).
33 - ::*:*:*:*:** (2×2×2×2×2+1).
49 - ::*:*:*:*::*** (16×3+1, length 14) but not ::*::***::*::*** (7×7, length 16).

If the input is not a positive natural number, you are free to return an error, produce undefined behaviour, or even fail to terminate. An explanation of your submission's method of finding the answer is appreciated.
Standard loophole restrictions apply: no extra input, no web requests, output/return value must be exactly the answer and not an infinite random stream of : and *, etc.

@Geobits I've said nothing about execution time, so as long as you can prove it'll give the correct answer eventually, you're good.

This problem relates to addition chains; specifically, the length of the correct answer for input x is 2*A117498(x) where A117498 gives the optimal combination of binary and factor methods for finding an addition chain.

GolfScript (61 60 55 54 53 chars)
~:X'']({:A{.'.+'\*A{2$+}%~}%}*{,}${1\~X=}?{44/'*:'=}%

This is less tricky than my earlier version and takes a slightly different approach, but it's still brute force. We know that ':'X*'*'X*+ is a candidate solution, so if we generate all well-balanced strings up to that length and take the shortest one which evaluates to the right thing we can be certain to find one.
# Evaluate input and store the target number in X
~:X
# Seed the generator with the empty string
'']
# X times...
({
    # Store the array of strings so far into A
    :A
    # Generate A' by mapping each element
    {
        # Dup: this leaves an untouched copy of the current string
        .
        # Wrap the duplicate in .+
        '.+'\*
        # For each element in A, generate that element suffixed with the current string
        A{2$+}%~
    }%
}*
# Order by length
{,}$
# Find the first element which evaluates to X
{1\~X=}?
# tr .+ :*
{44/'*:'=}%

Thanks to Howard, from whose solution I've stolen a couple of 1-char tweaks.

Haha, an input of 3 takes more then three seconds to execute on the web interpreter. Golfing at its finest.

@algorithmshark, you can speed it up quite a bit with a spot of deduplication. Insert .& just after the inner loop (i.e. between ~}% and }*.

Python 2.7 - 87 84 92
u=lambda n:n>1and min([u(i)+u(n/i)for i in range(2,n)if n%i<1]+[':'+u(n-1)+'*'],key=len)or''

Explanation:
This is a pretty straightforward solution. It recursively tests all possible representations of n as either the product of two numbers or as :(n-1)*, and then finds the minimum length solution. range(2,n) is necessary so that the recursion has bounded depth, and n<2 gives the base case.
Notes:
i and n/i are the two factors of n. The ... and ... or ... replacement for ... if ... else ... doesn't work because '' evaluates to false. min of strings gives one of the shortest strings. Python 2.7 saves 1 character by using / instead of //.
Edit: Moved the base case to the back of the expression, allowing me to use ... and ... or ... and shave a couple spaces.
Test cases:
u(1)
''
u(5)
'::*:**'
u(49)
'::*:*:*:*::***'

"min of strings gives one of the shortest strings" isn't true unless you supply the optional argument key=len. It's gives the lexicographically earliest string. (Example). Since '*' < ':' this means that you have a bias towards solutions involving powers of 2, but are they always the shortest?

Answer: actually the bias is more complicated, but it doesn't always give the correct answer. The smallest counterexample is u(33), for which sorting lexicographically gives the 14-char ::**::*::*:*** but sorting by length gives the 12-char ::*:*:*:*:**

I never knew that about Python string comparisons. I have updated my answer.

GolfScript (54 53 chars)
This is an approach which is in the spirit of Howard's (build strings which evaluate to the correct value and select the shortest, rather than brute force through candidate strings to find those which evaluate to the correct value), but is sufficiently different that I think it belongs in a separate answer.
~.''':*':s@,{):x,2>{:^~$x^/~$+{s\*}x^%*}%{,}$0=}/]((=

Online demo not available because it runs a buggy version of the interpreter.
# Let <N> denote the string which evaluates to N
# We want to enter the main loop with three values on the stack: <0> <1> <2>
# However, we'll never use <0>, so we can actually replace that with any value at all.
# Getting the input from underneath 3 items would normally use two stack manipulations.
# Trick: let's use the input value for <0>! (This gives a further bonus later).
# NB We store the value of <2> in the variable s
~.''':*':s@
# for x=1 to input_value ...
,{):x
    # for ^=2 to x-1 ...
    ,2>{:^
        # Use negative stack offsets to index the stack from the start
        # I.e. -1$ gets the first item on the stack, which is <0>
        # -2$ gets the second item on the stack, which is <1>
        # In general, val~$ gets <val>
        ~$x^/~$+
        # We have the string <^><x / ^> on the stack.
        # Increment it (x % ^) times to get a candidate <x>.
        {s\*}x^%*
    }%
    # Select a shortest string.
    {,}$0=
}/
# Group the stack into one array and select the appropriate offset,
# reusing that hacky <0> substitute for the offset.
]((=

It would be possible to shave one by replacing 3+ with ) (exploiting the fact that []0= leaves nothing on the stack) if it weren't that []2> leads to an error.

[]2> yields [] without error.

@Howard, ah, golfscript.apphb.com must be running an old version. But it turns out that I was wrong, because that replacement leads to getting the wrong output for input '1'.

Which you can fix with ((= instead of -1=.

And golfscript.apphb.com indeed runs an old version, the nested loops example doesn't work.

@Howard, true, I'd experimented with that earlier but it didn't give a saving by itself.

So @w0lf prevents further enhancements ;-) Actually golfscript.apphb.com saved me several times when I was away from my computer but urgently needed to do some golfing.

I was experimenting with x^%.s*/* now for quite some time (which would save 1 char if it wasn't for the zero case). Anyways, you can get rid of the variable s and replace it with -3$ - just to get remove a variable ;-)

@Howard, I know, although I prefer 2~$ for consistency.

GolfScript, 63 58 56 characters
~n./\{:v~[':*'1$*v,,2>{v,\%!},{.v=v,@/v=+}/]{,}$0=]}*-2=

The code takes input on STDIN and prints the result.
Examples:
> 49
:::**:*:*:*:**

> 1234
::::*:*:*:**:*:*:**::**::***

You can test your own cases online.

Wow, I thought that a factoring based approach would be quite a bit longer than a brute force approach.

@PeterTaylor I thought so too but it turned out that it is not the case. Moreover, my brute force solution was a bit longer than yours ;-)

Would you mind explaining what each portion does? I can only follow up until the :x(= bit. Also, +1 for being able to run 49 in a reasonable amount of time.

@algorithmshark I'm still working on the solution so it might still change a lot (as it just did). Mainly, the part x,2>{x\%!}, gives all true divisors of x, {.v=x@/v=+}/ then concatenates the solutions for d and x/d for all divisors d. {,}$ sorts them by length and 0= takes the shortest of them (plus the initial :(x-1)* case).

Brachylog 2, 30 (maybe eventually 26) bytes, language postdates challenge
Here's a function which works with the current Brachylog 2 implementation (and returns a list of character codes because the current implementation is having some issues with string handling):
∧.l∧?{ḋp~c×ᵐ{-₁↰₁:[42,58]c↻}ᵐc}

Try it online!
The language is still very new. Here's a 26 byte version of the program that should work according to the specification, but uses some unimplemented features, and thus isn't valid yet, but maybe will be in future (it's also even less efficient):
{ḋp~c×ᵐ{-₁↰₁:"*:"c↻}ᵐc}ᶠlᵒh

Explanation
∧.l∧?{ḋp~c×ᵐ{-₁↰₁:[42,58]c↻}ᵐc}
∧.l∧?                            Evaluation hint: try shortest outputs first
     {                        }  Define an inner function
      ḋ                          Prime factor decomposition of the input
       p                         Find a permutation
        ~c                       Find an inverse concatenation (i.e. partition)
          ×ᵐ                     Take the product of each set inside the partition
      ḋp~c×ᵐ                     Find a decomposition into factors ≥ 2
            {              }ᵐ    For each of those factors:
             -₁                  Decrement it
               ↰₁                Call the inner function recursively
                 :[42,58]c       Append "*:" (as character codes)
                          ↻      Move the last element to the start
                             c   Append the results together

The basic idea is fairly simple: we alternate between decomposing the number into (1 or more) factors (not necessarily prime factors, but factors of 1 are not allowed), and expressing each of those as 1 + (a representation obtained from a recursive call). This is guaranteed to search all possible Underload representations of the number (we can apply a multiplication stage "twice in a row" by multiplying together more than 2 numbers, and an increment stage twice in a row via separating them with a multiplication stage that multiplies together just one number). We don't need an explicit base case, because decomposing 1 into prime factors gives us an empty list, and thus we construct it with a multiplication stage that multiplies zero numbers together.
The program is fairly inefficient, especially because the evaluation order hint I gave (generate answers shortest to longest in terms of the size of the eventual output), while solving the "shortest" part of the question, isn't that great in terms of actually making the program complete quickly (a much more useful hint would be "generate only the shortest answer at each recursive stage", but that takes more bytes…). Additionally, ḋp~c×ᵐ can generate multiplicative partitions several times each, making the program do a lot of redundant work.

J - 81 char
For posterity, this was the best that I could do in J.
_2{::(0&(][,0{<@;"1@({~#(#~]-:"1<.)@(],.%)2}.i.@#)(/:#&>)@,':'<@,'*',~>@{:),~)&a:

We create a list of results, beginning with two empty strings (that's the ,~ and a:) representing 0 (never used) and 1, and then iterate a verb over them (sneaky use of hooks, trains and &) that appends the next number's shortest representation.
The actual verb that we iterate uses the length of the list as an indicator of what number we are operating on. First, we factor this number into pairs of factors (#(#~]-:"1<.)@(],.%)2}.i.@#), and retrieve each pair by pulling from the array ({~). We turn each of those pairs (there could be 0 of them if the number is prime) into single strings (<@;"1).
Then we append to that list the entry for the previous result bracketed by : and *, and sort this list by length ((/:#&>)). Finally, we take the first result from this list (0{) and append that to the end of the base array ([,). When the loop is done iterating, we have a list of length 2 more than the input, starting at 0. So what we have to return is the next-to-last string (_2{::).
   un =: _2{::(0&(][,0{<@;"1@({~#(#~]-:"1<.)@(],.%)2}.i.@#)(/:#&>)@,':'<@,'*',~>@{:),~)&a:
   un 49
::*:*:*:*::***
   un 1234
:*::*:*:*::*::***::*::*:****
   un 10010
:*::*:*::***::*:*:*:*:*:*:*::***
   2 * (1 + 3 * 2^2) * (1 + 3 * 2^7)
10010
   6!:2 'un 10010'   NB. time in seconds
19.5539

Jelly, 33 bytes, language postdates challenge
ÆḌḊµ⁹÷Ñ;Ñð€
’ß”:;;”*W;ÇLÞḢµ“”>1$?

Try it online!
A straightforward brute force solution.
Explanation
Main program
’ß”:;;”*W;ÇLÞḢµ“”>1$?
              µ  >1$?  If input is greater than 1, then:
’ß                       Run recursively on the input - 1
  ”:;                    Prepend a colon
     ;”*                 Append an asterisk
        W;               Cons to:
          Ç                the result of the helper, on {the original input}
           LÞ            Sort by length
             Ḣ           Take the first (i.e. shortest) result
               “”      Otherwise, return an empty string

The main program uses the helper function to enumerate all possible ways to produce the value via multiplication, then tries producing the value by addition, and returns the shortest possibility. It also handles the base case (an input of 1).
Helper function
ÆḌḊµ⁹÷Ñ;Ñð€
ÆḌ µ     ð€            For all proper factors of the input
  Ḋ                    except the first (i.e. 1):
    ⁹÷                   Divide it into the input;
      Ñ                  Run the main program on it;
       ;                 Append the result of:
        Ñ                  the main program run on {the factor}

The helper function tries all possible methods of expressing the input as a multiplication of two numbers, and mutual-recursively calls the main program in order to get at their shortest representations.

GNU Prolog, 96 bytes
v(N)-->{N#=1};{N#=A*B,A#<B,B#<N},v(A),v(B);{N#=M+1},":",v(M),"*".
s(N,S):-length(S,_),v(N,S,[]).

The first line is a grammar that implements Underload evaluation, and works in the reverse direction (actually, it doesn't quite work in the forward direction due to the A#<B constraint; change this to A#<N for a slower program that works both ways round). The second line defines the function-like predicate s (which is the function that's implemented as a solution to this program) which finds the shortest possible string which evaluates to the number given as input (this is frustratingly verbose for what's a relatively simple task, but that's Prolog for you…).
The program should be pretty self-explanatory, given that it's more or less a direct translation of the specification to a grammar, and then to Prolog syntax; the definition of v says that N is 1 in an empty string, or N is A × B (with A less than B less than N) and the string is the concatenation of v(A) and v(B), or N is M + 1 and the string is : concatenated with v(M) concatenated with *. The second line is a bit subtler; length(S,_) means "S has some length", but specifying this as the first thing on the line acts as a hint to the Prolog implementation that it should check the shortest lengths first (which means we'll get the shortest possible length for a return value).