| Bytes | Lang | Time | Link |
|---|---|---|---|
| 217 | JavaScript Node.js | 240807T162121Z | Andrew B |
| 210 | C gcc | 171122T201139Z | gastropn |
| 076 | Jelly | 170721T171336Z | Dennis |
| 319 | Javascript | 170721T032407Z | sq2 |
| 239 | Vim | 170720T113510Z | BlackCap |
| nan | A port of my Charcoal answer | 170720T085729Z | Charlie |
| 251 | Java OpenJDK 8 | 170720T081938Z | Olivier |
| 212 | Python 2 | 170719T090001Z | Coty Joh |
| 121 | Pyth | 170719T234940Z | FryAmThe |
| 203 | JavaScript ES6 | 170719T110228Z | Arnauld |
| 074 | 05AB1E | 170719T112725Z | kalsower |
| 216 | Ruby | 170719T142515Z | Simon Ge |
| nan | 170719T090715Z | Charlie | |
| 213 | Python 2 | 170719T070502Z | TFeld |
| 194 | Python 3 | 170719T074237Z | 0xffcour |
| 083 | Jelly | 170719T093610Z | Leaky Nu |
| 183 | Retina | 170719T092445Z | Neil |
| 219 | JavaScript | 170719T063148Z | user7234 |
| 074 | SOGL V0.12 | 170719T082757Z | dzaima |
| 215 | JavaScript | 170719T074452Z | tsh |
| 185 | Retina | 170719T064115Z | ovs |
| 1398 | Add++ | 170719T062052Z | caird co |
| 125 | Bubblegum | 170719T061645Z | Dennis |
JavaScript (Node.js), 218 217 bytes
s=>`jwq80
13ntvk
26p62g
25g7w8
239k3c
zg4xc
7rpbk
1e9dc
b8mw
5mbc
4f9reg
4f9r0g
4f9r0g`.replace(/.+/g,c=>parseInt(c,36).toString(2).slice(x=1)).replace(/./g,c=>+c?' ':'gtheanswertolifetheuniverseandeverythin'[x++%39])
C (gcc), 220 219 217 213 210 bytes
-1 byte thanks to ceilingcat
-3 bytes thanks to Jerry Jeremiah
q,i,j,t;main(c){for(;c="HDIG@GEGJ@FFFDFD@EGFDFD@DDBDFDFD@CDCDLE@BDDDJE@ADEDHE@AMED@AMDD@HDFDGD@HDFM@HDFM"[t++];)for(c%=64,j=c?q=!q,c:2;--j;)putchar(c?q?32:"theanswertolifetheuniverseandeverything"[i++%39]:10);}
Jelly, 76 bytes
“¡¢ʋỵṆ⁻kỴ⁷ṁḤæ ƊVṛĠ¥¡¢tṢ}ȧƘ=ẒṆ_`-p1ḷṠ’ḃ⁴ĖŒṙḂ¬R“£:(ḥB⁼Ṇ#¥Ṡ1ɗĠðȮ $¿⁹½ɓ»ṁȯ€⁶s27Y
Javascript, approximate drawing. 319 bytes
s=32
t="theanswertolifetheuniverseandeverything"
j=0
o=""
b=document.createElement('canvas'),c=b.getContext('2d')
b.width=b.height=s
c.font="25px Verdana"
c.fillText('42',0,20)
m=c.getImageData(0,0,s,s)
i=0
while(i<m.data.length) {
d=m.data[i+3]
o+=d?t[j%t.length]:" "
if(d)j++
i+=4
}
o.match(/.{1,32}/g).join("\n")
^ for what it's worth, not much really, but before I delete the failed code.
Vim, 239 keystrokes
:h4<CR>3hy5bZZitheanswerto<Esc>p:%s/ \|,//g<CR>ye3P
lqqi<CR><Esc>qqw3li <Esc>lq3@wbhr<CR>16l@q@w3-@w6l@ql@w9l@qll3@whr<CR>3l9@w4l@q 2@w4l@q2@w4l@q9l2@wlr<CR>9l@w4klr<CR>4whr<CR>jj
el<C-v>3k"aD0jji <Esc>qqdiwukPi <Esc>q5@qwy04j$"ap6+<C-v>GPp3kw3i <Esc>2@q4kbXj2@qywh<C-v>4jp2je<C-v>jj4A <Esc>8j5i <Esc>b<C-v>jj2I <Esc>
Linebreaks added for "readability"
Explanation
The first line yanks life, the universe and everything from a help page, which is one byte shorter than simply typing it out. It then turns that into this:
theanswertolifetheuniverseandeverythingtheanswertolifetheuniverseandeverythingtheanswertolifetheuniverseandeverythingtheanswertolifetheuniverseandeverything
The second line breaks the string up into:
the answer
toli fetheuniv
ersea nde ver
ything the ans
wer tol ife the
uni ver sean
dev ery thin
gth ean swer
tolifetheuni ver
seandeveryth ing
the ans wer
tol ifetheuniver
sea ndeverything
And then the final line indents it to make:
the answer
toli fetheuniv
ersea nde ver
ything the ans
wer tol ife the
uni ver sean
dev ery thin
gth ean swer
tolifetheuni ver
seandeveryth ing
the ans wer
tol ifetheuniver
sea ndeverything
A port of my Charcoal answer:
C# (.NET Core), 229 bytes
_=>{var r="";for(int i=0,j,k=0,n=0;i<68;i++)for(j=0;j++<")%*(+&(+)''%'%'('%'%&%#%'%'%%%$%-&%%%%+&&%&%)&(.&%*.%%2%'%(%)%'.)%'."[i]-34;){r+=i%2<1?' ':"theanswertolifetheuniverseandeverything"[k++%39];if(++n%27<1)r+='\n';}return r;}
Java (OpenJDK 8), 258 252 251 bytes
z->{int i=0,x,j,N[]={16515968,33489856,x=58950624,x+16,x-40,31458204,7865230,1966983,462847,233471,117670784,x=134185856,x};for(int n:N)for(j=32;j-->0;n/=2)System.out.print(n%2<1?j<1?"\n":" ":"theanswertolifetheuniverseandeverything".charAt(i++%39));}
It's a rather naive implementation. First a mask, for the printed characters, then a roll over the text until done.
- 6 bytes saved thanks to Carlos Alejo!
Python 2, 220 213 212 bytes
-1 byte by switching ()*4 for %39 from @officialaimm
s=""
w=l=i=0
for g in`int("352G7FS4XC8J2Q2M2HNK7IZI65Z9TVUMHOZ6MR3HY46RQBLWY4PR",36)`[:-1]:
for j in g*int(g):l+=1;s+=[' ',"theanswertolifetheuniverseandeverything"[w%39]][i%2]+"\n"*(l%27<1);w+=i%2
i+=1
print s
This is a different approach from the other Python answers. I use a hex base-36 (saved 7 bytes) encoding of a PNG-style RLE-style encoding of the image (a string of digits indicating the number of consecutive repeated pixels).
The string of digits is:
73869469755353565353431353533323902433339443437469034389033390735363735903735903
Then I iterate through those digits and alternately print that number of ' 's or characters from the palette ('theanswer...'). When more than 9 of a character is repeated, I'd simply add a 0 and then the remainder.
Pyth, 121 bytes
This contained too many bytes that I couldn't get to show up in TIO, so here's a hexdump:
00000000: 7558 6848 477c 2a64 6548 622c 5658 434d uXhHG|*deHb,VXCM
00000010: 2290 8d8d 817e 7e7b 7875 7572 6663 5753 "....~~{xuurfcWS
00000020: 504d 4946 4343 3f3c 3939 3633 302d 2d2a PMIFCC?<99630--*
00000030: 2721 211e 1b16 160d 0909 0300 225d 545d '!!........."]T]
00000040: 3133 6a43 2202 1232 a464 b09d 7303 4244 13jC"..2.d..s.BD
00000050: 9386 74d2 e954 b89e e722 3132 2a34 2e22 ..t..T..."12*4."
00000060: 6179 1aa6 55ad c176 932b 6088 d5c5 556c ay..U..v.+`...Ul
00000070: e4f4 5575 12a0 e7fb 1f ..Uu.....
You can add a call to wc or xxd in the bash script to see the byte length or the hexdump I produced above.
Interestingly, this program demonstrates a minor bug (?) in Pyth. Any carriage return bytes (0x0d) are read as newline bytes (0x0a) when in string literals. This forces me to add 6 bytes: X ... ]T]13 to replace the incorrect ten with a thirteen.
Otherwise the idea behind this code was fairly simple: record each position where a run of whitespace occurs. Then, pair each of those indices with the number of spaces in that run. Then, reconstruct the original string by repeating the phrase four times, then inserting in the correct locations. If the number of spaces recorded was zero, instead a newline is inserted.
JavaScript (ES6), 207 205 203 bytes
Returns an array of strings with some trailing spaces.
let f =
_=>[16515968,33489856,k=58950624,k+16,k-40,31458204,7865230,1966983,462847,233471,117670784,k=134185856,k].map(n=>'heanswertolifetheuniverseandeverythingt '.replace(/./g,(_,i,s)=>s[n>>i&i<27?k++%39:39]))
console.log(f().join('\n'));
05AB1E, 83 79 74 bytes
-4 bytes thanks to Erik the Outgolfer
Outputs a list of strings to save a byte.
•2ÖH₆Ôn₅Ò\ÊÑĆ¸Ý¾*£„ÔûC∞qΘœ™‚¹µ—₃₄fm•vNÈy×vyiðë¾¼’€€Ž»to‚쀀ªÜ€ƒ‰Ö’è}}}J27ô
Explanation
2ÖH₆Ôn₅Ò\ÊÑĆ¸Ý¾*£„ÔûC∞qΘœ™‚¹µ—₃₄fm is the base 255 representation of the decimal number:
73869469755353565353431353533323902433339443437469034389033390735363735903735903
Which encodes runs of 1s and 0s respectively, even indices being the 1s and uneven indeices the 0s. This evaluates to the binary number:
111111100011111111000000111
111111000011111100000000011
111110000011111000111110001
111100000011111000111110001
111000100011111000111110001
110001100011111111111000011
100011100011111111100001111
000111100011111110000111111
000000000000111100011111111
000000000000111000111111111
111111100011111000111111000
111111100011111000000000000
111111100011111000000000000
Code
•...• Convert from base 255 to decimal
v } For each digit, do:
NÈy× is_even(index) repeated that many times
v } For each digit, do:
yi } If digit is truthy, then:
ð Push space
ë Else:
¾¼ Get and increment counter, starts at 0
’€€Ž»to‚쀀ªÜ€ƒ‰Ö’ Push "theanswertolifetheuniverseandeverything"
è Get the character at that index
J Join whole stack
27ô Split into parts of 27
Ruby, 216 bytes
o=i=1
puts"6iffnqxq0opdbco5e3f1zk5d7eezo2j6zwly9z5yykqsc1hl5svaof".to_i(36).to_s(17).chars.map{|c|
o=!o
c.to_i(17).times.map{o ? (i+=1)&&"ngtheanswertolifetheuniverseandeverythi"[i%39]:" "}.join
}.join.scan /.{1,27}/
Explanation Similar to the Python solutions but I used Run Length Encoding before converting to base 36. So the data string is only 54 characters instead of 68.
But it still ended up longer over all, hopefully it can be golfed further.
Allow me to answer my own question...
Charcoal, 140 126 112 bytes
A⟦⟧βF⁶⁸F⁻℅§”c#*U[“⎆Vl¶·δ‴ü"Ip8ξZ{e/⪫¦σMM⪫¢Q⁸ULê←⪫W?υ⁻$⌀)”ι³⁴«¿﹪ι²§”m⌊0Y℅¿№XJ-⁵η}Z¿8_*<o%!±”L⊞Oβω ¿¬﹪L⊞Oυω²⁷⸿
You have here a link to the closest verbose version.
Explanation (see verbose version for details):
- The
)%*(+&(+)''%'%'('%'%&%#%'%'%%%$%-&%%%%+&&%&%)&(.&%*.%%2%'%(%)%'.)%'.string (68 bytes, 48 bytes compressed) is a representation of the RLE-encoding of the drawing. Every char code minus 34 is the number of spaces (even positions) or the number of text characters (odd positions) to print consecutively. - The algorithm just decompress the RLE-encoded string and writes the next char of the
gtheanswertolifetheuniverseandeverythinstring (39 bytes, 27 bytes compressed) every time a non-whitespace character is needed to be written. As I check the length of a list to get the next character and that list starts with one element, the last character of the string is written in the first position. - Every 27 characters written I insert a new line.
Acknowledgments
- Many thanks to Neil and his amazing tips for allowing me to save 28 bytes and finally be able to beat Bubblegum. :-)
Python 2, 235 218 213 bytes
x=bin(int('OC5POO6MZYQNBWY0RP6BKBZCOZL13MIAB6I8YZ5N7LXSZBVKX7GC57AW5631YCJ6XCLC',36))[2:].replace('1',' ')
for c in'theanswertolifetheuniverseandeverything'*4:x=x.replace('0',c,1)
while x:y,x=x[:27],x[27:];print y
Switched to a base 36 encoded int of the positions of letters as hinted in thew question.
Replaces each character one at a time.
111 111111 the answer
1111 111111111 toli fetheuniv
11111 111 111 ersea nde ver
111111 111 111 ything the ans
111 111 111 111 wer tol ife the
111 111 1111 uni ver sean
111 111 1111 ---> dev ery thin
111 111 1111 gth ean swer
111111111111 111 tolifetheuni ver
111111111111 111 seandeveryth ing
111 111 111 the ans wer
111 111111111111 tol ifetheuniver
111 111111111111 sea ndeverything
Edit: It seems officialaimm used base 36 before me.
Python 3, 224 220 219 215 211 194 bytes
- Thanks to @TFeld for
15 bytes:if(j%27<1)*jinstead ofif j*(j%27<1)and simplified print statement. - Thanks to @Leaky Nun for 4 bytes:Inverting 0 and 1, did not require the padding by zeros
7*'0' - @Leaky nun saved 17 bytes with his awesome golfing skills(Thanks a lot!!!!): awesome use of modular indexing
i=j=0
k=int("OHZE5WCKDTW6JYMO1JNROAAJQVAN6F8KEO0SMKJM86XIBMCEH5FXXONZGBAVCN3689DS",36)
while k:j+=1;print(k%2*'theanswertolifetheuniverseandeverything'[i%39]or' ',end='\n'*(j%27<1));i+=k%2;k//=2
Explanation:
Uses base-36 compression to compress this binary number(new-line excluded)
111111100011111111000000111
111111000011111100000000011
111110000011111000111110001
111100000011111000111110001
111000100011111000111110001
110001100011111111111000011
100011100011111111100001111
000111100011111110000111111
000000000000111100011111111
000000000000111000111111111
111111100011111000111111000
111111100011111000000000000
111111100011111000000000000
We basically have two counters i and j. On encountering a 1 we print a space; Else if 0 is encountered we print next letter from the string and increase i. j increases for each 0 or 1. We also print new-lines whenever necessary i.e. when j%27<1 becomes true.
Jelly, 83 bytes
“¡eu⁾ṃṣƬİḂṃ½)ṣṾṘƇ@^AṀẆṫ+¢ṗɲ⁾ṭḋZ`⁺×Ṗj½Ṇ-Þḣ2żṿƤc’BCẋ"`“ÆZ⁺ƙ{Æß¥ŀŒ1gỤ3Ḍṭṁ ṃjɓ»ṁȯ€⁶s27Y
How it works
“XX’BCẋ"`“YY»ṁȯ€⁶s27Y
“XX’ a large number
B binary
C complement
ẋ"` 1 becomes [1] and 0 becomes []
ṁ reshape
“YY» "theanswertolifetheuniverseandeverything"
ȯ€⁶ replace [] with " "
s27 split into chunks of length 27
Y join with newline
Retina, 183 bytes
7386¶6469¶555353¶465353¶33135353¶23239024¶133394¶034374¶090343¶090333¶735363¶735903¶735903¶theanswertolifetheuniverseandeverything
.*$
$&$&$&$&
(\d)(.)
$1$* $2$*
+s`1([ 1¶]+)(.)
$2$1
Try it online! Explanation: The first stage adds the RLE encoding of the cell bitmap and the text, which the second stage then duplicates to the correct length, while the third stage decodes the RLE encoding. The fourth stage then moves the text into the cells.
JavaScript, 265 237 219 bytes
(i=0)=>'jwq80,13ntvk,26p62g,25g7w8,239k3c,zg4xc,7rpbk,1e9dc,b8mw,5mbc,4f9reg,4f9r0g,4f9r0g'.split`,`.map(b=>parseInt(b,36).toString(2).slice(1).replace(/./g,c=>' theanswertolifetheuniverseandeverything'[+c||2+i++%39]))
Few bytes off thanks to @tsh.
JSFiddle
SOGL V0.12, 74 bytes
"ō⅓׀?@C⁶¬IΧΖO‘@øŗč"βΘ⅔Μv∙KΩqψ3╥W≡A;2ļm½±iq╗∆Δ⁶Πqīσ‽ε⁰5τΩ⅜δσΞoΤi┘‽N¹\Λ:‘' n
"...‘ push "the answer to life the universe and everything". because of a bug-not-really-bug, the starting quote is required
@øŗ remove spaces
č chop it into characters
"...‘ push a string of spaces and ŗ where ŗ gets replaced with each next characters of the character array
' n split into an array of line length 27
JavaScript, 215 bytes
solution based on guest44851
$=>'a21xb0k9qf30155yiv016ewp3018lkhz0ohfdb077or302cl5b0mgzr0b8hz028ghs7028gi67028gi67'.split(p=0).map(x=>parseInt(x,36).toString(2).slice(1).replace(/./g,x=>" theanswertolifetheuniverseandeverything"[+x&&1+p++%39]))
Retina, 185 bytes
7!8&sw'6Bi6fe!Av¶5#sea5nde5v'4y%g5!5&s¶3w#1B5ife5!¶2A2v#92se&¶1dev3#y9%¶gth4e&7sw'Bife!A4v'se&dev#yth3ing¶7!5&s6w'7B5ife!Av'7sea5ndev#y%g
'
#¶
&
an
%
thin
#
er
!
the
A
uni
B
tol
\d
$*
Add++, 1398 bytes
+32
&
&
&
&
&
&
&
+84
&
-12
&
-3
&
-69
&
&
&
&
&
&
&
&
+65
&
+13
&
+5
&
+4
&
-18
&
+13
&
-104
&
+22
&
&
&
&
&
&
+84
&
-5
&
-3
&
-3
&
-73
&
&
&
&
&
&
+70
&
-1
&
+15
&
-12
&
-3
&
+16
&
-7
&
-5
&
+13
&
-108
&
+22
&
&
&
&
&
+69
&
+13
&
+1
&
-14
&
-4
&
-65
&
&
&
&
&
+78
&
-10
&
+1
&
-69
&
&
&
&
&
+86
&
-17
&
+13
&
-104
&
+22
&
&
&
&
+89
&
-5
&
-12
&
+1
&
+5
&
-7
&
-71
&
&
&
&
&
+84
&
-12
&
-3
&
-69
&
&
&
&
&
+65
&
+13
&
+5
&
-105
&
+22
&
&
&
+87
&
-18
&
+13
&
-82
&
+84
&
-5
&
-3
&
-76
&
&
&
&
&
+73
&
-3
&
-1
&
-69
&
&
&
&
&
+84
&
-12
&
-3
&
-91
&
+22
&
&
+85
&
-7
&
-5
&
-73
&
&
+86
&
-17
&
+13
&
-82
&
&
&
&
&
&
&
&
&
&
&
+83
&
-14
&
-4
&
+13
&
-100
&
+22
&
+68
&
+1
&
+17
&
-86
&
&
&
+69
&
+13
&
+7
&
-89
&
&
&
&
&
&
&
&
&
+84
&
-12
&
+1
&
+5
&
-100
&
+93
&
+13
&
-12
&
-72
&
&
&
&
+69
&
-4
&
+13
&
-78
&
&
&
&
&
&
&
+83
&
+4
&
-18
&
+13
&
-104
&
+106
&
-5
&
-3
&
-3
&
-3
&
-1
&
+15
&
-12
&
-3
&
+16
&
-7
&
-5
&
-73
&
&
&
&
+86
&
-17
&
+13
&
-104
&
+105
&
-14
&
-4
&
+13
&
-10
&
+1
&
+17
&
-17
&
+13
&
+7
&
-5
&
-12
&
-72
&
&
&
+73
&
+5
&
-7
&
-93
&
+22
&
&
&
&
&
&
&
+84
&
-12
&
-3
&
-69
&
&
&
&
&
+65
&
+13
&
+5
&
-83
&
&
&
&
&
&
+87
&
-18
&
+13
&
-104
&
+22
&
&
&
&
&
&
&
+84
&
-5
&
-3
&
-76
&
&
&
&
&
+73
&
-3
&
-1
&
+15
&
-12
&
-3
&
+16
&
-7
&
-5
&
+13
&
-17
&
+13
&
-104
&
+22
&
&
&
&
&
&
&
+83
&
-14
&
-4
&
-65
&
&
&
&
&
+78
&
-10
&
+1
&
+17
&
-17
&
+13
&
+7
&
-5
&
-12
&
+1
&
+5
&
-7
&
P
It looks like hardcoding it is the shortest way (at least in Add++)
Bubblegum, 125 bytes
0000000: 45 8f c5 01 03 31 0c 04 ff ae 62 4b 0b 99 02 c7 E....1....bK....
0000010: d8 7d 84 e7 f9 59 30 5e 41 59 4a 84 71 ef e6 3d .}...Y0^AYJ.q..=
0000020: 4e c1 ea fd b7 42 48 91 66 d6 ae 6e da 89 d3 1c N....BH.f..n....
0000030: ef 60 ba 97 ae 6e b6 74 2e a5 76 d9 ad ae e4 16 .`...n.t..v.....
0000040: 69 59 08 a6 a6 e8 23 d4 22 af 08 d0 20 7d 17 f0 iY....#."... }..
0000050: 8a 9b 7c 76 c2 61 7b c8 4b 01 41 23 50 24 32 87 ..|v.a{.K.A#P$2.
0000060: f5 98 9e 88 35 24 21 83 ac 50 b2 e0 a2 16 0e 42 ....5$!..P.....B
0000070: bb ba a5 bc ae 6e bd 76 b7 69 d9 f9 07 .....n.v.i...