# Make an Emergency Corridor

In some nations there are recommendations or laws on how to form emergency corridors on streets that have multiple lanes per direction. (In the following we only consider the lanes going in the direction we are travelling.) These are the rules that hold in Germany:

• If there is only one lane, everyone should drive to the right such that the rescue vehicles can pass on the left.
• If there are two or more lanes, the cars on the left most lane should drive to the left, and everyone else should move to the right.

### Challenge

Given the number N>0 of regular lanes, output the layout of the lanes when an emergency corridor is formed using a string of N+1 ASCII characters. You can use any two characters from ASCII code 33 up to 126, one to denote the emergency corridor, and one for denoting the cars. Trailing or leading spaces, line breaks etc are allowed.

### Examples

Here we are using E for the emergency corridor, and C for the cars.

N  Output
1  EC
2  CEC
3  CECC
4  CECCC
5  CECCCC
6  CECCCCC
etc

• I won't fall for this! You're just looking for a lane of your own to slither through you sneaky snake.
– orlp
Commented Apr 4, 2018 at 18:01
• @PmanAce I really don't think flawr needs our help for this :P
– orlp
Commented Apr 4, 2018 at 18:04
• +1 because it actually works in Germany. Was in the situation last weekend. Commented Apr 4, 2018 at 19:48
• @msh210 I think the pictures in the german WP page explain it best. Commented Apr 5, 2018 at 6:55
• You know, at first this looked like a to-the-point challenge with C and E, but there are so many nice approaches possible for this challenge! Using mathematical operations for C=1/E=2 or C=2/E=3 like the top answer does; using C=0/E=1 with 10^(n-1); using C=0/E=. by decimal formatting 0.0; using C=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. ;) Commented Apr 5, 2018 at 11:32

# Python 2, 29 26 bytes

lambda n:10**n*97/30-1/n*9


Example:

>>> f(1)
23
>>> f(2)
323
>>> f(3)
3233

• you need to output 21 in the n=1 case Commented Apr 4, 2018 at 18:18
• @DanielIndie :( fixed but now it's ugly.
– orlp
Commented Apr 4, 2018 at 18:25
• Still a very creative solution:) Commented Apr 4, 2018 at 18:30
• @orlp sorry :) but still a fine solution :) Commented Apr 4, 2018 at 18:31
• 10**n*97/30-1/n*9 saves another byte, giving f(5) == 323333 etc.
– lynn
Commented Apr 5, 2018 at 7:54

# Python 3, 35 33 bytes

lambda N:'C'*(N>1)+'EC'+'C'*(N-2)


Edit: dropping f= to save 2 bytes, thanks to @dylnan's reminder.

Try it online!

To visualize it:

lambda N:'🚘'*(N>1)+'🚔🚘'+'🚘'*(N-2)


Output:

1 🚔🚘
2 🚘🚔🚘
3 🚘🚔🚘🚘
4 🚘🚔🚘🚘🚘
5 🚘🚔🚘🚘🚘🚘
6 🚘🚔🚘🚘🚘🚘🚘


Try 🚔 online!

# Python 3, 40 bytes

A straightforward solution:

lambda N:str(10**N).replace('100','010')


Try it online!

• I think the 'straightforward' solution has unnecessary whitespace after lambda N: Commented Apr 5, 2018 at 14:56
• @someone I was not aware of that, thanks. Commented Apr 5, 2018 at 15:25

# C (gcc), 32 bytes

f(n){printf(".%.*f"+1%n,n-1,0);}


Try it online!

Uses 0 and . characters:

.0
0.0
0.00
0.000
0.0000


# Japt, 5 4 bytes

Uses q for cars and + for the corridor.

ç¬iÄ


Try it

Credit to Oliver who golfed 4 bytes off at the same time as I did.

## Explanation

A short solution but a tricky explanation!

The straightforward stuff first: The ç method, when applied to an integer, repeats its string argument that number of times. The i method takes 2 arguments (s & n) and inserts s at index n of the string it's applied to.

Expanding the 2 unicode shortcuts used gives us çq i+1, which, when transpiled to JS becomes U.ç("q").i("+",1), where U is the input. So we're repeating q U times and then inserting a + at index 1.

The final trick is that, thanks to Japt's index wrapping, when U=1, i will insert the + at index 0, whatever value you feed it for n.

• I was going to post ç0 iQ1 for 6 bytes, but it would be better if you used it. Commented Apr 4, 2018 at 19:40
• Thanks, @Oliver. Got it down to 5 bytes in the meantime, though. Commented Apr 4, 2018 at 19:46
• ç¬iÅ for 4 bytes ;) I've never abused Japt this much. Commented Apr 4, 2018 at 19:57
• I was just about to do the same with Ä instead of Å :) Commented Apr 4, 2018 at 20:02

# R, 50 bytes

-11 thanks to Giuseppe!

pryr::f(cat("if"(x<2,12,c(21,rep(2,x-1))),sep=""))


Outputs 1 for emergency corridor and 2 for normal lanes

## My attempt, 61 bytes

Nothing fancy to see here, but let's get R on the scoreboard =)

q=pryr::f(if(x<2,cat("EC"),cat("CE",rep("C",x-1),sep="")))


Usage:

q(5)
CECCCC


f 1="EC"
f n="CE"++("C"<*[2..n])


Try it online!

# Python 2, 3029 28 bytes

lambda n:10/3.[1/n:n-~1/n]


Print 3 instead of C and . instead of E.

Explanation:

Try it online.

lambda n:    # Method with integer parameter and string return-type
10/3.    #  Calculate 10/3 as decimal (3.333333333) and convert it to a string
[1/n       #   Take the substring from index 1 if n=1, 0 otherwise
,n-~      #   to index n+1 +
1/n]  #    1 if n=1, 0 otherwise


# Python 2, 33323129 28 bytes

lambda n:1%n-1or'1-'+'1'*~-n


Prints 1 instead of C and - instead of E.

-2 bytes thanks to @ovs.
-1 byte thanks to @xnor.

Explanation:

Try it online.

lambda n:    # Method with integer parameter and string return-type
1%n-1      #  If n is 1: Return '-1'
or         #  Else:
'1-'+    #   Return '1-', appended with:
'1'*~-n  #   n-1 amount of '1's

• Your 10/3 one fails at 17
– Jo King
Commented Apr 5, 2018 at 22:52
• @JoKing I just clarified with OP, and he said "Using a built in integer type is sufficient.", which means up to n=16 if your integer built-in is 64-bit is enough, or in this case n=16 when the decimal value can't hold more than 15 decimal digits by default is enough. (Same applies to a lot of the other answers using languages with arbitrary number sizes, like Java, C# .NET, etc.) Commented Apr 6, 2018 at 9:42

# Pyth, 109 8 bytes

Xn1Q*NQZ


Uses 0 to denote the emergency corridor and ".
Try it here

### Explanation

Xn1Q*NQZ
*NQ     Make a string of <input> "s.
n1Q         At index 0 or 1...
X      Z    ... Insert 0.


# brainfuck, 42 bytes

,[[>]+[<]>-]>>[<]<[<]>+>+<[<-[--->+<]>.,>]


Try it online!

Takes input as char code and outputs as V being normal lanes and W being the cleared lane. (To test easily, I recommend replacing the , with a number of +s)

### How it Works:

,[[>]+[<]>-] Turn input into a unary sequence of 1s on the tape
>>[<]<[<]    Move two cells left of the tape if input is larger than 1
Otherwise move only one space
>+>+<        Add one to the two cells right of the pointer
This transforms:
N=1:  0 0' 1 0  -> 0 2' 1 0
N>1:  0' 0 1 1* -> 0 1' 2 1*
[<-[--->+<]>.,>]  Add 86 to each cell to transform to Ws and Vs and print


# 05AB1E, 7 bytes

Î>∍1I≠ǝ


Try it online!

0 is C and 1 is E.

Explanation

Î>          # Push 0 and input incremented            -- [0, 4]
∍         # Extend a to length b                    -- [0000]
1I≠      # Push 1 and input falsified (input != 1) -- [0000, 1, 1]
ǝ     # Insert b in a at location C             -- [0100]
# Implicit display

• Oh you sly fox. $<×TìsiR was how I was thinking. Commented Apr 5, 2018 at 22:37 • @MagicOctopusUrn That's an interesting approach ! I lingered over the "if" construction as well but it requires at least 3 bytes, hence the need for a different approach :-) Commented Apr 6, 2018 at 7:29 • In the new version of 05AB1E, 1I can be golfed to $. Commented Dec 10, 2018 at 15:32
• 5 bytes (also works in the legacy version). Commented Dec 11, 2018 at 7:09

# Octave (MATLAB*), 31 30 28 27 22 bytes

@(n)'CE'(1+(n>1==0:n))


Try it online!

The program works as follows:

@(n)                   %Anonymous function to take input
n>1==0:n   %Creates [1 0] if n is 1, or [0 1 (0 ...)] otherwise
1+(        )  %Converts array of 0's and 1's to 1-indexed
'CE'(            ) %Converts to ASCII by addressing in string


The trick used here is XNORing the seed array of 0:n with a check if the input is greater than 1. The result is that for n>1 the seed gets converted to a logical array of [0 1 (0 ...)] while for n==1 the seed becomes inverted to [1 0], achieving the necessary inversion.

The rest is just converting the seed into a string with sufficient appended cars.

(*) The TIO link includes in the footer comments an alternate solution for the same number of bytes that works in MATLAB as well as Octave, but it results in a sequence of '0' and '1' rather than 'E' and 'C'. For completeness, the alternate is:

@(n)['' 48+(n>1==0:n)]


• Saved 1 byte by using n==1~=0:1 rather than 0:1~=(n<2). ~= has precedence over <, hence the original brackets, but is seems that ~= and == are handled in order of appearance so by comparing with 1 we can save a byte.

• Saved 2 bytes by changing where the negation of 2:n is performed. This saves a pair of brackets. We also have to change the ~= to == to account for the fact that it will be negated later.

• Saved 1 byte using < again. Turns out that < has same precedence as == after all. Placing the < calculation before the == ensures correct order of execution.

• Saved 5 bytes by not creating two separate arrays. Instead relying on the fact that the XNOR comparison will convert a single range into logicals anyway.

• Very clever :-) Commented Apr 5, 2018 at 15:33
• @StewieGriffin Thanks :). Managed to knock off another 5 bytes more. Commented Apr 5, 2018 at 16:56

# Jelly, 11 9 bytes

’0ẋ⁾0E;ṙỊ


Try it online!

Full program.

Uses 0 instead of C.

# C (gcc), 39 bytes

f(n){printf("70%o"+!n,7|(1<<3*--n)-1);}


Try it online!

• Welcome to the site and nice first post! Commented Apr 4, 2018 at 21:40

# Python 3, 32 bytes

lambda n:f"{'CE'[n<2:]:C<{n+1}}"


Try it online!

Uses an f-string expression to format either'E'or'CE' padded on the right with'C'so it has width ofn+1.

f"{          :       }    a Python 3 f-string expression.
'CE'[n<2:]             string slice based on value of n.
:            what to format is before the ':' the format is after.
<          left align
{n+1}     minimum field width based on n


# Brain-Flak, 100 66 bytes

{({}[()]<((((()()()()){}){}){}())>)}{}(({}<>)())<>{<>{({}<>)<>}}<>


Try it online!

Uses " as the emergency lane and ! as the normal lanes.

• +1 for using this language of all things. XD
– Alex
Commented Apr 5, 2018 at 9:16
• @Alex, Well, Brain-Flak is the language of the month for April
– Jo King
Commented Apr 5, 2018 at 9:39
• Seriously or late April Fool's joke? Where do languages of the month get elected?
– Alex
Commented Apr 5, 2018 at 13:31
• @Alex Nominations and voting here, and then a month-specific post is made such as this one Commented Apr 5, 2018 at 15:09
• Oh, it's on this platform. I see, thanks! :-)
– Alex
Commented Apr 6, 2018 at 12:12

# C#, 34 bytes

n=>n++<2?"EC":"CE".PadRight(n,'C')


Try it online!

# APL (Dyalog Unicode), 21 17 16 bytes

(-≠∘1)⌽'E',⍴∘'C'


Try it online!

Thanks to Erik for saving 4 bytes and Adám for one further byte.

### How?

(-≠∘1)⌽'E',⍴∘'C' ⍝ Tacit function
⍴∘'C' ⍝ Repeat 'C', according to the input
'E',      ⍝ Then append to 'E'
⌽          ⍝ And rotate
1)           ⍝ 1
≠∘             ⍝ Different from the input? Returns 1 or 0
(-               ⍝ And negate. This rotates 0 times if the input is 1, and once if not.

• (⍵>1) doesn't need to be in parentheses. And you can save 4 bytes with a tacit function: (⊢×1<⊢)⌽'E',⍴∘'C'. Commented Apr 4, 2018 at 20:47
• @EriktheOutgolfer thanks! I didn’t have the time to go tacit after posting because I had a class today. I’ll edit when I get home. Commented Apr 4, 2018 at 21:35
Commented Apr 5, 2018 at 7:57
• 15 bytes with ⎕io=0: 'CE'[1(≠=∘⍳+)⎕]
– ngn
Commented Apr 6, 2018 at 3:50
• @ngn I can't even... can you get me a TIO link with the test cases? Can't seem to make it work... Commented Apr 6, 2018 at 16:17

2 bytes saved thanks to Angs, 1 byte saved thanks to Lynn

(!!)$"":"EC":iterate(++"C")"CEC"  Try it online! # Haskell, 3230 29 bytes This is zero indexed so it doesn't comply with the challenge g=(!!)$"EC":iterate(++"C")"CEC"


Try it online!

This doesn't work because output needs to be a string

f 1=21
f 2=121
f n=10*f(n-1)+1


Try it online!

Here we use numbers instead of strings, 2 for the emergency corridor, 1 for the cars. We can add a 1 to the end by multiplying by 10 and adding 1. This is cheaper because we don't have to pay for all the bytes for concatenation and string literals.

It would be cheaper to use 0 instead of 1 but we need leading zeros, which end up getting trimmed off.

• ((blah)!!) can become (!!)$blah to save a byte in your first two answers. – lynn Commented Apr 5, 2018 at 12:44 • @Lynn Thanks! I tried to do that earlier but I must have mis-counted the bytes. Commented Apr 5, 2018 at 13:11 # Python 3, 30 29 bytes lambda n:"CEC"[~n:]+"C"*(n-2)  Try it online! OK, there is a lot of Python answers already, but I think this is the first sub-30 byter among those still using "E" and "C" chars rather than numbers. # JavaScript (Node.js), 28 bytes n=>n<2?21:"12".padEnd(n+1,1)  Try it online! # APL (Dyalog Unicode), 16 bytes ≡∘1⌽'CE',1↓⍴∘'C'  Try it online! # Stax, 7 bytes ü♣àj#F  Run and debug it This uses the characters "0" and "1". This works because when you rotate an array of size 1, it doesn't change. Unpacked, ungolfed, and commented, it looks like this. 1]( left justify [1] with zeroes. e.g. [1, 0, 0, 0] |) rotate array right one place 0+ append a zero$   convert to string


Run this one

# JavaScript (Node.js), 19 bytes

n=>--n?"0"+10**n:10


Try it online!

# Perl 5-p, 2720 19 bytes

$_=1x$_;s/1?\K1/E1/


Try it online!

Saved a byte by using 1 for the cars and E for the emergency corridor.

# Jelly, 6 bytes

⁵*ṾṙỊṙ


Displays car lanes as 0, the emergency lane as 1.

Try it online!

### How it works

⁵*ṾṙỊṙ  Main link. Argument: n

⁵*      Compute 10**n.
Ṿ     Uneval; get a string representation.
ṙỊ   Rotate the string (n≤1) characters to the left.
ṙ  Rotate the result n characters to the left.


# Whitespace, 141104 103 bytes

[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}
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}
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}
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}

SNS      Duplicate top (0)             [0,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}

SNS      Duplicate top (-1)            [-1,-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.

• Would it be easier to print the clear lane as - by abusing printing -1?
– Jo King
Commented Apr 5, 2018 at 8:40
• @JoKing Unfortunately it would be longer. Try it online 112 bytes. It indeed changed push_0;print_as_integer;push_1;print_as_integer to push_-1;print_as_integer, but in exchange the two push_0;print_as_integer are replaced with push_45;print_as_character, where push_0 = SSSN, and push_45 = SSSTSTTSTN. And an additional push_45 has to be added as well, because for input n=1 I now print the duplicated 0 I already had on the stack, so I didn't had to push 0 again because the 0 was already on the stack. Commented Apr 5, 2018 at 9:09
• I meant - as replacing 1 and 1 replacing 0. Then you would avoid having to push 45, and as far as I can tell this would save on printing a number in the first half of the conditional, but slightly increase costs on pushing 1 instead of 0. Check my Gol><> answer for an example of the output I mean
– Jo King
Commented Apr 5, 2018 at 9:38
• @JoKing I've tried implementing it, but I ended up at 107 bytes (here is the same code with added highlighting and explanation). It indeed saves on print_-1 instead of print 0 and 1, but an additional print_-1 is necessary outside the loop. EDIT: Been able to reduce it to 103 bytes by changing subtract_1;if_0_jump_to_ONE;push_-1;print_integer to subtract_2;if_negative_jump_to_ONE;print_integer, because -1 is already on the stack then. So thanks for -1 byte. :) Commented Apr 5, 2018 at 11:01

# AutoHotkey 32 bytes

Replaces the letter "C" with "EC" unless amount of C > 1, then it sends "CEC" and exits the app.

::C::EC
:*:CC::CEC^c
^c::ExitApp


C => EC
CC => CEC then exits the program. Any further Cs will be entered after the program exits.

# APL+WIN, 20 16 bytes

4 bytes saved thanks to Adám

Prompts for integer n:

(-2≠⍴n)⌽n←1⎕/⍕10


1 for emergency corridor o for cars.

• 16: (-2≠⍴n)⌽n←1⎕/⍕10
Commented May 1, 2018 at 8:03
• @Adám Thanks. I see ngn's trick, 1⎕/⍕, is coming in useful. One for the idiom list? Commented May 1, 2018 at 15:11
• What idiom list are you talking about?
Commented May 1, 2018 at 15:44
• @Adám The two idiom lists I use most frequently are the Finnapl and APL2idioms Commented May 1, 2018 at 15:52
• I'm not sure what's so idiomatic here. It is just golfy. Anyway, my idiom list may interest you.
Commented May 1, 2018 at 17:06

# J, 11 bytes

1":1&<=i.,]


Try it online!

Based on ngn’s comment.

🚘 and 🚔:

1&<,~/@A.'🚔',~'🚘'\$~,&4

# MathGolf, 7 6 bytes

ú░\┴╜╪


Try it online.

Output 1 for E and 0 for C.

Explanation:

ú         # 10 to the power of the (implicit) input
#  i.e. 1 → 10
#  i.e. 4 → 10000
░        # Convert it to a string
#  i.e. 10 → "10"
#  i.e. 10000 → "10000"
\       # Swap so the (implicit) input is at the top of the stack again
┴╜     # If the input is NOT 1:
╪    #  Rotate the string once towards the right
#   i.e. "10000" and 4 → "01000"
# Output everything on the stack (which only contains the string) implicitly