Anonymous prefix lambda. Returns a program body.
{
1=≢⍵:⍕⍵ ⍝ single element
(2=≢⍵)∧(⍵[2]=11×⍵[1]):⍕⍵[1] ⍝ 2, 22 etc.
1=≢∪⍵:'⊢',⍕⊃⍵ ⍝ all the same
(⊢≡⊃×⍳∘≢)⍵:'+',⍕⊃⍵ ⍝ linear
((⌊=⊢)!⍣¯1⊢⊃⍵)∧(1∧.=1↓⍵):'!',⍕!⍣¯1⊃⍵ ⍝ factorial followed by all 1s
(⍵[2]∧.=1↓⍵)∧(⍵[1]=10|2⊃⍵):(⍕⊃⍵),'⌈',(⊃⍕2⊃⍵) ⍝ b ab ab ab
e←{∊⍉2 2⍴'+×',⍕¨⍵}¨⍸(⊃⍵)=∘.×⍨⍳10
b←⍵∘≡¨e(({0::⍬ ⋄ ⍎⍵}¨,\)⍴∘⊂)¨⍨(≢⍵)
∨/b:⊃b/e
Q←{'''',⍨⍵/⍨1+''''=⍵}
(5∧.≤⍵)∧(≢⍕⍵)>6+(+/14=⍵)+≢⍵:'{⍺←⎕AV⍳⊃⋄1⌽⍺⊢⍵}''',Q ⎕AV[⍵] ⍝ string fallback
(≢⍕⍵)>9+(+/5=⍵)+≢⍵:'{⍺←¯4+⎕AV⍳⊃⋄1⌽⍺⊢⍵}''',Q ⎕AV[4+⍵] ⍝ offset string fallback
'{⍺←⊃⋄1⌽⍺⊢⍵}',⍕⍵ ⍝ fallback
}
Try it online!
Methods
This explores various methods and returns the first usable one, eventually falling back to a universally applicable method.
Single element
If the list has only one element, it is returned as-is.
2, 22 etc.
A single digit can just be repeated to generate the number 11 times bigger,
All the same
We just return the rightmost (⊢
) number.
Linear
f(n) = k×n sequences just insert a plus before the first term.
Factorial followed by all 1s
When the first number n=!m and subsequent numbers are 1, then !m
is a solution because !m
is n and m!m
is 1 and !1
is 1.
b ab ab ab
Since all two-digit numbers are larger than all single-digit numbers, a running maximum, where the front of the first number is glued to the back of the second number, is a solution.
The three-line-code
Check whether any formula of the type +a×b
is valid.
String fallback
Long sequences with no numbers under 5 (because 4 is a line break) can be encoded as characters of the SBCS.
Offset string fallback
If there are numbers under 5, we shift up by 9 to avoid those.
Fallback
Simple string concatenation of the string "{⍺←⊃⋄1⌽⍺⊢⍵}"
and the stringified (⍕
) input. E.g. [3,1,4]
returns the program body {⍺←⊃⋄1⌽⍺⊢⍵}3 1 4
.
The part in braces is an ambivalent function which means it can be either a prefix function or an infix function. Thus the leftmost instance of it will run in prefix mode, and all the others in infix mode. The difference between the modes is whether ⍺
, signifying the left argument, has a value. If it doesn't then it will be assigned the function ⊃
(first).
Explanation of fallback method
{
…}
anonymous lambda:
⍺←⊃
If there is no left argument (⍺
) assign the function ⊃
(first) to ⍺
⋄
then:
At this point, the following code means two different things depending on whether ⍺
is a list of numbers (infix call) or the function "first" (prefix call).
If ⍺
is a list of numbers:
⍺⊢⍵
discard the left argument in favour of the right argument
1⌽
rotate that one step left
If ⍺
is the function "first":
⊢⍵
yield the right argument
⍺
pick the first element of that
1⌽
rotate it one step (a no-op on a scalar)
Example run of fallback method
Executing 3 1 4
's code, {⍺←⊃⋄1⌽⍺⊢⍵}3 1 4
, assigns the "first" function to ⍺
and thus returns the first element; 3
.
Executing {⍺←⊃⋄1⌽⍺⊢⍵}3 1 4{⍺←⊃⋄1⌽⍺⊢⍵}3 1 4
lets the rightmost lambda "capture" the left 3 1 4
as its left argument, so ⍺
has a value which is discarded in favour of 3 1 4
which is then rotated one step left and yields 1 4 3
as result. This is then used as sole argument to the leftmost lambda, where ⍺
becomes the "first" function, causing the result to be the first element; 1
.
Executing {⍺←⊃⋄1⌽⍺⊢⍵}3 1 4{⍺←⊃⋄1⌽⍺⊢⍵}3 1 4{⍺←⊃⋄1⌽⍺⊢⍵}3 1 4
lets the rightmost lambda "capture" the middle 3 1 4
as its left argument which is then discarded in favour of the right argument, 3 1 4
, which when rotated one step left is 1 4 3
. This is then used as right argument of the middle lambda together with the leftmost 3 1 4
as left argument. The left argument is discarded for the right, which rotated one step left yields 4 3 1
. This then becomes the sole argument of the leftmost lambda, so ⍺
becomes the "first function", returning the first element; 4
.
Scoring
When it becomes time to test using actual data, use this test harness (linked populated with pretest data). The test cases go in the Input field, and the Output will be the total byte count of all 500 programs together. (It will also throw an error, but that's just because it afterwards tries to evaluate the Input as-is.)
1What does "repeated for k times" means? NewSourceCode = repeat SourceCode k times? e.g. SourceCode = "ABC", k = 3, then NewSourceCode = "ABCABCABC"? – tsh – 2018-03-16T07:14:56.350
print(end="1");
repeated for 2 times isprint(end="1");print(end="1");
– l4m2 – 2018-03-16T07:15:30.2931What does "sum of code lengths" means? Should we submit more than one program? – tsh – 2018-03-16T07:17:08.700
You submit one program which generates 500 programs – l4m2 – 2018-03-16T07:18:26.520
So submit a program that generates a list of 500 programs where the first program outputs the first element of the input list, the second program is the first program repeated twice and outputs the second element of the list and so on? – Emigna – 2018-03-16T07:26:42.300
@Emigna To my understanding, that's no different from
LengthOfSingleSourceCode * 1275
... – tsh – 2018-03-16T07:28:41.590Will the programs our program generate have access to the input? – Emigna – 2018-03-16T07:30:42.763
@Emigna I'm assuming not- why even bother with a generator if that's the case? – Chris – 2018-03-16T07:48:28.597
2@Emigna No, for each list it generates a single program. Say that program is just
x
. Thenx
should give the first element of the list,xx
should give the second element of the list,xxx
should give the third, and so on. – Chris – 2018-03-16T07:50:34.913500 programs because there are 500 test cases – l4m2 – 2018-03-16T08:04:50.910
It looks people are just generating code that indexes into the array written verbatim and counts up the index with each copy. This means the only role the array plays is to potentially be compressed into the code, which strikes me as a chameleon challenge. – xnor – 2018-03-16T11:10:44.840
I meant, e.g. for an array of length 1, they can directly output the number. That kind of optimization may appear? as I don't limit the generator length @xnor – l4m2 – 2018-03-16T11:12:48.417
That's an interesting thought, I hadn't considered special-casing small lengths. I still expect array compression to be a much bigger factor though. – xnor – 2018-03-16T11:19:37.007
I expect the winner will handle both special cases and general compression well. – recursive – 2018-03-16T22:33:03.483
Me too. Now how to move the comment into chat? – l4m2 – 2018-03-17T06:56:51.267
Will the input always be a non-empty list of integers in the range 1–100? – Adám – 2018-03-17T20:24:43.640
@Adám Not sure about 1 to 100, but the porbably of
1
is15%
,2
is12.75%
,3
is10.8375%
... I don't know the max number in the possible seeds – l4m2 – 2018-03-18T12:42:46.980Do we need to allow for modular indexing - i.e., if the array is length 5, and the code is repeated 7 times, do we still need to return the 2nd element? – Sok – 2018-03-19T14:39:38.693
@Sok no as
print(end="1");
doesnt – l4m2 – 2018-03-19T14:46:08.257@l4m2: Is this going to be scored? – recursive – 2018-03-27T14:12:12.607