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.)
print(end="1");
repeated for 2 times isprint(end="1");print(end="1");
\$\endgroup\$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. \$\endgroup\$