65
\$\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\$ – immibis 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

58
\$\begingroup\$

Jelly, 22 bytes

“t,ȧṫÞċḅ»Ḳ“¡¥Ɓc’ṃs4K€Y

Try it online!

How it works

“t,ȧṫÞċḅ»Ḳ“¡¥Ɓc’ṃs4K€Y  Main link. No arguments.

“t,ȧṫÞċḅ»               Use Jelly's dictionary to yield the string
                        "Hare Rama Krishna". Fortunately, the words Rama, Krishna,
                        and hare (lowercase H) are in the dictionary.
         Ḳ              Split at spaces, yielding ["Hare", "Rama", "Krishna"].
          “¡¥Ɓc’        Base-250 literal; yielding 15973600.
                ṃ       Convert 15973600 to base ["Hare", "Rama", "Krishna"]
                        (ternary), where "Krishna" = 0, "Hare" = 1, and "Rama" = 2.
                 s4     Split the resulting string array into chunks of length 4.
                   K€   Join each of the four chunks by spaces.
                     Y  Join the resulting strings by linefeeds.
\$\endgroup\$
  • 43
    \$\begingroup\$ Convert 15973600 to base ["Hare", "Rama", "Krishna"] Thank goodness you explained that further, because that made no sense. \$\endgroup\$ – Nic Hartley Apr 30 '17 at 7:20
  • 11
    \$\begingroup\$ I need to use base ["Hare", "Rama", "Krishna"] more. \$\endgroup\$ – ATaco May 11 '17 at 4:09
63
\$\begingroup\$

05AB1E, 38 bytes

Can be shortened by 2 bytes if trailing newlines are okay.

“«Î‡Ä¦í¥Â“#€¦`«'kì)™ð«•2ÍZì•3BSè#4ô¨»?

Try it online!

Explanation

“«Î‡Ä¦í¥Â“                              # push the string "drama share irish dna"
          #                             # split on spaces
           €¦                           # remove the first character of each word
             `                          # split to stack as separate words 
              «'kì                      # concatenate the last 2 and prepend "k"
                  )™                    # wrap in list and title-case
                    ð«                  # append a space to each
                      •2ÍZì•            # push 27073120
                            3B          # convert to base-3: 1212221110100011
                              Sè        # index into the list with each
                                #       # split on spaces
                                 4ô     # split into pieces of 4
                                   ¨    # remove the last
                                    »   # join on spaces and newlines
                                     ?  # print without newline
\$\endgroup\$
  • 58
    \$\begingroup\$ drama share irish dna, really? Where did that idea come from? +1 :) \$\endgroup\$ – Stewie Griffin Apr 27 '17 at 22:13
  • 14
    \$\begingroup\$ Tell me frankly, did you built a generator of 05AB1E golfed code? \$\endgroup\$ – YSC Apr 28 '17 at 12:13
  • 1
    \$\begingroup\$ @YSC that's not a bad idea for dictionary compression. I'm on it. \$\endgroup\$ – Magic Octopus Urn Jun 27 '17 at 18:51
  • 1
    \$\begingroup\$ 32 bytes \$\endgroup\$ – Kevin Cruijssen Jan 29 at 12:43
43
\$\begingroup\$

Python 2, 60 bytes

a="Hare"
for x in"Krishna","Rama":print a,x,a,x+'\n',x,x,a,a

Try it online!

This seems to beat template-replacement approaches like this attempt.

\$\endgroup\$
39
\$\begingroup\$

Octave, 74 59 bytes

[{'Hare ','Krishna ','Rama ',10}{'ababdbbaadacacdccaa'-96}]

Verify the output here.

Explanation:

{'Hare ','Krishna ','Rama ',10} creates a cell array with three strings, where the fourth is 10 (ASCII-value for newline).

{'ababdbbaadacacdccaa'-96} is a vector that indexes the cell array above. The vector is [1 2 1 2 4 ...] since we subtract 96 from the string ababd....

The surrounding square brackets are used to concatenate the results, instead of getting ans = Hare; and = Krishna; ans = ...

\$\endgroup\$
  • \$\begingroup\$ I didn't know you can index cell like that in Octave (unlike MATLAB)! \$\endgroup\$ – Memming Apr 30 '17 at 12:01
19
\$\begingroup\$

Retina, 39 bytes


hkhk¶kkhh
h
Hare 
*`k
Krishna 
k
Rama 

Try it online!

Mostly plain substitutions, the only "trick" is the modifier * which prints the result of the substitution and then reverts the string back to what it was before.

\$\endgroup\$
16
\$\begingroup\$

PHP, 61 Bytes

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

simply replacement from the digits as key in the array with the values strtr

Try it online!

\$\endgroup\$
12
\$\begingroup\$

JavaScript, 75 70 bytes

`0101
1100
0202
2200`.replace(/./g,n=>['Hare ','Krishna ','Rama '][n])

Try it online!

console.log(`0101
1100
0202
2200`.replace(/./g,n=>['Hare ','Krishna ','Rama '][n]))

\$\endgroup\$
  • 2
    \$\begingroup\$ Better use literal newline characters in the template string directly instead of digit 3. The template string will have the same length, but you will not have to replace them later. (Will not cause array index issue, because /./ not matches newline characters.) \$\endgroup\$ – manatwork Apr 27 '17 at 20:20
12
\$\begingroup\$

C

154 bytes, 124 bytes, 96 bytes

i;main(){char*c="agagvggaavapapvppaaHare \0Krishna \0Rama \0\n";while(i<19)printf(c+c[i++]-78);}

Try it online!

28 bytes saved, thanks to Dennis

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf. \$\endgroup\$ – Rohan Jhunjhunwala Apr 28 '17 at 3:27
  • 1
    \$\begingroup\$ It's a pretty standard acronym for programming puzzles and code golf, the name of this stack exchange. Sorry, I should have made this clear. Welcome to our site, good first post. \$\endgroup\$ – Rohan Jhunjhunwala Apr 28 '17 at 3:30
  • 1
    \$\begingroup\$ This won't work with all compilers, but gcc doesn't require the include statement. Also, if you restrict your submission to C, you can drop the int and declare i globally so it defaults to zero. tio.run/nexus/… \$\endgroup\$ – Dennis Apr 28 '17 at 3:39
  • 2
    \$\begingroup\$ @AnT That's correct, but code golf competitions don't require "proper" code. Here on PPCG, we define languages by their implementation. As long as there is a compiler that produces a working executable, the answer is valid. \$\endgroup\$ – Dennis Apr 28 '17 at 13:16
  • 1
    \$\begingroup\$ main(){for(char*t="BG@ESCB;:N8F6DIBA10Hare \0Krishna \0Rama \0\n";*t^72;printf(t+*t++-48));} Saves 4 bytes. \$\endgroup\$ – Johan du Toit May 12 '17 at 9:22
11
\$\begingroup\$

V, 40, 36 bytes

iHare Krishna 
 Rama ç^/ä$Ùdww.$2p

Try it online!

Hexdump:

00000000: 6948 6172 6520 4b72 6973 686e 6120 0a0e  iHare Krishna ..
00000010: 2052 616d 6120 1be7 5e2f e424 d964 7777   Rama ..^/.$.dww
00000020: 2e24 3270                                .$2p

Explanation:

iHare Krishna   " Enter 'Hare Krishna' on line 1
<C-n> Rama      " Enter 'Hare Rama' on line 2. This works because <C-n>
                " autocompletes alphabetically, and 'Hare' comes before 'Krishna'
<esc>           " Return to normal mode
ç^/             " On every line:
   ä$           "   Duplicate the text on the line horizontally
     Ù          "   Make a new copy of the line
      dw        "   Delete a word
        w       "   Move forward a word
         .      "   Delete a word again
          $     "   Move to the end of the line
           2p   "   And paste what we've deleted twice

The <C-n> command is extremely useful for challenges like this. :)

\$\endgroup\$
10
\$\begingroup\$

C#, 109 bytes

void a(){Console.Write("{0}{1}{0}{1}\n{1}{1}{0}{0}\n{0}{2}{0}{2}\n{2}{2}{0}{0}","Hare ","Krishna ","Rama ");}

Fairly straightforward, Console.Write implicitly formats the string, and using Write instead of WriteLine not only saves 4 bytes, but avoids a trailing newline. Uses Unix-style newlines so might not work so well on windows, extra 6 bytes for windows by changing \n to \r\n

This method will output directly to the console, if you prefer a method that returns a string:

C#, 118 bytes

string a(){return string.Format("{0}{1}{0}{1}\n{1}{1}{0}{0}\n{0}{2}{0}{2}\n{2}{2}{0}{0}","Hare ","Krishna ","Rama ");}

Alternatively if you need a fully stand-alone and compilable program:

C#, 135 bytes

class A{static void main(){System.Console.Write("{0}{1}{0}{1}\n{1}{1}{0}{0}\n{0}{2}{0}{2}\n{2}{2}{0}{0}","Hare ","Krishna ","Rama ");}}

This should work as a compiled program provided you set A as the entry class.

\$\endgroup\$
  • \$\begingroup\$ Looks a bit weird without spaces between the words. \$\endgroup\$ – manatwork Apr 28 '17 at 9:37
  • \$\begingroup\$ @manatwork good point, thankfully spec says trailing spaces are okay, so only cost 3 bytes \$\endgroup\$ – Skidsdev Apr 28 '17 at 9:40
  • \$\begingroup\$ Could start a flame war, but you can use var instead of string to reduce the second example even more. \$\endgroup\$ – John Baughman Apr 28 '17 at 15:46
  • \$\begingroup\$ @JohnBaughman A method's return value cannot be var as far as I know, and var.Format() is definitely not a thing \$\endgroup\$ – Skidsdev Apr 28 '17 at 19:21
  • \$\begingroup\$ Duh... Didn't think of that. You are correct, I just got myself into lamba world and misinterpreted what I was reading. \$\endgroup\$ – John Baughman Apr 28 '17 at 19:51
8
\$\begingroup\$

C, 85 bytes

i;f(){for(i=0;printf("$0$0900$$9$*$*9**$$)"[i++]-36+"Hare \0Rama \0Krishna \0\n"););}

I would prefer a secular mantra, but I hope this is one of those peaceful religions.

See it work here.

This compacts a naive implementation by 23 bytes.

\$\endgroup\$
8
\$\begingroup\$

Python 2, 92 88 74 bytes

for i in'0101311003020232200':print['Hare','Krishna','Rama','\n'][int(i)],

Try it online!

No, it's not clever, no, it's not the shortest but hey, I'm new to this and it works.

Another solution (84 80 bytes):

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\$
  • \$\begingroup\$ Swap the zeroes and ones, along with 'Hare' and 'Krishna' in the list. This removes the leading zero. Then convert the new number (1010300113121232211) to hex (0xe054e999f20c553), surround it by backticks, and finally realize xnor's answer is still ahead by 12 bytes! tio.run/##K6gsycjPM/r/Py2/… \$\endgroup\$ – vroomfondel Jun 27 '17 at 22:32
6
\$\begingroup\$

Perl, 67 bytes

Inspired by this JavaScript entry.

$_='0101
1100
0202
2200
';s/./qw'Hare Krishna Rama'[$&].$"/ge;print

Perl, 67 bytes

@_=<Hare Krishna Rama>;print"@_[split//]\n"for<0101 1100 0202 2200>
\$\endgroup\$
  • 2
    \$\begingroup\$ (Hare,Krishna,Rama) saves two bytes in your first code. \$\endgroup\$ – Dada May 4 '17 at 12:27
5
\$\begingroup\$

Matlab 139 136 105 bytes (thanks to @2501)

fprintf('Hare Krishna Hare Krishna\nKrishna Krishna Hare Hare\nHare Rama Hare Rama\nRama Rama Hare Hare')

Try it Online in Octave!

\$\endgroup\$
  • \$\begingroup\$ or 126 bytes if the trailing new line is allowed \$\endgroup\$ – user13267 Apr 28 '17 at 11:23
  • \$\begingroup\$ @2501: haha that's great. Actually I think it's 105 bytes may be you should post it as an answer \$\endgroup\$ – user13267 Apr 28 '17 at 11:32
  • 5
    \$\begingroup\$ I'm pretty sure further improvements are possible. \$\endgroup\$ – 2501 Apr 28 '17 at 12:11
  • 1
    \$\begingroup\$ FYI, TIO supports Octave :) tio.run/nexus/… \$\endgroup\$ – Beta Decay Apr 28 '17 at 16:47
  • 1
    \$\begingroup\$ x={'Hare ','Krishna ','Rama ',10};[x{'ababdbbaadacacdccaa'-96}] is 64 bytes. :P (translating @Stewie Griffin's octave submission) \$\endgroup\$ – Memming Apr 30 '17 at 12:06
5
\$\begingroup\$

MySQL, 115 100 bytes

(Thanks @manatwork!)

SELECT CONCAT(a,b,a,b,d,b,b,a,a,d,a,c,a,c,d,c,c,a,a)FROM(SELECT'Hare 'a,'Krishna 'b,'Rama 'c,'\n'd)t
\$\endgroup\$
  • \$\begingroup\$ You can remove all AS . \$\endgroup\$ – manatwork Apr 29 '17 at 15:11
  • \$\begingroup\$ Good trick, I'll definitely be using this method for some challenges. \$\endgroup\$ – BradC Jun 27 '17 at 20:07
5
\$\begingroup\$

R, 75 85 83 bytes

cat(x<-c("Hare ","Krishna ","\n")[c(1,2,1:3,2,2,1,1,3)],sub(x[2],"Rama ",x),sep="")

It creates a vector with Hare, Krishna, and the newline, takes the ones needed, and then repeats it replacing Krishna by Rama.

Needed to include the space on every word and sep="" because otherwise cat() would put a space at the beginning of each line.

\$\endgroup\$
  • \$\begingroup\$ I can't get this to work in TIO \$\endgroup\$ – Giuseppe Apr 28 '17 at 15:12
  • \$\begingroup\$ Probably needs a print() or something. \$\endgroup\$ – BLT Apr 28 '17 at 17:18
  • \$\begingroup\$ @Giuseppe I'm using here that R automatically prints. But depending how you call it, print() or cat() would be needed. That would require way more bytes. I'll post an alternative with them this weekend :) \$\endgroup\$ – Masclins Apr 29 '17 at 8:31
  • 1
    \$\begingroup\$ @AlbertMasclans no I'm getting an error saying object 'h' not found tio.run/nexus/… \$\endgroup\$ – Giuseppe Apr 29 '17 at 14:26
  • 2
    \$\begingroup\$ I think you need to replace the h= with h<- inside the calls to p \$\endgroup\$ – Giuseppe Apr 29 '17 at 14:31
5
\$\begingroup\$

PowerShell, 73 53 52 Bytes

absolutely demolished by Jeff Freeman - using an actual newline instead of \n saves another byte, and also saved one more on the array format. (from (1,2) to ,1,2)

-1 thanks to TesselatingHeckler, no comma in the array notation.

$a="Hare ";"Krishna ","Rama "|%{"$a$_$a$_
$_$_$a$a"}

My old answer - Tried a few other replace methods but all ended up being slightly longer somehow.

$a,$b,$c="Hare ","Krishna ","Rama ";"$a$b$a$b
$b$b$a$a
$a$c$a$c
$c$c$a$a"

there are newlines in the string.

pretty straightforward, uses the fact that powershell will expand variables within doublequotes, and a shorter method of assigning variables to save some bytes.

\$\endgroup\$
  • \$\begingroup\$ Good solution, but I challenge you to get it down more by noticing a pattern in the output and pipelining. I've got it down to 55 Bytes. I don't know how to spoiler a comment, so just reply here if you'd like to see it (or if you'd like some hints!). \$\endgroup\$ – Jeff Freeman Apr 28 '17 at 18:42
  • 2
    \$\begingroup\$ Instead of a spoiler tag, I figured out how to share via a link. Try it online! \$\endgroup\$ – Jeff Freeman Apr 28 '17 at 21:04
  • \$\begingroup\$ @JeffFreeman nice one, shaved two more off that on the newline and array - down to 52, beating almost all the non-golfing languages. \$\endgroup\$ – colsw May 2 '17 at 9:03
4
\$\begingroup\$

Sed, 72

Score includes +1 for the -r flag.

s/^/Hari Krishna/
s/.*/& &/
s/(\S+)(.*)\b(.+)/&\n\3\2\1/p
s/K\S+/Rama/g

Try it online.

\$\endgroup\$
  • \$\begingroup\$ Pretty sure there's a way to remove some bytes using change, append or insert instead of substitute. Edit : not that sure anymore ; Edit edit : yeah no that doesn't work because they don't work on the pattern space but instead directly output \$\endgroup\$ – Aaron Apr 28 '17 at 13:21
4
\$\begingroup\$

Ruby, 62 61 bytes

As I worked on this it eventually became almost identical to @xnor's Python answer, except Ruby doesn't have spaces between arguments in its print function, forcing me to use joins instead and resulting in a longer answer...

-1 bytes from @manatwork

%w"Krishna Rama".map{|i|puts [h=:Hare,i]*2*' ',[i,i,h,h]*' '}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Make :Hare a Symbol. \$\endgroup\$ – manatwork Apr 28 '17 at 7:42
4
\$\begingroup\$

Bash, 93 90 bytes

h="Hare " k="Krishna " r="Rama " s="$h$k$h$k\n$k$k$h$h\n$h$r$h$r\n$r$r$h$h"
echo -e "${s}"
\$\endgroup\$
  • 1
    \$\begingroup\$ Nice first try. May I suggest a couple of minor improvements? pastebin.com/0bYQF26n \$\endgroup\$ – manatwork Apr 29 '17 at 10:13
  • \$\begingroup\$ @manatwork you should add it as your answer. \$\endgroup\$ – Pandya Apr 29 '17 at 14:27
  • 1
    \$\begingroup\$ Without the variable s= it's down to 79 h="Hare " k="Krishna " r="Rama ";echo -e "$h$k$h$k\n$k$k$h$h\n$h$r$h$r\n$r$r$h$h" \$\endgroup\$ – ULick Apr 30 '17 at 16:18
3
\$\begingroup\$

Pyth -- 45 41 bytes

Ms(GHGHbHHGG)K"Hare "gK"Krishna "gK"Rama 

Try It

\$\endgroup\$
3
\$\begingroup\$

AHK, 107 92 bytes

This feels ridiculous but I can't find a shorter means in AHK to do this:

h=Hare
k=Krishna
Send %h% %k% %h% %k%`n%k% %k% %h% %h%`n%h% Rama %h% Rama`nRama Rama %h% %h%

Here's what I tried first that was 107 bytes and tried to be fancy. As Digital Trauma pointed out, though, it would have been shorter to just send the raw text.

n=1212422114131343311
a=Hare |Krishna |Rama |`n
StringSplit,s,a,|
Loop,Parse,n
r:=r s%A_LoopField%
Send %r%

StringSplit creates a pseudo-array with 1 as the first index. So the first term is referenced with s1, the second with s2, etc. Otherwise, there's nothing fancy here.

\$\endgroup\$
  • 13
    \$\begingroup\$ The entire output is only 92 bytes. Would it be better simply to Send Hare Krishna ...? \$\endgroup\$ – Digital Trauma Apr 27 '17 at 21:43
  • \$\begingroup\$ @DigitalTrauma HA! Yes, probably, then. I presumed fancy was better and didn't even check. AHK is not a good language for this. \$\endgroup\$ – Engineer Toast Apr 28 '17 at 12:15
3
\$\begingroup\$

Batch, 75 bytes

@for %%h in (Krishna Rama)do @echo Hare %%h Hare %%h&echo %%h %%h Hare Hare
\$\endgroup\$
3
\$\begingroup\$

C 96 bytes

*v[]={"Hare ","Krishna ","Rama ","\n"};
main(){for(long a=0x30ae2305d10;a/=4;printf(v[a&3]));}

You only need two bits of information to figure out which word from the prayer is needed next, so instead of using a bunch of memory and encoding it as a string, you can just encode it as a 40 bit integer (i.e. inside a long int).

\$\endgroup\$
3
\$\begingroup\$

ksh / bash / sh, 66 bytes

h=Hare;for j in Krishna Rama;do echo $h $j $h $j"
"$j $j $h $h;done
\$\endgroup\$
  • \$\begingroup\$ @nimi Thanks. Changed it to an <CR>, makes it even shorter. Can't test it myself, but this should work. At least it works in bash and even (busybox)sh. \$\endgroup\$ – ULick Apr 30 '17 at 17:58
  • \$\begingroup\$ seems I added a blank in editing, but did subtract one, due to the change from '\n' to <CR> \$\endgroup\$ – ULick Apr 30 '17 at 18:53
3
\$\begingroup\$

APL (Dyalog), 47 bytes

Requires ⎕IO←0 which is default on many systems.

↑∊¨↓'Krishna ' 'Hare ' 'Rama '[(4⍴3)⊤8×4 1 5 2]

Try it online!

8×4 1 5 2 multiply; [32,8,40,16]

(4⍴3)⊤ convert to 4-digit base 3; matrix [[1,0,1,0],[0,0,1,1],[1,2,1,2],[2,2,1,1]]

[] index

 split into list of lists

∊¨ enlist (flatten each)

 mix into matrix (padding with spaces)

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

Retina, 60

Direct port of my sed answer:


Hari Krishna
.+
$0 $0
:`(\S+)(.*)\b(.+)
$0¶$3$2$1
K\S+
Rama

Try it online.

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

Java 7, 104 103 bytes

String c(){return"xyxy\nyyxx\nxRama xRama\nRama Rama xx".replace("x","Hare ").replace("y","Krishna ");}

Explanation:

String c(){                                      // Method without parameters and String return-type
  return"xyxy\nyyxx\nxRama xRama\nRama Rama xx"  //  Return this String
    .replace("x","Hare ")                        //  after we've replaced all "x" with "Hare "
    .replace("y","Krishna ");                    //  and all "y" with "Krishna "
}                                                // End of method

Try it here.

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

Batch, 117 111 103 bytes

@set a=Hare &set b=Krishna &set c=Rama 
@echo %a%%b%%a%%b%^

%b%%b%%a%%a%^

%a%%c%%a%%c%^

%c%%c%%a%%a%

Down to 103 now that trailing newlines are allowed.

^\n\n is used to insert a newline at the end of the first three lines and there's a space after Rama.

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

Perl, 76 74 bytes

$_=121202211;$_.=0 .y/2/3/r;print$_?(0,Hare,Krishna,Rama)[$_].$":$/for/./g
\$\endgroup\$
  • 1
    \$\begingroup\$ 2 bytes shorter: Try it online! (I'm pretty sure the output is the right one) \$\endgroup\$ – Dada Apr 28 '17 at 19:35

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.