CAPL1.5+ ; 6 without input; 10 - 10 = 0 with input
Sidenote
I have read somewhere [link?] that custom languages aren't allowed in golfing questions, as they could make built-in functions that do exactly what the question is asking, however I made CAPL to make golfing easier in general. If you think this is not allowed here, let me know!
I got some ideas from ><> and Befunge (You can move between lines and use hexadecimal characters to push numbers), some from Ruby and some from my own to make golfing easier.
CAPL reads from left to right, and goes one line down at the end of the line. If it is as the last line, the program will quit.
As no-one knows this language yet, I'll try to explain as much as possible.
Outputting y. 6 bytes
bb*.n<
bb*
b
is hexadecimal for 11
, so bb*
is 11*11
=121
, which is the UTF-8 equivalent of y
. This value is pushed to the stack.
.
Pops the top value from the stack, and outputs as UTF-8. As 121
is on top of the stack, the index is ignored here.
n
Outputs a newline
<
Sends the pointer back to the beginning of the line, thus repeating that line. As we don't expect input, we can do this safely without re-asking for the input.
Outputting from input. 10 bytes, 0 after bonus
i~a&{X:.)}
i
Takes input from the user, pushes as UTF-8 on the top of the stack, and pushes the length after that. I.e. [72,101,108,108,111,5]
~
Pops a number from the stack, then reverses that amount of bytes. I.e. [111,108,108,101,72]
a
Hexadecimal for 10
, the newline character
&{...}
Makes an infinite loop. We have input, so we can't send the pointer back to the line. I could place the function on the line below, which would safe me a byte, but newlines aren't allowed in this challenge.
X
Removes the top value from the stack (The index from the loop)
:.
Duplicates the top value, then outputs as UTF-8
)
Turns stack right. ([1,2,3,4,5]
-> [5,1,2,3,4]
)
Howver, this means we start with a newline, then start outputting the input, then a newline, then the input, etc. If we're not allowed to start with a newline, use the following code with 12 bytes, or 2 after subtracting the bonus.
iXa#~&{X:.)}
The only new command here is #
, which pushes the amount of items on the stack to the stack.
I removed the length from i
, because adding 1, then swapping with the newline is longer than removing and getting the length again.
Just for fun, here is a "Hello World" program
"Hello World"#~
#?!;.<
The ?!
operation is the same as ><>'s
4For the bonus, should it still print
y
for empty STDIN? – Martin Ender – 2014-12-17T13:33:45.53013"you are not allowed to use "y" or "\n"" -- should I read this as "You may not use
y
or\n
inside of a string literal"? – apsillers – 2014-12-17T14:28:45.250@MartinBüttner Optionally. – Ramon Snir – 2014-12-17T15:37:00.897
@apsillers In the source code, nothing that evaluates to y or \n. Not in a literal, not as a constant, not as a variable name. Nothing. – Ramon Snir – 2014-12-17T15:37:36.797
5@RamonSnir Wait, so I can just print empty lines for empty STDIN if I go for the bonus? – Martin Ender – 2014-12-17T15:38:21.340
2This is impossible in Java, without using
System
– Ypnypn – 2014-12-17T16:38:13.707@Ypnypn Here's my best guess: Perhaps a rule of thumb might be "if a source-code token has a
y
in it, you must be able to replace it with a synonymous token without ay
." For example, if you haveSystem.out.print("y")
, you can do something likeSstem = System
andSstem.out.print
would work fine (at least conceptually). By contrast, if you replace the literal"y"
with something else, you're not going to get the same result. I think this should probably clarified in the spec. – apsillers – 2014-12-17T16:46:02.36313On a related note, GNU
true.c
is 80 lines long. – Paused until further notice. – 2014-12-17T22:32:11.0131@Ypnypn It is possible. There are more ways than one to get
System.out
. – TheNumberOne – 2014-12-18T01:11:08.803@TheBestOne Indeed; I just solved this with reflection. – Ypnypn – 2014-12-18T01:51:31.693
7@DennisWilliamson On a similarly related note, false.c is 2 lines long.... ;_; – LordAro – 2014-12-18T02:30:44.980
Is providing output in a different character encoding permissible (eg. using PETSCII rather than ASCII-1967)? – Mark – 2014-12-18T06:56:04.290
1@RamonSnir Just to be sure, is my C# answer valid? It uses
System
(which I cannot get rid of, because it's the top namespace and everything is in it), but in your previous comment you said "nothing that evaluates toy
". It does not evaluate toy
. Is that answer valid? – ProgramFOX – 2014-12-18T16:40:41.957@Ypnypn I just solved this without reflection. – TheNumberOne – 2014-12-18T18:13:39.120
6the coreutils
yes
takes an optional argument on the command line, notstdin
. – Brian Minton – 2014-12-18T20:05:08.2901@RamonSnir and I started this after
git clone
ingcoreutils
and getting angry thatls
is 8000 lines long. This post is all about getting candidates for replacing the "bloated" coreutils :) – Nitz – 2014-12-18T21:05:19.1873
@DennisWilliamson: Let me introduce you to GNU Hello
– Nate Eldredge – 2014-12-18T21:46:45.253@NateEldredge: I call foul though since that's an example program and not a standard utility. ;-) – Paused until further notice. – 2014-12-18T22:00:31.663
2@RamonSnir "Nothing that evaluates to
y
or\n
" would make the challenge impossible. Please clarify what you mean. – nyuszika7h – 2014-12-19T12:28:11.650I don't get GNU: Why would you possibly need a program which spams you with
y
s? – MrLore – 2014-12-20T10:30:16.4377@MrLore: to pipe into other programs that might constantly ask for confirmation of the various things they are doing, so you dont have to sit there typing the
y
s yourself. – marcus erronius – 2014-12-20T16:57:57.0601OMG there's no
\n
at the end of the file O_o that's horrible! – o0'. – 2014-12-21T11:08:59.7471@mrl i regularly do
yes | sudo pacman -Syu
because i'm a terrible person – undergroundmonorail – 2014-12-28T18:53:33.6171@undergroundmonorail the whole story started when I did
alias aurauy="yes | ( aura -Suy && aura -Auy )"
:) – Ramon Snir – 2014-12-29T08:36:08.9371There are lots of answers which use
121 + 1
to gety
's ascii code. Are those invalid? Can I have newlines outside string literals? – cat – 2016-03-18T18:27:09.8931Microsoft, 1 Steve Ballmer — developers, developers, developers, developers, ... – sergiol – 2017-06-08T00:43:54.630
How do you get to 91 from that page while counting comments? I see 130. – Fabian Röling – 2020-02-16T22:29:07.453