Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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

share|improve this question
2  
I like this context-specific ascii-art trend. :) –  Adam Maras Jan 22 at 17:53
2  
In order to be a fair code-golf I think you should give a more restrictive definition of the output. –  Howard Jan 22 at 18:16
2  
I'm waiting for someone to submit a biologically accurate looking heart. Whoever does that immediately gets a +1 from me. –  Joe Jan 23 at 16:05
2  
@JoeStead Your notions about romantic attributes of Valentines Day look suspicious :D –  VisioN Jan 23 at 16:25
2  
This would've much been better as a popularity-contest –  SztupY Jan 29 at 16:40
show 5 more comments

19 Answers

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

share|improve this answer
2  
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 :) –  VisioN Jan 23 at 11:57
3  
!!!Awesome job!!! –  SpYk3HH Jan 23 at 15:04
1  
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 Jan 23 at 15:19
    
@Florent Are you talking about CoffeeScript? –  VisioN Jan 23 at 15:21
2  
No. EcmaScript 6. –  Florent Jan 23 at 15:21
show 4 more comments

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♥
               ♥
share|improve this answer
add comment

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--------
-----------------
share|improve this answer
4  
Nice job putting V at the bottom. –  luser droog Jan 22 at 20:16
1  
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): ideone.com/ELnkEE –  reima Jan 23 at 1:09
    
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 :) –  Yimin Rong Jan 23 at 18:35
add comment

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
share|improve this answer
    
I like your approach ^_^ –  Navin Jan 29 at 1:52
add comment

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.

share|improve this answer
    
+1 for the flamboyant heart-shaped code :D –  epidemian Feb 14 at 17:46
    
@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 –  KChaloux Feb 14 at 18:05
add comment

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
share|improve this answer
    
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. pastebin.com/i1TSEZfE –  manatwork Jan 22 at 19:28
    
@manatwork Thank you. Edited the body now down to 121 chars :-) –  Wasi Jan 22 at 19:36
    
Are you counting the three vertical loves on the right side that you got by staggering the horizontal loves? –  Joshua Taylor Jan 29 at 20:42
    
@JoshuaTaylor Thanks, I didn't notice those vertical loves. Including those the love count will be 23 :D –  Wasi Jan 30 at 10:30
add comment

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.

share|improve this answer
    
your second program throws an error: "syntax error at -e line 1, near "/\D/for3h112a05e0n1l2j4f6b9" (tried to invoke from command line using -e) –  Tomas Jan 31 at 17:40
    
From command line, you will need to replace $" with ' ': perl -e "print' 'x(15&ord),Love,$/x/\D/for'3h112a05e0n1l2j4f6b9'=~/./g" –  primo Jan 31 at 17:46
    
OK, nice trick! –  Tomas Jan 31 at 17:50
    
The 36 byte version doesn't actually print the required output –  Tobia Feb 1 at 13:20
    
@Tobia It needs to be in exactly the shape shown, which is actually 61 bytes, as mentioned. –  primo Feb 2 at 0:05
add comment

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

share|improve this answer
1  
You could shorten your code by removing the 0 before floating numbers and assigning Math.abs to a variable. –  Florent Jan 24 at 9:48
    
Also you may want to remove two useless semicolons from the end to save another couple of bytes. –  VisioN Jan 29 at 8:11
add comment

JavaScript - 136 121 115 113 characters

s="";for(k=800;k--;)
x=1.25-k%40/16,y=k/320-1.25,
s+=Math.pow(x*x+y*y-1,3)<x*x*y*y*y
?"Love"[k%4]:39==k%40?"\n":" ";s

To run : copy paste into browser console (eg : Chrome or Firefox)

share|improve this answer
    
["L","o","v","e"] => "Love" will save 11 bytes. –  VisioN Jan 23 at 11:49
    
Fixed :). Thanks for suggestion. –  tigrou Jan 23 at 12:04
add comment

C, 116 chars

(I don't know if this is heart-shaped enough... fills the inside of three circles to produce the output.)

i = 192, x, y;
main(t) {
  for (; i--; putchar(i % 16? y : 10))
    y = i / 16 - 8,
    x = i % 16 - 8,
    t = x*x + y*y,
    y = " Levo"[ (t < 64 & y < 0 | t < 8*abs(x)) * (i % 4 + 1) ];
}

Output:

 veLov   veLov 
oveLove oveLove
oveLove oveLove
oveLove oveLove
oveLoveLoveLove
oveLoveLoveLove
oveLoveLoveLove
 veLoveLoveLov 
 veLoveLoveLov 
  eLoveLoveLo  
    oveLove    

Earlier in the golfing process, before replacing constant expressions (change M to adjust size):

#define M 4

i = 3*M * 4*M, x, y;
main(t) {
  for (; i--; putchar(i % (4*M)? x : '\n')) {
    y = i / (4*M) - 2*M, x = i % (4*M) - 2*M,
    t = x*x + y*y, x = " Levo"[ (t < 4*M*M & y < 0 | t < abs(2*M*x)) * (1 + i % 4) ];
  }
}

I also felt compelled to do this. :P

#define F for
#define M main

 /*##     ####
####*/i  =192,x
,y;M(t) {F(;i--
;putchar(i %16?
y:10))y=i/16-8,
x=i%16-8,t=x*x+
y*y,y=" Levo"[(
 t<64&y<0|t<8*
  abs(x))*(i%
    4+1)];}
share|improve this answer
    
What kind of mystic options/compiler should you use to compile it ? All variables in C are typed en.wikibooks.org/wiki/C_Programming/Variables –  Micka Jan 24 at 19:07
1  
@Micka global variables are implicitly declared as int, a legacy feature borrowed from pre-ANSI-C. It compiles just fine for me with clang foo.c or gcc -std=c99 foo.c (though, with plenty of warnings). Do you get any errors? I don't know C well enough to gurantee that I don't depend on undefined behaviour, but I believe I don't do that. –  FireFly Jan 24 at 19:30
1  
Hrm, apparently it's not sanctioned by any of the standards--I thought it was legal in C90 at least, but alas, it isn't. So it's just a legacy thing left from pre-ANSI-C that most compilers support anyway (see this footnote on wikipedia e.g.). –  FireFly Jan 24 at 19:53
add comment

Ruby, 47, or Golfscript, 41

Boring answer.

puts" Love  Love
"+"LoveLoveLove
"*6+"    Love"

Golfscript version:

" Love  Love
""LoveLoveLove
"6*"    Love"

Output:

 Love  Love
LoveLoveLove
LoveLoveLove
LoveLoveLove
LoveLoveLove
LoveLoveLove
LoveLoveLove
    Love
share|improve this answer
23  
Lol, what an ugly heart! :D –  Abbas Jan 22 at 18:48
add comment

Ruby - 113 Characters

l=->a,b=28,c=1{puts (("Love"*a).center(b))*c};l.call(2,14,2);l.call(3,14,2);[7,7,7,6,5,4,3,2,1].map{|x|l.call(x)}

Output:

1.9.3p448 :811 > l=->a,b=28,c=1{puts (("Love"*a).center(b))*c};l.call(2,14,2);l.call(3,14,2);[7,7,7,6,5,4,3,2,1].map{|x|l.call(x)}
   LoveLove      LoveLove   
 LoveLoveLove  LoveLoveLove 
LoveLoveLoveLoveLoveLoveLove
LoveLoveLoveLoveLoveLoveLove
LoveLoveLoveLoveLoveLoveLove
  LoveLoveLoveLoveLoveLove  
    LoveLoveLoveLoveLove    
      LoveLoveLoveLove      
        LoveLoveLove        
          LoveLove          
            Love            

To be more clear

enter image description here

share|improve this answer
add comment

Perl - 159 characters

Not massively golfed...

printf"%-14s%14s\n",$_,~~reverse for map{($.,$i,$c)=split',';($"x$i).($.x$c)}qw(L,5,3 o,3,8 v,1,11 e,0,13 L,1,13 o,3,11 v,5,9 e,7,7 L,8,6 o,10,4 v,12,2 e,13,1)

Here's the same with added whitespace for slightly better readability...

printf "%-14s%14s\n", $_, ~~reverse
   for map {
      ($.,$i,$c) = split',';
      ($"x$i).($.x$c)
   } qw(
      L,5,3
      o,3,8
      v,1,11
      e,0,13
      L,1,13
      o,3,11
      v,5,9
      e,7,7
      L,8,6
      o,10,4
      v,12,2
      e,13,1
   )

Output is...

     LLL            LLL     
   oooooooo      oooooooo   
 vvvvvvvvvvv    vvvvvvvvvvv 
eeeeeeeeeeeee  eeeeeeeeeeeee
 LLLLLLLLLLLLLLLLLLLLLLLLLL 
   oooooooooooooooooooooo   
     vvvvvvvvvvvvvvvvvv     
       eeeeeeeeeeeeee       
        LLLLLLLLLLLL        
          oooooooo          
            vvvv            
             ee             

The complete word "Love" is contained within (vertically) 22 times, plus numerous partials.

share|improve this answer
    
+1 for creativity. I wouldn't have thought to make it vertical. –  Andrew G Jan 23 at 15:55
add comment

Sclipting — 28 chars / 56 bytes

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

Output:

    LOVE    LOVE
  LOVELOVELOVELOVE
LOVELOVELOVELOVELOVE
  LOVELOVELOVELOVE
    LOVELOVELOVE
      LOVELOVE
        LOVE
share|improve this answer
    
(Or in 24 characters: 긢꼙겱딧꽔밂各감啃갰啃긂밀⓶復냄뭖끐⓷復겠⓸復終 — but this uses the instruction, which I added after this challenge was posted, so it doesn’t count.) –  Timwi Jan 23 at 19:28
    
This is 84 bytes. –  tobyink Jan 23 at 21:08
    
No, it’s 56 bytes as UTF-16. –  Timwi Jan 23 at 21:13
    
@Timwi for me it measures 58 bytes. Odd. –  primo Jan 28 at 23:54
    
@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 Jan 30 at 14:03
show 1 more comment

Wolfram Language (Mathematica) - 111

i=0;MatrixForm@Table[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

share|improve this answer
add comment

Perl - 97 characters

This answer is based on @Wasi's 121 character Python solution.

$.=love;say"   $.    $.$/$. $.$. $.$/$.   $.   $.";say$"x$_,$.,$"x(9-$_*2),$.for 0..4;say$"x 6,$.

You need to run perl with the -M5.010 option to enable 5.10-specific features. This is apparently allowed.

Interesting features:

  • I use the variable $. to store the word "love". This is because it can be immediately followed by another word if necessary. $.for is unambiguously tokenized as $. followed by for; $_for would not be tokenized as $_ followed by for because $_for is itself a legal variable name.
  • $", a built-in variable representing that character that arrays will be joined with when interpolated into a string (and defaults to a single space character) is used instead of " " for savings of one character here and there.
  • Perl's string interpolation beats the python .replace method significantly in code golfing.
  • Perl's for 0..4 similarly wins over for i in range(5)
share|improve this answer
add comment

APL, 36 chars / bytes*

7 16⍴∊'  ' 'Love'[20400948744⊤⍨36/2]

Must be evaluated with ⎕IO←0 (the default varies by implementation)

*APL can be written in a single-byte charset if needed (as long as you don't use any other Unicode character) so N chars = N bytes for the purpose of counting.

Output:

Contains exactly 20 "Love"

  Love    Love
LoveLoveLoveLove
LoveLoveLoveLove
LoveLoveLoveLove
  LoveLoveLove
    LoveLove
      Love
share|improve this answer
add comment

C# 224

class P{static void Main(){for(int i=0,m=1;i<30;i++)for(int l=0;l<new[]{5,6,7,6,8,10,3,10,4,13,1,13,1,87,1,27,4,23,7,20,11,16,16,11,20,7,24,3,27,1}[i];l++,m++)System.Console.Write((i%2>0?"love"[m%4]:' ')+(m%29>0?"":"\n"));}}

Formatted:

class P
{
    static void Main()
    {
        for (int i = 0, m = 1; i < 30; i++)
            for (int l = 0; l < new[] { 5, 6, 7, 6, 8, 10, 3, 10, 4, 13, 1, 13, 1, 87, 1, 27, 4, 23, 7, 20, 11, 16, 16, 11, 20, 7, 24, 3, 27, 1 }[i]; l++, m++)
                System.Console.Write((i % 2 > 0 ? "love"[m % 4] : ' ') + (m % 29 > 0 ? "" : "\n"));
    }
}

Output:

     velove       elovel
   ovelovelov   velovelove
 lovelovelovel velovelovelov
lovelovelovelovelovelovelovel
ovelovelovelovelovelovelovelo
velovelovelovelovelovelovelov
 lovelovelovelovelovelovelov
   elovelovelovelovelovelo
    ovelovelovelovelovel
      lovelovelovelove
         lovelovelov
           elovelo
             vel
              l
share|improve this answer
add comment

Extended BrainFuck : 193 (counted without non essential whitespace)

    {h]<[<]<        [<}>>-->
-3>->-3>+9>+9>+>-7>+>->-5>+>->->
-3>+>+>>4->->6+>5+>>4+[-<4+>]<[-
<4+<5+<5+<5+4>&h++]>[-[-[<10+.[-]
    >-[[-]3<[-]]>[<4+3<[[->>
    +<<]<]]]>[[>]>[.>&h]<<++
        >]]>[<<4+[->8+<]
        >....[-]<<+>>]>]

Turns into:

Brainfuck: 264 (counted without non essential whitespace)

    >>-->->>        >->->>>+
    >>>>>>>>        >+>>>>>>
>>>+>->>>>>>>+>->->>>>>+>->->->>>
+>+>>---->->++++++>+++++>>++++[-<
++++>]<[-<++++<+++++<+++++<+++++>
>>>]<[<]<[<++]>[-[-[<++++++++++.[
    -]>-[[-]<<<[-]]>[<++++<<<
    [[->>+<<]<]]]>[[>]>[.>]<[
        <]<[<]<<++>]]>[<<
        ++++[->++++++++<]
            >....[-]<
             <+>>]>]

You run it with any bf interpreter. Ubuntu has bf and beef and both works nicely.

bf heart.bf

The output (344 bytes):

    LOVELOVE        LOVELOVE
    LOVELOVE        LOVELOVE
LOVELOVELOVELOVELOVELOVELOVELOVE
LOVELOVELOVELOVELOVELOVELOVELOVE
LOVELOVELOVELOVELOVELOVELOVELOVE
LOVELOVELOVELOVELOVELOVELOVELOVE
    LOVELOVELOVELOVELOVELOVE
    LOVELOVELOVELOVELOVELOVE
        LOVELOVELOVELOVE
        LOVELOVELOVELOVE
            LOVELOVE
            LOVELOVE

Ungolfed EBF code:

>>--> ; mark

;; ##  ##@   
;;########@
;;########@
;; ######@
;;  ####@
;;   ##@
;; the block below is the art above 
;; where space is replaced with ->
;; # with > and @ with +>
->>>->->>>+>
>>>>>>>>+>
>>>>>>>>+>
->>>>>>>+>
->->>>>>+>
->->->>>+>+>

;; we store the string LOVE after a blank
>
~"LOVE"<[<]<
[<++] ;; add 2 to every cell until mark

;; Variables
:new
:zero
:in
:next

@zero
$in(
  -[ ; 1
     -[ ; 2
        $zero 10+.[-]
        $in-[#[-]$new<[-] @in] ; its second round lf
        $next[#$in++++$new<[[->>+<<]<]]@new     ; 2   
      ]
      >[[>]>[.>]<[<]<[<]<<++>   ]@new
  ]>[@next $zero 4+(-$in 8+) $in.... (-) $new+ $in]@zero
)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.