# Love in 245 Characters (code golf)

**URL:** <https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771>\
**Category:** Community\
**Created:** [February 14, 2019, 12:13am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771 "2019-02-14T00:13:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 14, 2019, 12:13am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/1 "2019-02-14T00:13:53Z")

</div>

I’ve found that in addition to everything else Julia excels at, it’s also a great language for [code golf](https://en.wikipedia.org/wiki/Code_golf), and came up with the below as practice. Thought I’d share it in case anyone finds it amusing, or share the same hobby. (It is of quite limited practical use.)

```julia
   0:2e-3:2π .|>d->(P=
 fill(5<<11,64 ,25);z=8cis(
d)sin(.46d);P[64,:].=10;for
r=0:98,c=0 :5^3 x,y=@.mod(2-
$reim((.016c-r/49im-1-im)z),
 4)-2;4-x^2>√2(y+.5-√√x^2)^
  2&&(P[c÷2+1,r÷4+1]|=Int(
    ")*,h08H¨"[4&4c+1+r&
      3])-40)end;print(
       "\e[H\e[1;31m",
         join(Char.(
            P)))
             );

```

Copy and paste into Julia REPL in a fast terminal (not Atom) with font supporting Unicode (not Windows command prompt).

> **One-line version (245 characters)**
>
> ```julia
> 0:2e-3:2π.|>d->((P=fill(5<<11,64,25))[64,:].=10;z=8cis(d)sin(.46d);for r=0:98,c=0:5^3 x,y=@.mod(2-$reim((.016c-r/49im-1-im)z),4)-2;√2(y+.5-√√x^2)^2<4-x^2&&(P[c÷2+1,r÷4+1]|=Int(")*,h08H¨"[1+r&3+4&4c])-40)end;print("\e[H\e[1;31m",join(Char.(P))));
> 
> ```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [February 14, 2019, 1:42am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/2 "2019-02-14T01:42:47Z")

</div>

This is a thing of beauty.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 14, 2019, 1:55am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/3 "2019-02-14T01:55:02Z")

</div>

💞  
would you post an elaborated, self-explanatory version of the same thing?

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 14, 2019, 7:35am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/4 "2019-02-14T07:35:25Z")

</div>

Here’s an expanded version with comments:

```julia
# rotation angle 'd' goes from 0 to 2π (one revolution)
0:2e-3:2π .|> d -> (
    # screen buffer, with empty braille characters and new lines
    (P = fill(5<<11,64,25))[64,:] .= 10;
    # transformation vector: cis = rotation, sin = zoom
    z = 8cis(d)sin(.46d);
    # loop over all pixels
    for r=0:98, c=0:5^3
        # (..)z = scale and rotate point, using complex numbers
        # reim = split complex number into x/y
        # mod = repeat pattern
        x,y = @.mod(2 - $reim((.016c-r/49im-1-im)z), 4) - 2;
        # test if point is within a heart shape
        √2(y+.5-√√x^2)^2 < 4-x^2 &&
            # set pixel, using braille characters
            (P[c÷2+1,r÷4+1] |= Int(")*,h08H¨"[1+r&3+4&4c]) - 40)
    end;
    # print buffer to screen
    print("\e[H\e[1;31m", join(Char.(P))));

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 14, 2019, 8:26am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/5 "2019-02-14T08:26:44Z")

</div>

Amazing! I just sent a video of the code and output to my wife.

> [@bennedich](#):
>
> (It is of quite limited practical use.)

You saved me having to buy a paper card 😃

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [February 14, 2019, 3:50pm UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/6 "2019-02-14T15:50:16Z")

</div>

The code is so nice that, with the permission of @bennedich, I have added it at the end of [The Julia Express](http://bogumilkaminski.pl/files/julia_express.pdf).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 14, 2019, 4:26pm UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/7 "2019-02-14T16:26:32Z")

</div>

Wow, I just ran this—so sweet (in both sense of the word)!

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [February 14, 2019, 10:45pm UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/8 "2019-02-14T22:45:27Z")

</div>

I showed this to people in my lab and they were finally convinced to start learning Julia.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 15, 2019, 8:51am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/9 "2019-02-15T08:51:40Z")

</div>

I love you now!

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [February 19, 2019, 1:42pm UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/10 "2019-02-19T13:42:13Z")

</div>

this is indeed quite nice.  
If you send it to your partner, you might also want to mention their name in the code. 🙂

```julia
          0:2e-3:2π .|>d->(P=
        fill(5<<11,64 ,25);z=8cis(
       d)sin(.46d);P[64,:].=10;for
       r=0:98,c=0 :5^3 x,y=@.mod(2-
       $reim((.016c-r/49im-1-im)z),
        4)-2;4-x^2>√2(y+.5-√√x^2)^
        (length("KATJA"[3:4]))&&
         (P[c÷2+1,r÷4+1]|=Int(
          ")*,h08H¨"[4&4c+1+r&
           3])-40)end;print(
             "\e[H\e[1;31m",
                join(Char.(
                  P)))
                   ) 

```

---

<div class="post-metadata">

**Author:** ![eckofoid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eckofoid/32/221638_2.png) [@eckofoid](https://discourse.julialang.org/u/eckofoid)\
**Post date:** [July 6, 2019, 11:15pm UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/11 "2019-07-06T23:15:23Z")

</div>

I really wanted this to work, but all I get are an infinite number of 63 character lines, each character an empty braille array. This happens on my MacBook with terminal app, emacs, and intelliJ IDEA, using Julia 1.0.1.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [July 7, 2019, 5:36am UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/12 "2019-07-07T05:36:49Z")

</div>

Hmm, some quick troubleshooting:

- Copy & paste again to ensure correctness
- Ensure that your font displays Braille characters:

```julia
julia> (x->print(Char(x%65==64 ? 10 : x-x÷65))).(10400:10658);
⠀⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿
⡀⡁⡂⡃⡄⡅⡆⡇⡈⡉⡊⡋⡌⡍⡎⡏⡐⡑⡒⡓⡔⡕⡖⡗⡘⡙⡚⡛⡜⡝⡞⡟⡠⡡⡢⡣⡤⡥⡦⡧⡨⡩⡪⡫⡬⡭⡮⡯⡰⡱⡲⡳⡴⡵⡶⡷⡸⡹⡺⡻⡼⡽⡾⡿
⢀⢁⢂⢃⢄⢅⢆⢇⢈⢉⢊⢋⢌⢍⢎⢏⢐⢑⢒⢓⢔⢕⢖⢗⢘⢙⢚⢛⢜⢝⢞⢟⢠⢡⢢⢣⢤⢥⢦⢧⢨⢩⢪⢫⢬⢭⢮⢯⢰⢱⢲⢳⢴⢵⢶⢷⢸⢹⢺⢻⢼⢽⢾⢿
⣀⣁⣂⣃⣄⣅⣆⣇⣈⣉⣊⣋⣌⣍⣎⣏⣐⣑⣒⣓⣔⣕⣖⣗⣘⣙⣚⣛⣜⣝⣞⣟⣠⣡⣢⣣⣤⣥⣦⣧⣨⣩⣪⣫⣬⣭⣮⣯⣰⣱⣲⣳⣴⣵⣶⣷⣸⣹⣺⣻⣼⣽⣾⣿

```

- Ensure that your terminal supports ANSI escape codes:

```julia
julia> print("abc\e[Gxxx")
xxx

```

---

<div class="post-metadata">

**Author:** ![eckofoid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eckofoid/32/221638_2.png) [@eckofoid](https://discourse.julialang.org/u/eckofoid)\
**Post date:** [July 8, 2019, 6:45pm UTC](https://discourse.julialang.org/t/love-in-245-characters-code-golf/20771/13 "2019-07-08T18:45:28Z")

</div>

Hi Max,

I did what I tell all my students to do, and rebooted the computer. Now works a treat!

Sorry to have wasted your time. It’s a lovely script which everyone should run!

Cheers,

Eric
