[S S S N
_Push_0][S N
S _Duplicate_0][T N
T T _Read_STDIN_as_number][T T T _Retrieve][S S S T S N
_Push_2][T S S T _Subtract][S N
S _Duplicate_input-2][N
T T N
_If_negative_Jump_to_Label_-1][S S S T N
_Push_1][S N
S _Duplicate_1][T N
S T _Print_as_integer][S S T T N
_Push_-1][T N
S T _Print_as_integer][T S S T _Subtract][N
S S T N
_Create_Label_LOOP][S N
S _Duplicate][N
T T S N
_If_negative_Jump_to_EXIT][S S S T N
_Push_1][S N
S _Duplicate_1][T N
S T _Print_as_integer][T S S T _Subtract][N
S N
T N
_Jump_to_LOOP][N
S S N
_Create_Label_-1][T N
S T _Print_as_integer][N
S S S N
_Create_Label_EXIT]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.
[..._some_action]
added as explanation only.
Prints 1
instead of C
and -
instead of E
.
-1 byte thanks to @JoKing by suggesting the use of 1
and -1
instead of 0
and 1
.
Explanation in pseudo-code:
Integer i = STDIN-input as integer - 2
If i is negative (-1):
Print i (so print "-1")
Else:
Print "1-1"
Start LOOP:
If i is negative:
EXIT program
Print "1"
i = i-1
Go to the next iteration of the LOOP
Example runs:
Input: 1
Command Explanation Stack Heap STDIN STDOUT STDERR
SSSN Push 0 [0]
SNS Duplicate top (0) [0,0]
TNTT Read STDIN as integer [0] {0:1} 1
TTT Retrieve heap at 0 [1] {0:1}
SSSTSN Push 2 [1,2] {0:1}
TSST Subtract top two [-1] {0:1}
SNS Duplicate input-2 [-1,-1] {0:1}
NTSN If neg.: Jump to Label_-1 [-1] {0:1}
NSSN Create Label_-1 [-1] {0:1}
TNST Print top as integer [] {0:1} -1
NSSSN Create Label_EXIT [] {0:1}
error
Try it online (with raw spaces, tabs and new-lines only).
Stops with error: Exit not defined.
Input: 4
Command Explanation Stack Heap STDIN STDOUT STDERR
SSSN Push 0 [0]
SNS Duplicate top (0) [0,0]
TNTT Read STDIN as integer [0] {0:4} 4
TTT Retrieve heap at 0 [4] {0:4}
SSSTSN Push 2 [4,2] {0:4}
TSST Subtract top two [2] {0:4}
SNS Duplicate input-2 [2,2] {0:4}
NTSN If neg.: Jump to Label_-1 [2] {0:4}
SSSTN Push 1 [2,1] {0:4}
SNS Duplicate top (1) [2,1,1] {0:4}
TNST Print as integer [2,1] {0:4} 1
SSTTN Push -1 [2,1,-1] {0:4}
TNST Print as integer [2,1] {0:4} -1
TSST Subtract top two [1] {0:4}
NSSTN Create Label_LOOP [1] {0:4}
SNS Duplicate top (1) [1,1] {0:4}
NTTSN If neg.: Jump to Label_EXIT [1] {0:4}
SSSTN Push 1 [1,1] {0:4}
SNS Duplicate top (1) [1,1,1] {0:4}
TNST Print as integer [1,1] {0:4} 1
TSST Subtract top two [0] {0:4}
NSNTN Jump to Label_LOOP [0] {0:4}
SNS Duplicate top (0) [0,0] {0:4}
NTTSN If neg.: Jump to Label_EXIT [0] {0:4}
SSSTN Push 1 [0,1] {0:4}
SNS Duplicate top (1) [0,1,1] {0:4}
TNST Print as integer [0,1] {0:4} 1
TSST Subtract top two [-1] {0:4}
NSNTN Jump to Label_LOOP [-1] {0:4}
SNS Duplicate top (-1) [-1,-1] {0:4}
NTTSN If neg.: Jump to Label_EXIT [-1] {0:4}
NSSSN Create Label_EXIT [-1] {0:4}
error
Try it online (with raw spaces, tabs and new-lines only).
Stops with error: Exit not defined.
18I won't fall for this! You're just looking for a lane of your own to slither through you sneaky snake. – orlp – 2018-04-04T18:01:45.467
A school assignment disguised as code golf? ;) – PmanAce – 2018-04-04T18:03:34.130
16@PmanAce I really don't think flawr needs our help for this :P – orlp – 2018-04-04T18:04:37.643
I wouldn't suppose we could zero index? – Post Rock Garf Hunter – 2018-04-04T18:16:29.003
1@user56656 Sorry, it is too late to change that now that so many people have answered. – flawr – 2018-04-04T18:26:45.417
8+1 because it actually works in Germany. Was in the situation last weekend. – ElPedro – 2018-04-04T19:48:24.117
I don't understand how you get E and C stuffed into one lane, or C, E, C, C, and C into four. (That's a question on the storyline of the question, not on the challenge itself.) – msh210 – 2018-04-04T22:16:25.513
2@msh210 Normally there are n lanes, but when an emergency corridor is formed there are n lanes
C
and the emergency corridorE
. – user202729 – 2018-04-05T02:49:28.94010
@msh210 I think the pictures in the german WP page explain it best.
– flawr – 2018-04-05T06:55:45.5079You know, at first this looked like a to-the-point challenge with
C
andE
, but there are so many nice approaches possible for this challenge! Using mathematical operations forC=1
/E=2
orC=2
/E=3
like the top answer does; usingC=0
/E=1
with10^(n-1)
; usingC=0
/E=.
by decimal formatting0.0
; usingC=1
/E=-
by utilizing-1
; etc. etc. So many unique possibilities for a challenge that looked so to-the-point at first. Too bad I can only +1 once. ;) – Kevin Cruijssen – 2018-04-05T11:32:52.177Is there a maximum
n
we need to support? In Java for example,long
is a 64-bit number and a decimaldouble
/float
has only 15 digits after the comma, so using an arithmetic or decimal approach only works up ton=16
.. I think this applies to every programming language without an arbitrary number-size. Some languages also start using1e#
from a certain amount of digits. Besides, I highly doubt there are any highways in Germany with more than 8 lines wide in one direction.. – Kevin Cruijssen – 2018-04-06T06:51:31.3671@KevinCruijssen Using a built in integer type is sufficient. – flawr – 2018-04-06T09:19:54.927