Digitising the Library of Babel

8

1

The Library of Babel is defined as

a universe in the form of a vast library containing all possible 410-page books of a certain format and character set.

For the purposes of this question, each page of the book contains 40 lines of 40 characters each. The formatting rules are as follows:

  1. Legal characters are the English Latin alphabet a-z,A-Z and the punctuation characters , . (comma, space and full stop).
  2. Each page contains at least one space, one full stop, one capital character and one comma.
  3. There must be a space after every full stop and comma. No combination of the punctuation characters , . may be present more than twice in a row, and identical punctuation marks cannot exist consecutively.
  4. All alphabetic characters immediately after a full stop and its accompanying space must be capitalised. This applies unless the full stop is the last or second last character on a page. Capital characters may not occur anywhere else.

Challenge

Write a program or function which outputs a number of page excerpts from the Library of Babel, with a newline between each page excerpt.

Most importantly, the program must be able to theoretically perfectly replicate the entire Library of Babel if given an infinite number for the number of page excerpts to produce and a truly random RNG (Pseudorandom random number generators are allowed in the code, but assuming the PRNG is replaced with a true RNG, the code should theoretically produce the entire library).

Since this is code golf, the shortest answer will be accepted.

An example of valid output (for 2 pages):

, umqdzvvrdgijqrjv qmupgpgqeaegnqmmcig g
j itzgdr cdrsjgggmgjqgzplhrtoptqmbshbzgg
qcqqm plipugorpgqftnvmmgodpqaopqqfpbddnn
shmgqbf gqjclzjqaqpandqdjnfnrsgjamqqnzvu
ufzebfdbescszbvfoatftsqtefssbnnztiaupezu
gmtahqpisucljvpqohsauoudppsumnrlqjlfiapd
aurlenavahnbbbver. Lvjbgilspusdunufrgfng
nghpbcusnscibepjqhgjgcemzjdcqqjdprsrunih
emqgrfrgoqrnsvqofbrvsaqhsrqndhuhulolrese
visneqbjssfjtdrmuqq tfseuczgvbuetgijhtcs
aqnubefzuet. Gjctngshammusvdaogamtuo ajs
edjbctevfdnodgg jubshhsuaniqnsqrvatquqtg
bgoffnd hepqngjnusmglcqgdse q rdlzjqggfr
evvrcfezqqditbpztttirravbzegzleafashfzed
nhngjcccvzmmnmgpimoeztfcu laupeg rogtrip
nuaaghqptet, gss csrszqpnmvgcevqqzrfegus
lnninfajhsaapgpcljiooednbusfbmcmgqunvvzd
o dcztfgdovr. Ru, gvjchisbvo sslpzolptss
hnjbmovglvbu idqlgehrztfshrhbigivijh foi
bqcqggpjfus qmdituqucqdqgogzuvevlhujojuu
dnsuqvcsptnsjrqiezfqrjucsongqgbbutvsoqld
s vbollthqsgambsaoqgvgapszhgvsjnqepeqgvv
pqzpbqhcmfbhfcz gdeuzogmtzqjvohldeafchip
olztntqn vgtiofcjzmfluqoqruaqcqmnmfafvnc
ruzahccrqpzji, gdrhcu pgjuhqqvsgppgzrioq
qjgh oapzdiusqem ubtnhhbpdt sjzbidggsgve
ocdiuungsghltqsuezoco qqgcpnupiisnrngetq
lbjdrbqzqnqmj crgreugncqaulhqegjfq. Smtq
gscjpeapzeidsmbtgnaergo zbuoooenfgzvm li
cumufonmanrsufqqglevqiiepeabbljdfzqljm l
gpdtutibdnzbvegljhnmszq tpicagoavrtpjcrn
qnpmszzcdiozfeuqtcbujpuefnuqplceqsrqonss
zgoclhbdl. Fsrgqeuouergciq. Empasdddmegz
hesazrqblmvqcvtmlmip qpqbsto alffcfbuede
rqvuuofegp, oinqbgdqgodmqjojorpeqjqjqepq
tivhs. Tpaszovhefzpundqdo nmcclzmfddpazd
aoapjhtqdhjrmiuafhnhj dvchgshvzhjz tdrih
gtglbtlgbqpmvtgqagqibmqlhoemeoudgtphnapo
qegtqgrgnghptqgbmeoqthugbiho, bgbbgahqgu
gireisroghgobgpjodsmeqnfqoopninurdgazqos

. Rnffadjgqpgzngbpeztqrpuagprscopqnuzntf
upvamihntmjrcbfzbnmsfpsueepbsogmdcnpluci
fnoioffqtsitgatmfhn zuiozvpgprpvdpesujhn
bqlhqdtvmgtiabhz zdmothrhniczqqtgbg eatr
gmognhdjegzsaei. Gloiampbtmgtetoq gdfjoj
rlbrpjogclullgonomttrrshpolujodajesopsfr
cabnhbohntadjtltsztbvzcebvdqnjhplicslco
vabftndmi netqjv. Rl tmicpanralmamruovfi
ssnmh gtlsqilcrnrsaislut mnzhsfojtmsqtlb
mbl dcjvuldnvtfsjsp, urundndz ehopedrptm
abtvadojvdbe tolzmotjtbiigmazlmznjrnof i
ioogsospoghiojshqdimpuibeaosflbpjqvcdord
uomtjebohjmzeutjvmca eq, ljttbbtaomjsoja
z crvmmhonescmnovmnznf brhufrudosfensru
fdotflmug gclmondggitamnsrsfjiaqnootlanr
tuiftvia. Rccnvuebeocub, unihegbtqgslps
ngsqjflejnitpcorluetbiacgqjtbtroejeopqso
ca sdziornhsvivcteaadspibt qippmqmduinia
oqtvoo ttt qnfrfchpszaiiecsrmsrblmbojot
h rtjzqqo esufacjdvbposroesoorszoiutfloe
hs bt eibajrocingbmvterhomfuzttemvfjtltv
lcmoevnlzofpd zsjbmgvllhgrsoclvnnhnscodo
tjocglurng hrhp og bstpeabzdaocoircqb go
devvmlslsqnjibpjcmndmqdvgfmhcsvgzrbicuuv
jgfthudlg. Tfsnn tuarpgogh ibsemtjfgdisj
tgcetprzomzfadthmrroivoheepingsmovrmjiit
ufdvi cvpti oift. Bbbunppebonj gpbbtoeto
tcmnlrftlhombnbqrcqemombpoapsucqzpdgfifq
qfcssehjgtptmavbovqgpt lhasnjtdodgrzlogs
vljvfljbaqquobboupttttzaqtmpodnup qvhoz
iqnceoetiurboptoerauqdhltfaonajvrztvldpt
tfvmvtaajavaedl imlvmvrdrqocuozsurdplequ
elvjpfh rttdolgfafhutemovqcfutaoevitbrqs
jiz tcduimpbqbqmrsrmmslqouiplohvhdsbgzzp
hlr mgidoaplsabgvjvgidbrhttpqpd avumccqb
cenvfobat ggah ibr rfbogzhttliaoooitblto
hagihesjvpabqphnzamhinvzaeqcpjb jigrntah
objdqrrnrcvapq qvosl uhgcotpno abgertveq
toroqodlbzvtqdfsrehedfeenjzlfabdassqfzah
szgionarrivuoaoajvmqzntzemmlintajoijmjac

March Ho

Posted 2015-08-14T14:19:39.780

Reputation: 296

@Vioz- I am assuming that the RNGs are all not truly random, and they don't need to be. What I mean is that if the PRNG was made truly random, it should be able to produce the library in its entirety. – March Ho – 2015-08-14T14:45:20.337

"Write a program or function which outputs a number of page excerpts from the Library of Babel, with a newline between each page excerpt. " I don't understand this. The example output looks like one page from a book to me, not a number of pages – Fatalize – 2015-08-14T14:49:11.713

1Can a page start or end with a symbol? Can there be a space before a symbol? – Kade – 2015-08-14T14:53:20.417

@Vioz- Yes, it can, as long as all other rules are followed. Since both non-space symbols must be followed by a space, there cannot be a space before a symbol as this would violate the <=2 symbol rule. – March Ho – 2015-08-14T14:55:07.910

I assume no outputed page can ever be a duplicate of another one? – Fatalize – 2015-08-14T14:56:35.317

Well because in that case the condition that the program should "theoretically perfectly replicate the entire Library of Babel " is kind of meaningless because a program that generates a 40 lines 40-chars wide page with completely random characters will eventually replicate the library of babel, given (a lot) of time – Fatalize – 2015-08-14T15:02:54.003

@Fatalize The idea is to weed out programs that generate an entire field of alphabetic characters and convert the first few characters into punctuation (or similar loophole abuse). – March Ho – 2015-08-14T15:04:17.033

Your first line is 41 characters. How is that legal? – Not that Charles – 2015-08-14T15:05:52.623

@NotthatCharles Made a typo while editing in some additional concerns, fixed. Thanks. – March Ho – 2015-08-14T15:06:32.953

1The only part of point 3 I'm certain I understand is the first sentence. What do you mean by "twice in a row" and, in particular, what is the distinction between that and "consecutively"? – Peter Taylor – 2015-08-14T15:08:11.893

@PeterTaylor (,|\s|\.){3} is illegal – March Ho – 2015-08-14T15:14:58.427

2Ok, so it comes down to "There must be a space immediately following every full stop and comma, and a letter immediately following every space". Modulo the cases which would make it contradict point 4, which allows non-alpha characters to end the page. – Peter Taylor – 2015-08-14T15:35:39.323

Answers

2

Pyth, 125 72 bytes

VQV1601=+kO+G",. "Iqek\,=+k+dOG)Iqek\.=+k+dOrG1)Iqekd=+kOG))VhcckJ40JN)d

Try it online here.

I finally golfed this down to a reasonable size. And by "golfed down" I mean "rewrote entirely". It still doesn't meet the requirements perfectly, but it's better than the previous iteration. I even have a breakdown now:

  O+G",. "     Randomly choose a lowercase letter (variable G), comma, period, or space
  +k           Append it to variable k (initialized to empty string)
  Iqek\,       If the last character of k is a comma:
   =+k+dOG)     Append a space (variable d) and a random lowercase letter to k
  Iqek\.       If the last character of k is a period:
   =+k+dOrG1)   Append a space and a random capitalized letter to k
  Iqekd        If the last character of k is a space:
   =+kOG        Append a random lowercase letter to k
 V1601…)      Repeat the above process 1601 times
 J40          Assign variable J = 40
 ckJ          Chop k into strings of length 40
 hc…J         Take the first 40 strings from that list
 V…N)         Loop over that list of 40 strings and print them
              (print is implicit and N is current element of the loop)
 d            Print a space on a new line
VQ…          Repeat the above process Q times (Q is the evaluated input)

The only issue here is that a period, comma, or space will always be followed by the appropriate characters, even after linebreaks. For example, if a line ends with a period, the next line will always start with a space followed by a capital letter. I do not think this is what the question asked for.

Mike Bufardeci

Posted 2015-08-14T14:19:39.780

Reputation: 1 680

2

Lua, 353 346 bytes

Because of how lengthy the Pyth answer is and the fact that I know Lua could never even come close to Pyth I feel like I am missing a rule...

Code:

math.randomseed(os.time())g="abcdefghijklmnopqrstuvwxyz ,."for i=1,io.read() do 
print(" ")for i=1,40 do k=0h=0v=0 print("")for j=1,40 do c=math.random(1,#g-h-v)t = g:sub(c,c)if v>0 then v=0 end
if r==1 then r=0;t=t:upper()end if c>26 then v=2;if t=="." then r=1 end k=k+1;if c>27 then t=t.." " end if k>1 then h=2 end end io.write(t) end end end

Probably can be golfed down quite a bit, but this is it for now.

Nico A

Posted 2015-08-14T14:19:39.780

Reputation: 2 390