66
\$\begingroup\$

Recently, I've seen Hare Krishna people with their mantra on the emblem and I've found it may be quite interesting to code golf.

The challenge

Write the Hare Krishna mantra, i.e.:

Hare Krishna Hare Krishna
Krishna Krishna Hare Hare
Hare Rama Hare Rama
Rama Rama Hare Hare

Winning criteria

This is , so shortest code in bytes wins!

Rules

  • Casing must be preserved.
  • Text should contain new lines.
  • Lines may have trailing space(s).
  • Trailing newline is allowed.
  • Parsing from web or any other external resource disallowed.
\$\endgroup\$
  • 2
    \$\begingroup\$ From the title I was expecting something to decode binary strings into ASCII characters. The title represents a backslash. \$\endgroup\$ – user253751 May 1 '17 at 23:12
  • 5
    \$\begingroup\$ All the mantra has 97 bytes. Surpisingly enough, there are answers with more than that many bytes. \$\endgroup\$ – Masclins May 2 '17 at 8:03
  • \$\begingroup\$ Hmm, Why does it surprisingly look like a Rickroll dupe? \$\endgroup\$ – Matthew Roh May 2 '17 at 13:07

55 Answers 55

2
\$\begingroup\$

Brainfuck Substitutor, 277 bytes

{--y{-z[<]w++v->u>+<t-[x[-]Hxtuyy-]>-.ax+[ty<]>>-]<-.rx>+[-vwz>-]>.ext>w<y{]>-. x>tt-<]>u]>-.Kxtuyy-]>w.ix+[v-z>{]>.sx+[vv-z>{]>.hx+[v-z>{]>-.nxt>{<yy-]>.!xwwwww.Rxtuy]>y.mx+[v-z>+>{]>.
Hare Krishna Hare Krishna!Krishna Krishna Hare Hare!Hare Rama Hare Rama!Rama Rama Hare Hare

Brainfuck Substitutor (or BFS for short) can redefine characters to be replaced with other characters.

This program redefines the characters of the Hare Krishna mantra with brainfuck 'mini' programs that prints the appropiate characters.

As this is run in succinct mode (which doesn't need any command line arguments and is on my default), we can't redefine newlines (at least, in the current version) so I use the character ! instead - but it still outputs a newline.

This first bit of the program: {--y{-z[<]w++v->u>+<t-[x[-] is short replacements for patterns that come up often in the future.

The interpreter breaks them down like this:

{ equals --
y equals {- equals ---
z equals [<]
w equals ++
v equals ->
u equals >+<
t equals -[
x equals [-]

Similar thing goes for every other character. I used the shortest brainfuck constants on the esolangs wiki page to get my algorithms, and replaced common patterns. There may be other patterns I can replace (my original attempt was 320 bytes), but this is what I have for now.

\$\endgroup\$
  • \$\begingroup\$ Could you replace Hare, Krishna, and Rama with their own macros? \$\endgroup\$ – ETHproductions Apr 28 '17 at 21:19
  • \$\begingroup\$ @ETHproductions Maybe I could, but that wouldn't be too interesting ;) \$\endgroup\$ – Okx Apr 28 '17 at 21:26
2
\$\begingroup\$

Perl, 98 72 bytes

@a=("Hare ","Krishna ","Rama ","\n");foreach(split//,'ababdbbaadacacdccaad'){print $a[ord($_)-97]}

98 now reduced to 72, following @hobbs reworking, nice.

print+("Hare ","Krishna ","Rama ",$/)[$_]for'01013110030202322003'=~/./g
\$\endgroup\$
  • 1
    \$\begingroup\$ Good start! Here are some tips: gist.github.com/arodland/c061dd7b0207a9b77a768391c3ff3e64 \$\endgroup\$ – hobbs Apr 29 '17 at 4:21
  • \$\begingroup\$ And the PPCG Perl golfing tips \$\endgroup\$ – hobbs Apr 29 '17 at 4:22
  • \$\begingroup\$ Retaining the ideas from yours but just golfing it harder puts it within a few bytes of the best perl solutions \$\endgroup\$ – hobbs Apr 29 '17 at 4:31
  • \$\begingroup\$ Nice work, have revised the answer accordingly. \$\endgroup\$ – steve Apr 29 '17 at 8:26
2
\$\begingroup\$

Emacs Lisp, 109 bytes

(lambda()(apply'concat(mapcar(lambda(x)(nth(- x ?a)'("Hare ""Krishna ""Rama ""\n")))"ababdbbaadacacdccaad")))

Can theoretically be reduced to 103 bytes by using ^@,^A,^B and ^C instead of a, b, c and d and x instead of (nth(- x ?a)), but then it could not be pasted due to NULL characters.

\$\endgroup\$
  • 1
    \$\begingroup\$ How long would it be to output the original raw string directly? \$\endgroup\$ – Cœur May 1 '17 at 16:12
  • 1
    \$\begingroup\$ @Cœur 91 as the length of the string itself, +2 for quotes, +10 if you wrap it in a lambda, resulting in 103 total. Emacs Lisp is not a good language for golfing. \$\endgroup\$ – Lord Yuuma May 11 '17 at 15:11
2
\$\begingroup\$

Wolfram Mathematica, 71 75 bytes

{"Krishna ","Hare ","Rama ","\n"}[[IntegerDigits[73825885093,4]+1]]<>""

Every part of result was encoded as an index in a list, and every index was encoded as digit of 4-ary integer.

\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! :) \$\endgroup\$ – Adnan May 1 '17 at 11:44
2
\$\begingroup\$

Perl 6, 61 60

my $a="Hare ";"$a$_ $a$_\n$_ $_ $a$a".say for <Krishna Rama>
\$\endgroup\$
2
\$\begingroup\$

Hack 68 bytes

<?hh echo strtr("0101
1100
0202
2200",["Hare ","Krishna ","Rama "]);

Straight port of the PHP Answer, just wanted to finally get an excuse to use hack in PPCG :D

\$\endgroup\$
2
\$\begingroup\$

C++ 158 Bytes

#include <iostream>
#define H "Hare "
#define K "Krishna "
#define R "Rama "
#define N "\n"
void m(){std::cout<<(H K H K N K K H H N H R H R N R R H H);}
\$\endgroup\$
2
\$\begingroup\$

C# 103 99 Bytes

()=>{string a="Hare ",b="Krishna ",c="Rama ",d="\n";return a+b+a+b+d+b+b+a+a+d+a+c+a+c+d+c+c+a+a;};

Ungolfed full program:

class A
{
    static void Main()
    {
        System.Func<string> f =
            () =>
            {
                string a = "Hare ",
                    b = "Krishna ",
                    c = "Rama ",
                    d = "\n";
                return a + b + a + b + d + b + b + a + a + d + a + c + a + c + d + c + c + a + a;
            };
        System.Console.Write(f());
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ You could golf it by 1 byte by using a port of my Java 7 answer: ()=>{return"xyxy\nyyxx\nxRama xRama\nRama Rama xx".Replace("x","Hare ").Replace("y","Krishna ");}; Try it here. \$\endgroup\$ – Kevin Cruijssen May 2 '17 at 13:14
2
\$\begingroup\$

Clojure(Script), 70 bytes (69 bytes?)

(def a"Hare")(run! #(println a % a %(str"\n"%)% a a)["Krishna""Rama"])

This version with 69 bytes will only work at the repl, because map is lazy and I don't use the return value for anything. Dunno how the rules work in this case...

(def a"Hare")(map #(println a % a %(str"\n"%)% a a)["Krishna""Rama"])
\$\endgroup\$
2
\$\begingroup\$

Haskell, 92 91 bytes

g=mapM_ putStr$(!!)["Hare ","Krishna ","Rama ","\n"]<$>(read.(:[]))<$>"0101311003020232200"

Edit: removed concat, used mapM_ for putStr instead

Try it online!

\$\endgroup\$
2
\$\begingroup\$

///, 46 bytes

/4/Hare //k/Krishna //X/4k4k
kk44/X
/k/Rama /X

Try it online!

There was already one answer in ///, but this one adds a bit of complexity to remove some bytes.

In pseudocode this could translate to:

4="Hare "
k="Krishna "
X=4+k+4+k+'\n'+k+k+4+4
print(X+'\n')
replace k with "Rama "
print(X)

(I wanted to use h as the letter to substitute with "Hare ", but "Krishna" also contains an h, so that messed up the substitutions and I had to switch it with 4)

\$\endgroup\$
2
\$\begingroup\$

Husk, 22 bytes

ṁSeÖ>m´+Πmw¶¨Hȧeȷ#,R□m

Try it online!

I've worked for quite some time on this answer since I wanted to beat the "compressed number" algorithm of the current top answer by exploiting more of the regularities in the text.

In the end I "only" managed to tie its score. I actually have in mind a couple of builtins for Husk that could easily save more bytes, but implementing them before posting this answer would have felt too much like cheating.

Explanation

Husk automatically prints a matrix of strings by joining them with spaces and newlines. In this case we are actually trying to build the matrix

[["Hare","Krishna","Hare","Krishna"],
["Krishna","Krishna","Hare","Hare"],
["Hare","Rama","Hare","Rama"],
["Rama","Rama","Hare","Hare"]]

which will implicitly get printed to StdOut as the required text. For simplicity, I will show each step of computation as if it was printed by a full program, joining strings with spaces and newlines.

We need to work with "Hare" and ("Krishna" and "Rama"): mw¶¨Hȧeȷ#,R□m

¨Hȧeȷ#,R□m is a compressed string; Husk has a dictionary containing common n-grams (sequences of characters) which are encoded with symbols possibly reminding of the original n-gram. For example, the H here is a plain "H", while ȧe is "are\n". The plaintext string this decodes to is "Hare\nKrishna Rama" (where \n is an actual newline). splits this string on the newline, and mw splits each line into words. The output we get up to here is:

Hare
Krishna Rama

"Hare" should pair with both of the other words: Π

Cartesian product of all the lines. This means that we pair each element of the first line with each element of the second line. Our result is:

Hare Krishna
Hare Rama

The first and third lines are just those words repeated twice: m´+

Concatenate each of the lines with itself. (´ makes + use a single argument twice, and m maps this function to each line). We get:

Hare Krishna Hare Krishna
Hare Rama Hare Rama

The other lines are the previous lines sorted in reverse alphabetical order: ṁSeÖ>

This is not as weird as it looks. Ö> sorts a list in descending order. With SeÖ> we create a two-element list with the original argument before and after having been sorted. maps this function to each line, and concatenates the resulting lists. This is finally:

Hare Krishna Hare Krishna
Krishna Krishna Hare Hare
Hare Rama Hare Rama
Rama Rama Hare Hare
\$\endgroup\$
1
\$\begingroup\$

Fourier, 94 bytes

|72a97a114a101a32a|H|75a114a-9a115a104a+6a97a32a|K|82a97a109a97a32a|RHKHK10aKKHH10aHRHR10aRRHH

Try it on FourIDE!

Makes good use of functions: H for Hare, K for Krishna and R for Rama.

Now I can't get the song My Sweet Lord out of my head :P

\$\endgroup\$
1
\$\begingroup\$

Haskell 103 95 Bytes

main=putStr$concat$concat$[[h,x,h,x,n,x,x,h,h,n]|x<-["Krishna ","Rama "]]where h="Hare ";n="\n"

Old 103 Bytes:

main=putStr$concat[h,k,h,k,n,k,k,h,h,n,h,r,h,r,n,r,r,h,h] where h="Hare ";k="Krishna ";r="Rama ";n="\n"
\$\endgroup\$
1
\$\begingroup\$

SmileBASIC 3, 76 bytes

Straightforward answer with string multiplication. ? is print.

?"Hare Krishna "*2?"Krishna "*2+"Hare "*2?"Hare Rama "*2?"Rama "*2+"Hare "*2
\$\endgroup\$
1
\$\begingroup\$

Groovy, 82 bytes

a=["Hare ","Krishna ","Rama ","\n"]
"0101311003020232200".each{print a[it as int]}

An improvement of my previous answer (1 byte longer)

h="Hare "
k="Krishna "
r="Rama "
n="\n"
print h+k+h+k+n+k+k+h+h+n+h+r+h+r+n+r+r+h+h
\$\endgroup\$
1
\$\begingroup\$

cat 97 bytes

Hare Krishna Hare Krishna<CR>
Krishna Krishna Hare Hare<CR>
Hare Rama Hare Rama<CR>
Rama Rama Hare Hare<^D>

I consider <^D> as two characters, whence 97

\$\endgroup\$
  • \$\begingroup\$ I would really like to know what's wrong with the answer. It's working, and it's shorter than many "clever" answers by people around. So what?! \$\endgroup\$ – yo' May 3 '17 at 11:44
  • 1
    \$\begingroup\$ Is is not very clever of semi boring \$\endgroup\$ – Christopher May 5 '17 at 15:00
  • \$\begingroup\$ There are many puzzles where the "trivial" output is, in fact, the shortest. Even if it isn't, I always start with the trivial solution so I have something to compare against. This answer is straight-forward, but perfectly fine. \$\endgroup\$ – BradC Jun 27 '17 at 19:37
  • \$\begingroup\$ mothereff.in/byte-counter this site would beg to differ with your byte count \$\endgroup\$ – Brian H. Sep 12 '17 at 13:49
  • \$\begingroup\$ @BrianH. Right, it should be even lower; I'm not sure how I counted, I probably considered <CR> as 2 charaters rather than 1. \$\endgroup\$ – yo' Sep 12 '17 at 13:59
1
\$\begingroup\$

05AB1E, 54 bytes

•4Ñ••}ò´••9Ä•)©\•2}¾S•3BRv®yè8ÝJ"ahrekisnm"‡})4ôvyðý™,

Try it online!

•4Ñ•                          # Hare
    •}ò´•                     # Krishnu
         •9Ä•)©\              # Rama
                •2}¾S•3BR     # Sequencing data.
v®yè8ÝJ"ahrekisnm"‡}          # For each sequence part, push the right word.
                    )4ôvyðý™, # Format it correctly.

05AB1E changed its base-214 encryption to base-255; this is the reason this submission no longer works; however, it was valid at the time.

\$\endgroup\$
  • \$\begingroup\$ @DLosc 05AB1E changed it's base-214 encryption to base-255; this is the reason this submission no longer works. \$\endgroup\$ – Magic Octopus Urn Jun 27 '17 at 18:49
  • \$\begingroup\$ @DLosc I think this would be like ~34 bytes now. \$\endgroup\$ – Magic Octopus Urn Jun 27 '17 at 18:50
1
\$\begingroup\$

///, 49 bytes

/#/Hare //k/Krishna //j/Rama /#k#k
kk##
#j#j
jj##

Try it online!

Although /// is an esoteric language, this one is shorter than most of the other (non-golfing, and a bad implementation of this problem in Retina) languages here. Mostly because /// has too few functionalities, that they can be represented in few bytes.

\$\endgroup\$
1
\$\begingroup\$

Javascript 85 bytes

I know there's a better answer already, but i wanted to give it a try anyway, the method is pretty different as you'll see:

a=["Krishna ","Rama "];q="Hare ";b=(z)=>q+a[z]+q+a[z]+`
`+a[z]+a[z]+q+q+`
`;b(0)+b(1)

a=["Krishna ","Rama "];q="Hare ";b=(z)=>q+a[z]+q+a[z]+`
`+a[z]+a[z]+q+q+`
`;console.log(b(0)+b(1));

Please note that i had to put the console.log() in a weird spot, if you just paste the code block into the console it'll print the mantra.

dumbded down better golfed version 76 bytes:

a="Krishna ";b="Rama ";c="Hare ";d=`
`;c+a+c+a+d+a+a+c+c+d+c+b+c+b+d+b+b+c+c

a="Krishna ";b="Rama ";c="Hare ";d=`
`;console.log(c+a+c+a+d+a+a+c+c+d+c+b+c+b+d+b+b+c+c)

\$\endgroup\$
1
\$\begingroup\$

Recursiva, 57 53 bytes

r2r1r0"0101/n1100/n0202/n2200"'Hare ''Krishna ''Rama 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

SmileBASIC, 58 bytes

We can use the fact that the second half is the same as the first, with "Krishna" replaced by "Rama"

P"Krishna 
P"Rama 
DEF P K?("Hare "+K)*2?K*2;"Hare "*2
END

(Note trailing spaces)

Storing "Hare " in a variable is the same length.

\$\endgroup\$
0
\$\begingroup\$

Tcl, 87 bytes

puts "[set h Hare] [set k Krishna] $h $k
$k $k $h $h
$h [set r Rama] $h $r
$r $r $h $h"

Try it online!

\$\endgroup\$
  • \$\begingroup\$ [set h Hare]? [set r Rama]? \$\endgroup\$ – Khuldraeseth na'Barya Sep 26 '17 at 0:45
  • \$\begingroup\$ @Scrooble: What are you trying to ask? set puts the value in a var that can later be consulted if prefixed by $ \$\endgroup\$ – sergiol Sep 26 '17 at 0:50
  • \$\begingroup\$ Your code does not output quite what the challenge specifies. I was giving corrections; sorry for my lack of clarity. \$\endgroup\$ – Khuldraeseth na'Barya Sep 26 '17 at 0:54
  • \$\begingroup\$ @Scrooble: Thanks. Now fixed. Sorry for misunderstanding you \$\endgroup\$ – sergiol Sep 26 '17 at 0:56
0
\$\begingroup\$

Jq 1.5, 59 57 54 52 bytes

def h:"Hare "+.;"Krishna ","Rama "|h*2,.*2+"Hare "*2

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 49 41 bytes

;"1{&(<;.2'Hare Krishna Rama ')(,-)#:5 12

How it works

                                   #:5 12 to binary [(0 1 0 1), (1 1 0 0)] 
                               ( -)       negate    [(0-1 0-1),(-1-1 0 0)]
                               (, )       append    [(0 1 0 1),(1 1 0 0),(0-1 0-1),(-1-1 0 0)] 
     (<;. 'Hare Krishna Rama ')           split by 
         2                                the last character, keep it in
   {&                                     negative argument selects from the end
;"1                                       get rid of the ASCII boxes

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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