Love in 245 Characters (code golf)

I’ve found that in addition to everything else Julia excels at, it’s also a great language for 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.)

   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)
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))));
105 Likes

This is a thing of beauty.

3 Likes

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

1 Like

Here’s an expanded version with comments:

# 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))));
19 Likes

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

You saved me having to buy a paper card :smiley:

15 Likes

The code is so nice that, with the permission of @bennedich, I have added it at the end of The Julia Express.

7 Likes

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

4 Likes

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

21 Likes

I love you now!

1 Like

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

          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)))
                   ) 
5 Likes

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.

Hmm, some quick troubleshooting:

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

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

2 Likes