Draw the Heart Shape

  • Challenge


    Draw a Heart shape


    enter image description here


    ...as ASCII art!


    Your art doesn't have to look exactly like mine, but it has to look like a Heart Shape.


    The inside of of the heart has to contain the words "Love" at least 20 times


    Rules



    • The program must write the art to the console.

    • Shortest code (in bytes, any language) wins.


    The winner will be chosen on February 14th on Valentines Day


    I like this context-specific [tag:ascii-art] trend. :)

    In order to be a fair code-golf I think you should give a more restrictive definition of the output.

    I'm waiting for someone to submit a biologically accurate looking heart. Whoever does that immediately gets a +1 from me.

    @VisioN you've uploaded an image, not submitted an entry to this, it doesn't count ;)

    @JoeStead Your notions about romantic attributes of Valentines Day look suspicious `:D`

    Why bytes? Usually it’s *characters* that count.

    Couldn't you just wait until Feb 14, man.

    @Timwi, that's not what the tag wiki for [tag:code-golf] says.

    This would've much been better as a `popularity-contest`

  • JavaScript [160 bytes]



    The following code seems to be 160 bytes unformatted.



       ('l2v2l6v2'+  'e1l1v3l2'+
    'v3e1v7e1v7e1v7e1l2v6e1l4v5'+
    'e1l6v4e1l8v3e1l7l3v2e1l9l3v1')
    .replace(/[lve]\d/g,function
    (c){return Array(-~c[1]).
    join({l:' ',v:'Love'
    ,e:'\n'}[c[0
    ]])})


    Simply run this in the browser console (e.g. in Firebug or Chrome Dev Tools).


    Changing `/[lve]\d/` to `/../` and using array `['','Love','\n']` instead of object will save another 10 bytes leading to **150 bytes** in total, but the source heart won't be that well shaped anymore `:)`

    !!!Awesome job!!!

    Using fat arrow function will save a lot of bytes. Replace `function(c){return` by `c=>` and remove `}`. You should post a shorter version!

    @Florent Are you talking about CoffeeScript?

    No. EcmaScript 6.

    @Florent Ah! As far as I know ECMAScript 6 is still experimental and unfortunately it is still very hard to test how it works. Lets assume my code is ECMAScript 5 compatible `:)` But thank you for pointing out! I hope soon JavaScript will compete with Perl and Python in golfing matters and everyone will be able to try that out.

    The latest Firefox versions already support ES6, and plus the `let` keyword for true local variables

    I'm always skeptical when people claim javascript IS a language... but that's really nice

  • GolfScript: 62 57 54 characters



    4 1.5\.5,+{.5\-\2*\0.}/]4/{[32'LOVE']2*]zip{(*}%''+}%~


    Output:



        LOVE    LOVE
    LOVELOVELOVELOVE
    LOVELOVELOVELOVELOVE
    LOVELOVELOVELOVE
    LOVELOVELOVE
    LOVELOVE
    LOVE


    Or, for some added love, and the obligatory abuse of whitespace insignificance (for 84 characters):



        5 1       .8\
    .)...5 ,{.5\-\3
    *\0.}/]4 /{[32[9829
    :x.'LOVE'\]''+]2
    *[@;]zip{(*}%
    ''+}%~' '15
    *x[]+
    +


    Output:



         ♥LOVE♥        ♥LOVE♥
    ♥LOVE♥♥LOVE♥ ♥LOVE♥♥LOVE♥
    ♥LOVE♥♥LOVE♥♥LOVE♥♥LOVE♥♥LOVE♥
    ♥LOVE♥♥LOVE♥♥LOVE♥♥LOVE♥
    ♥LOVE♥♥LOVE♥♥LOVE♥
    ♥LOVE♥♥LOVE♥
    ♥LOVE♥


  • C - 183 bytes


    Not a winner, but a whole lotta love. Can you figure out how it works?


    #include <stdio.h>
    #define C(a) ((a)*(a)*(a))
    int main(){int x,y;for(y=9;y>-6;y--){for(x=-8;x<9;x++)putchar(C(x*x+y*y-25)<25*x*x*y*y*y?"LOVE"[(x+10)%4]:'-');putchar('\n');}return 0;}

    Output:


    -----------------
    --LOVE-----OVEL--
    -ELOVEL---LOVELO-
    -ELOVELO-ELOVELO-
    -ELOVELO-ELOVELO-
    -ELOVELOVELOVELO-
    --LOVELOVELOVEL--
    --LOVELOVELOVEL--
    ---OVELOVELOVE---
    ----VELOVELOV----
    ----VELOVELOV----
    ------LOVEL------
    -------OVE-------
    --------V--------
    -----------------

    Nice job putting `V` at the bottom.

    Nice use of the heart curve! You can still save a couple of characters and get down to 153 (I also changed the output slightly at the cost of one added byte): http://ideone.com/ELnkEE

    Very cool. I've never been great at REALLY squeezing bytes. You should post it as your own `C`! The heart curve is public domain :)

  • Python, 210 characters



    Of course, this won't win because it is a code golf, but I wanted to be creative and I have not used the word Love in my source code:



    import gzip
    print(gzip.decompress(b'\x1f\x8b\x08\x00\x95\x10\xe0R\x02\xffSPP\xf0\xc9/KU\x80\x03\x10\x8f\x0bB\xa1c.l\x82dJ\xe0\xb0\x01\xe6\x02\x0cATa.T\xf7\x02\x00\xd9\x91g\x05\xc5\x00\x00\x00').decode('ascii'))


    This is the output:



       Love          Love
    LoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLove
    LoveLoveLoveLove
    LoveLoveLove
    Love

    I like your approach ^_^

    Using gzip is not really creative...

  • Scala - 273 Characters



    Well, I certainly don't expect to win for brevity, but I wanted to see if I could do it in Scala. A smarter golfer could probably shave several bytes off, but here's what I got:



    type D=Double;def w(x:D,y:D,a:D)={val(i,j)=(x-a,y-8);Math.sqrt(i*i+j*j)< 8};val l:Stream[Char]="love".toStream#:::l;val c=l.toIterator;def p(b:Boolean)=print(if(b)c.next else' ');for(y<-0 to 24){for(x<-0 to 32){if(y>7)p((16-x).abs< 24-y)else p(w(x,y,8)|w(x,y,24))};println}


    Or, if you prefer (still valid code!)



      type D=      Double 
    def w(x:D, y:D,a:D)=
    {val(i,j)= (x-a,y -8);
    Math.sqrt(i* i+j*j)< 8};
    val l : Stream [Char] =
    "love".toStream#:::l;val
    c= l .toIterator;def p
    (b:Boolean) =print(if
    (b)c.next else' ');
    for (y <-0 to 24)
    { for (x<- 0 to
    32){if(y >7)
    p((16-x).
    abs <
    24-y)
    else
    p(w
    (x,
    y,
    8
    )
    |
    w(
    x,
    y,
    24)
    )}
    println}


    Prints out two semicircles and a triangle to the screen, making a pretty decent facsimile of a heart.Picture of heart.scala output



    Needs to be run with the scala interpreter (compiling would require adding some extra cruft for object Main { def main(args: Array[String]) = { ... } } and I'm just havin' none of that.


    @epidemian I probably could've made it look more like a normal heart, but I misjudged the size of the code and was too lazy. So I made a tail instead. I like to pretend that it's a balloon :p

  • Python 2, 117



    prints exactly 20 loves horizontally.



    x="love";print"   x    x\nx xx x\nx   x   x".replace("x",x)
    for i in range(5):print" "*i+x+" "*(9-i*2),x
    print" "*6,x


    output:



       love    love
    love lovelove love
    love love love
    love love
    love love
    love love
    love love
    love love
    love

    Spare some characters by: use a single letter placeholder in the 2nd line's string then `replace()` them; in the 3rd line's `print` change one `+` to `,` and 10 with 9; in the last line use variable x's value. http://pastebin.com/i1TSEZfE

    @manatwork Thank you. Edited the body now down to 121 chars :-)

    Are you counting the three vertical `love`s on the right side that you got by staggering the horizontal `love`s?

    @JoshuaTaylor Thanks, I didn't notice those vertical `love`s. Including those the `love` count will be 23 :D

  • Perl - 36 bytes





      open    0;s
    /\S.?/Lo.ve
    /ge,print
    , , for
    <0>


    Output:



      LoveLove    LoveLove
    LoveLoveLoveLoveLoveLove
    LoveLoveLoveLoveLove
    LoveLoveLoveLove
    LoveLove


    This is a bit of a cheat; it will print Love once for every two non-white space characters in the source code. With the required whitespace to make the heart shape, the code is 61 bytes in length: flattened it is only 36 bytes:



    open 0;s/\S.?/Lo.ve/ge,print,,for<0>





    Perl - 60 bytes



    print$"x(15&ord),Love,$/x/\D/for'3h112a05e0n1l2j4f6b9'=~/./g


    Outputs the following:



       Love        Love
    Love Love Love Love
    Love Love Love
    Love Love
    Love Love
    Love Love
    Love Love
    Love Love
    Love


    Exactly 20 Love.






    Brief Explaination

    by request




    • for'3h112a05e0n1l2j4f6b9'=~/./g

      This modifies the print statement, and iterates over each character. The regex /./ obviously matches a single character, and in a list context /./g will return a list of all characters in the string. A more common, but slightly longer way to write this would be for split//,'3h112a05e0n1l2j4f6b9'.

    • print$"x(15&ord),Love,$/x/\D/

      The special variable $" defaults to a space. The ordinal value of each character mod 16 stores the number spaces needed between each Love via string repetition (x). Finally, if the character is not a digit (/\D/), the value of $/, which defaults to "\n" is tacked on to the end.


    your second program throws an error: *"syntax error at -e line 1, near "/\D/for3h112a05e0n1l2j4f6b9"* (tried to invoke from command line using `-e`)

    From command line, you will need to replace `$"` with `' '`: `perl -e "print' 'x(15&ord),Love,$/x/\D/for'3h112a05e0n1l2j4f6b9'=~/./g"`

    OK, nice trick!

    The 36 byte version doesn't actually print the required output

    @Tobia It needs to be in exactly the shape shown, which is actually 61 bytes, as mentioned.

    Someone on SO asked about your solution (or others in Perl on this question). That user cannot comment yet. Would you care to add a bit more detailed, beginner-friendly explanation to your answer?

    @simbabque done.

  • Wolfram Language (Mathematica) - 111



    i=0;[email protected][If[(x^2+y^2-200)^3+10x^2y^3<0,{"L","O","V","E"}[[i++~Mod~4+1]],""],{y,-20,20},{x,-20,20}]


    enter image description here


  • Javascript - 147 141 137 133 characters



    with(Math){s="";for(k=800;k--;)
    x=abs(1.25-k%40/16),y=k/320-1.25,
    s+=.75>x+abs(y)|.5>sqrt(x*x-x+y*y-y+.5)
    ?"Love"[k%4]:39==k%40?"\n":" "}s


    Note : I posted another answer, but this one use different approach and heart has a different shape.



    How it works :



    enter image description here



    First, I render a diamond (equation is |x|+|y|) then, I combine two circles on the top. x values are mirrored (so only one circle is needed).


    You could shorten your code by removing the `0` before floating numbers and assigning `Math.abs` to a variable.

    Also you may want to remove two useless semicolons from the end to save another couple of bytes.

  • Sclipting — 28 chars / 56 bytes



    겤뙡늆굚넰밌各긂밀❷거雙復냄뭖끐❸갰右거雙復겠⓸걠右復終


    Output:



        LOVE    LOVE
    LOVELOVELOVELOVE
    LOVELOVELOVELOVELOVE
    LOVELOVELOVELOVE
    LOVELOVELOVE
    LOVELOVE
    LOVE

    (Or in 24 characters: `긢꼙겱딧꽔밂各감啃갰啃긂밀⓶復냄뭖끐⓷復겠⓸復終` — but this uses the `啃` instruction, which I added after this challenge was posted, so it doesn’t count.)

    This is 84 bytes.

    No, it’s 56 bytes as UTF-16.

    @Timwi for me it measures 58 bytes. Odd.

    @primo: It’s 56 bytes. If you saved it as “Unicode” in Windows Notepad, you get a 58 byte file that consists of the UTF-16 BOM (2 bytes) plus the 56-byte program.

    @Timwi That makes sense.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM