Love in 245 Characters (code golf)


#1

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

#2

This is a thing of beauty.


#3

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


#4

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

#5

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

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


#6

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


#7

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


#8

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


#9

I love you now!


#10

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