14
3
Code golf always involves some answers that bend the rules more or less by breaking constraints that the challengers took for granted or just haven't thought about and didn't list in the rules. One of these interesting loopholes is the possibility to output more than the challenge asks for to get a better result.
Taking this to extremes, we can write an universal code golf solver that prints the desired output — if you don't care that it might take ages and outputs lots of other stuff before and after it.
All we need to output is a sequence that is guaranteed to contain every possible subsequence. For this code golf, this will be the Ehrenfeucht-Mycielski sequence:
The sequence starts with the three bits 010; each successive digit is formed by finding the longest suffix of the sequence that also appears earlier within the sequence, and complementing the bit following the most recent earlier appearance of that suffix.
Every finite subsequence of bits occurs contiguously, infinitely often within the sequence
The first few digits of the sequence are:
010011010111000100001111... (sequence A038219 in OEIS).
Combining 8 bits of the sequence to a byte, we'll get ASCII output that we can output to the screen or to a file and that contains every possible finite output. The program will output parts of pi, the lyrics of “Never gonna give you up”, some nice ASCII art, its own source code, and everything else you could want it to output.
For testing correctness, here are hashes for the first 256 bytes of the sequence:
MD5: 5dc589a06e5ca0cd9280a364a456d7a4
SHA-1: 657722ceef206ad22881ceba370d32c0960e267f
The first 8 bytes of the sequence in hexadecimal notation are:
4D 71 0F 65 27 46 0B 7C
Rules:
Your program must output the Ehrenfeucht-Mycielski sequence (nothing else), combining 8 bits to a byte/ASCII character.
Shortest program (character count) wins. Subtract 512 from your character count if you manage to generate the sequence in linear time per generated byte.
The longest suffix in 010 which appeared earlier in that sequence is 0, isn't it? And the most recent earlier appearance is just the second 0. And until now, nothing follows the second 0, so there is nothing we can build a complement on. I'm not a native english speaker - maybe I just got it wrong. The wikipedia article uses the same words, but has a longer sequence so I would name it "the most recent ... which has a follower". – user unknown – 2012-06-09T02:06:37.563
8Pedantic quibble: pi will never appear - only every finite string will be contained in the output. – Keith Randall – 2012-06-09T02:55:23.430
I have another question: May a repetition overlap? For example in 111, (1[1)1]? – user unknown – 2012-06-09T03:01:25.593
@KeithRandall: I would prefer a sequence which is guaranteed not to contain 'Never gonna give you up' and productions of similar kind. – user unknown – 2012-06-09T03:04:34.310
@userunknown I think they're just relying on the idea that the most recent suffix is not "earlier" in the string, being at the end, so it is automatically not considered. – breadbox – 2012-06-09T05:33:26.417
@breadbox: Maybe you can clarify, why the first triple-one: 111 is followed by a zero? Since it is the first 111, we have to search for a shorter sequence - maybe 11? There is the overlapping 11 which is followed by a 1, so the next character has to be a 0. The 11 to the left is followed by 0 so this would lead to another 1. So we have to accept overlaps? – user unknown – 2012-06-09T06:06:06.330
@userunknown Or look at it this way: if you don't defined "earlier" to exclude the end of the string, then it the longest match will necessarily always be at the end. Which is obviously uninteresting, and therefore excluded. Think like a mathematician instead of a programmer and it makes perfect sense! – breadbox – 2012-06-09T06:16:01.293
@KeithRandall: Of course. Corrected now. – schnaader – 2012-06-09T09:50:36.140
@userunknown: It says "complementing the bit following the most recent earlier appearance of that suffix". "Most recent" will lead you to the 11 "most right", but "earlier appearance" excludes overlappings with the suffix (010011010111), so the 11 you choose is the other one: 010011010111 – schnaader – 2012-06-09T10:00:15.430
@schnaader: But then a 0 follows, you have to complement it, which leads to 1, and you would end with a series of 4 times 1 - but there is only 111. – user unknown – 2012-06-09T11:26:45.620
@userunknown: Sorry, messed it up. Earlier appearance includes overlappings. Every position is fine as long as it's not the suffix itself which isn't followed by a bit. We choose the rightmost match position. So it is the other way round, we are choosing 010011010111 indeed, so a 0 is added. – schnaader – 2012-06-09T11:34:35.530
@schnaader: But you have to test out the example that far too, to find that out; don't you? It's not clear from the description of the rules; nor at wikipedia, and more so not here. – user unknown – 2012-06-09T12:45:43.447
2
It might be worth mentioning that the mere presence of an "answer" embedded at an unspecified location in an infinite string cannot be regarded as "outputting" that answer, of course. Also, this particular sequence is just one example of a disjunctive sequence -- there are infinitely many sequences like this.
– r.e.s. – 2012-06-09T14:16:44.640