| Bytes | Lang | Time | Link |
|---|---|---|---|
| 027 | JavaScript V8 | 250218T194357Z | Weird Gl |
| 261 | PHP | 170227T194854Z | Divcy |
| 052 | Perl | 170227T181654Z | ThisSuit |
| 067 | Python 2 | 170227T025517Z | math jun |
| 017 | CJam | 170227T124835Z | Martin E |
| 066 | Ruby | 170227T004032Z | Value In |
| 172 | PowerShell | 170227T022105Z | briantis |
| 059 | Haskell | 170227T011054Z | nimi |
| 007 | Vim | 170227T005314Z | DJMcMayh |
JavaScript (V8), 27 bytes
f=n=>
(`
f=`+f).split`
`[n]
Thanks to Shaggy for the suggested edits
Not much to say about this answer, you just add a line skip to the code of your function and select the corresponding line.
PHP, 261 bytes
<?php function f($l){
$a="aWYoJGw9PTEpJG09Ijw/cGhwIGZ1bmN0aW9uIGYoXCRsKXsiO2lmKCRsPT0yKSRtPSJcJGE9XCIkYVwiOyI7aWYoJGw9PTMpJG09IlwkYj1cIiR";
$b="iXCI7IjtpZigkbD09NCkkbT0iZXZhbChiYXNlNjRfZGVjb2RlKFwkYS5cJGIpKTt9Pz4iO2VjaG8gJG07";
eval(base64_decode($a.$b));}?>
The encoded string is:
if($l==1)$m="<?php function f(\$l){";
if($l==2)$m="\$a=\"$a\";
if($l==3)$m="\$b=\"$b\";
if($l==4)$m="eval(base64_decode(\$a.\$b));}?>";
echo $m;
Perl, 52 bytes
$_=q{print+(split/
/,"\$_=q{$_};
eval")[<>-1]};
eval
This is a simple variation on the classic quine
$_=q{print"\$_=q{$_};eval"};eval
The "payload" is split on newlines and the correct line is selected by indexing into the resulting list.
Perl, 49 48 bytes (non-competing)
#!/usr/bin/perl -d:A
sub DB'DB{
print${"_<$0"}[<>]}
1
38 bytes for the code (excluding the shebang but including -d:A) plus 10 bytes for the filename, which must be Devel/A.pm. The Devel directory must be in @INC.
Technically, this violates Rule #4 because -d:A causes the file to be parsed twice, so it's a non-competing solution.
It uses a debugger hook to access the lines of the file, which perl stores in the @{"_<$filename"} array at compile time.
Python 2, 104 73 67 bytes
Thanks to Jonathan Allan for saving 6 bytes!
s=\
['print s[input()]or s', 's=\\', 0, 'exec s[', '0]']
exec s[
0]
Edit: Same byte count, but I like this solution better
Python version of Value Ink's Ruby answer.
Older answer (67 bytes):
1
s=\
['print s[-input()]or s', 0, 's=\\', 1]
print s[-input()]or s
CJam, 19 18 17 bytes
1
{'_'~]ri(=}
_
~
Based on the standard CJam-quine. The {...}_~ runs the ... with the block itself on the stack (and in this case, also 1 below that). Then we do:
'_'~ e# Push the third and fourth line.
] e# Wrap all four lines in a list.
ri e# Read input and convert to integer.
(= e# Use as index into the lines.
Ruby, 71 70 66 bytes
->n{
k=["}", "k[-n]%%k.inspect", "k=%s", "->n{"]
k[-n]%k.inspect
}
"Cheating" Mode: 7+1 = 8 bytes
Requires the -p flag for +1 byte. Literally a copy of the V answer. Prints the number that is inputted; the entire program is effectively just no-ops.
1
2
3
4
PowerShell, 184 172 bytes
$v=0,
'$v=0,',
"'`$v=0',",(($q='"{0}`$v=0{0},",(($q={0}{1}{0})-f([char]39),$q)')-f([char]39),$q),
(($z='(($z={0}{1}{0})-f([char]39),$z;$v[$args]')-f([char]39),$z);$v[$args]
Explanation
Starts by creating an array $v on the first line. On that same line, the first (0th) element is set to 0, and a comma , continues its definition.
The next line sets the next element (1) of the array to a string representing the content of the first line of the script, so that $v[1] returns the first line.
The 3rd line first sets the 3rd element of the array (index 2) to a string representing the 2nd line of the script, then on the same line sets the 4th element (index 3) using a quine snippet that uses the format operator (-f) to replace certain instances of single quotes ([char]39) and the format template string, into itself, to reproduce the entirety of the 3rd line.
Line 4 basically does the same thing, but also ends the creation of the array and then indexes into it using the supplied argument.
Haskell, 69 59 bytes
(lines(s++show
s)
!!)
s="\n(lines(s++show\n s)\n !!)\ns="
Based on the standard Haskell quine. The first expression (spread over the first three lines) is an unnamed function that picks the nth line from the quinified string s (s++show s). +2 bytes for making indexing 1-based (imho an unnecessary rule).
For a Try it online! version I have to name the function which adds 4 bytes.
Vim, 7 bytes
1
2
3
4
As far as I can tell, this complies with all of the rules. In vim by default, the empty program prints out all of the input. Since
<N><CR>
Is a noop, nothing changes the input text, and since each input matches the desired output, this same approach works with any number of lines.