23
\$\begingroup\$

Simple challenge: try to output the following text in as few bytes as you can:

       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

The original drawing contains 332 characters.

Rules

  • No input or an unused input.
  • Output can be in any reasonable format (string, list of strings, matrix of chars and so on).
  • You can use uppercase instead of lowercase for the drawing if your prefer so.
  • Trailing whitespaces and newlines allowed.
  • This is , so may the shortest program/function for each language win!

Notes

  • This has been identified as a possible duplicate of We're no strangers to code golf, you know the rules, and so do I. That question was about searching and replacing text. Here you have to draw a shape using a given text, and at least two of the current answers demonstrate that golfing languages can beat plain compression algorithms in this case.
\$\endgroup\$
  • 2
    \$\begingroup\$ @cairdcoinheringaahing it was demonstrated in the sandbox that golfing languages can beat plain compression algorithms in this case. \$\endgroup\$ – Charlie Jul 19 '17 at 6:40
  • 1
    \$\begingroup\$ Personally, I think this is on-topic and not a dupe. I don't think you need a spoiler quote, though. \$\endgroup\$ – programmer5000 Jul 19 '17 at 10:15
  • 1
    \$\begingroup\$ @Phoenix I don't think this is a dupe, sogl has outgolfed bubblegum. Therefore, compression is not an optimal way of doing this. \$\endgroup\$ – programmer5000 Jul 19 '17 at 10:34
  • 1
    \$\begingroup\$ To be honest, I didn't realize my reopening vote was binding. For the reasons already mentioned by @programmer5000, I really don't think it's a dupe of We're no strangers.... It may be a dupe of another -- yet to identify -- challenge, but not this one. \$\endgroup\$ – Arnauld Jul 19 '17 at 12:41
  • 1
    \$\begingroup\$ @Nacht the best part is that at first I drew the 42 with * characters, and then replaced them with the text, which fit perfectly. I did not have to modify the original drawing to make the sentence fit. Coincidence? \$\endgroup\$ – Charlie Jul 20 '17 at 6:53

22 Answers 22

21
\$\begingroup\$

Python 3, 224 220 219 215 211 194 bytes

  • Thanks to @TFeld for 1 5 bytes: if(j%27<1)*j instead of if 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

Try it online!

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can save 1 byte by changing if j*(j%27<1): to if(j%27<1)*j: \$\endgroup\$ – TFeld Jul 19 '17 at 8:41
  • 2
    \$\begingroup\$ 205 bytes \$\endgroup\$ – Leaky Nun Jul 19 '17 at 9:52
  • 2
    \$\begingroup\$ 201 bytes with integer arithmetic \$\endgroup\$ – Leaky Nun Jul 19 '17 at 9:55
  • 2
    \$\begingroup\$ 200 bytes with modular indexing \$\endgroup\$ – Leaky Nun Jul 19 '17 at 9:56
  • 2
    \$\begingroup\$ 194 bytes \$\endgroup\$ – Leaky Nun Jul 19 '17 at 9:59
8
\$\begingroup\$

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

Try it online!

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.

\$\endgroup\$
7
\$\begingroup\$

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ How did you generate this? The closest I could get was 127 \$\endgroup\$ – musicman523 Jul 19 '17 at 6:23
  • 1
    \$\begingroup\$ With zopfli. \$\endgroup\$ – Dennis Jul 19 '17 at 6:25
  • \$\begingroup\$ I did use zopfli, but I swear I tried with and without trailing newlines, with extra spaces....couldn't get under 127 \$\endgroup\$ – musicman523 Jul 19 '17 at 7:14
6
\$\begingroup\$

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ô

Try it online!

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
\$\endgroup\$
  • \$\begingroup\$ Replace "ÔAo îιË3š1ĆRÕ₃FWš{ÓÛÏ.!XµM§&¶ñD°3PŸ{óNι2Ðbмh"253ö with •—."Ôq‚Ā0Ál0j¿«ªžé¨0õ₄7“Ÿ!½ÏiæÆø-δq–Å05q½Yñá+• for -4. \$\endgroup\$ – Erik the Outgolfer Jul 19 '17 at 12:42
  • \$\begingroup\$ @EriktheOutgolfer thanks, updated my answer and now I finally know what these are for :) \$\endgroup\$ – kalsowerus Jul 19 '17 at 12:54
  • \$\begingroup\$ Nice answer :). Hope you continue to learn the language. If you have any questions feel free to chat me. \$\endgroup\$ – Magic Octopus Urn Jul 20 '17 at 0:21
  • \$\begingroup\$ @MagicOctopusUrn Thanks a lot! :) \$\endgroup\$ – kalsowerus Jul 20 '17 at 5:59
5
\$\begingroup\$

SOGL V0.12, 74 bytes

"ō⅓׀?@C⁶¬IΧΖO‘@øŗč"βΘ⅔Μv∙KΩqψ3╥W≡A;2ļm½±iq╗∆Δ⁶Πqīσ‽ε⁰5τΩ⅜δσΞoΤi┘‽N¹\Λ:‘'    n

Try it Here!

"...‘              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
\$\endgroup\$
5
\$\begingroup\$

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υω²⁷⸿

Try it online!

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 gtheanswertolifetheuniverseandeverythin string (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. :-)
\$\endgroup\$
  • 1
    \$\begingroup\$ If you use Assign(Minus(1, c), c); then you can just use if (c) { ... } else Print(" ");. \$\endgroup\$ – Neil Jul 19 '17 at 9:27
  • \$\begingroup\$ Actually if you use for (68) and AtIndex("...", i) then you can use Modulo(i, 2) instead and you don't need c at all. \$\endgroup\$ – Neil Jul 19 '17 at 9:33
  • \$\begingroup\$ And if you use if (Not(Modulo(Length(PushOperator(u, w)), 27))) then you don't need a either. \$\endgroup\$ – Neil Jul 19 '17 at 9:35
  • \$\begingroup\$ Oh, and AtIndex does an automatic Modulo so Modulo(b, 39) can be just b. \$\endgroup\$ – Neil Jul 19 '17 at 9:36
  • 1
    \$\begingroup\$ u is a predefined variable which is just an empty array (saves using A⟦⟧υ). Every time you push something to it its length increases by 1. This is shorter than an explicit assignment to increment. \$\endgroup\$ – Neil Jul 19 '17 at 9:44
5
\$\begingroup\$

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

\$\endgroup\$
5
\$\begingroup\$

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

Try it online!

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.

\$\endgroup\$
  • \$\begingroup\$ +1 for different approach. You can save a byte by using "theanswertolifetheuniverseandeverything"[w%39] TIO \$\endgroup\$ – officialaimm Jul 19 '17 at 12:54
  • \$\begingroup\$ Thanks and noted. I was trying to work it in along with an 'or' replacement for the bracket boolean setup I have here now, but it's messing up my newlines... \$\endgroup\$ – Coty Johnathan Saxman Jul 19 '17 at 13:12
  • \$\begingroup\$ I didn't know PNG used this compression algorithm - I thought that was GIF. \$\endgroup\$ – wizzwizz4 Jul 19 '17 at 18:31
  • \$\begingroup\$ Nice catch. It wasn't PNG (nor GIF), but actually RLE. I'll add a link to the answer. \$\endgroup\$ – Coty Johnathan Saxman Jul 20 '17 at 2:22
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

Jelly, 83 bytes

“¡eu⁾ṃṣƬİḂṃ½)ṣṾṘƇ@^AṀẆṫ+¢ṗɲ⁾ṭḋZ`⁺×Ṗj½Ṇ-Þḣ2żṿƤc’BCẋ"`“ÆZ⁺ƙ{ÆߥŀŒ1gỤ3Ḍṭṁ ṃjɓ»ṁȯ€⁶s27Y

Try it online!

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
\$\endgroup\$
3
\$\begingroup\$

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

Try it online!

It looks like hardcoding it is the shortest way (at least in Add++)

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

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
\$\endgroup\$
3
\$\begingroup\$

Jelly, 76 bytes

“¡¢ʋỵṆ⁻kỴ⁷ṁḤæ ƊVṛĠ¥¡¢tṢ}ȧƘ=ẒṆ_`-p1ḷṠ’ḃ⁴ĖŒṙḂ¬R“£:(ḥB⁼Ṇ#¥Ṡ1ɗĠðȮ $¿⁹½ɓ»ṁȯ€⁶s27Y

Try it online!

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

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
$* 

Try it online!

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

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]))
\$\endgroup\$
2
\$\begingroup\$

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}/

Try it online!

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.

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

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

Try it online!

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

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

\$\endgroup\$
  • \$\begingroup\$ .substr -> .slice(1) save some bytes \$\endgroup\$ – tsh Jul 19 '17 at 7:25
  • \$\begingroup\$ "Output can be in list of string" so just skip the join \$\endgroup\$ – tsh Jul 19 '17 at 7:40
  • \$\begingroup\$ @tsh. Thanks. . . \$\endgroup\$ – user72349 Jul 19 '17 at 8:38
1
\$\begingroup\$

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

Try it online!

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.

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

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

Try it online!

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!
\$\endgroup\$
  • \$\begingroup\$ You can save some bytes with int[]{16515968,33489856,x=58950624,x+16,x-40,31458204,7865230,1966983,462847,233471,117670784,x=134185856,x}. \$\endgroup\$ – Charlie Jul 20 '17 at 8:23
1
\$\begingroup\$

C (gcc), 220 bytes

f(q,i,j){char*t="GCHF:FDFI:EEECEC:DFECEC:CCACECEC:BCBCKD:ACCCID:@CDCGD:@LDC:@LCC:GCECFC:GCEL:GCEL";for(q=i=0;*t;t++)for(j=*t^58?q=!q,*t-64:1;j--;)putchar(*t^58?q?32:"theanswertolifetheuniverseandeverything"[i++%39]:10);}

Try it online!

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

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.

\$\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.