# 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 Apr 4 '18 at 18:01
• @PmanAce I really don't think flawr needs our help for this :P – orlp Apr 4 '18 at 18:04
• +1 because it actually works in Germany. Was in the situation last weekend. – ElPedro Apr 4 '18 at 19:48
• @msh210 I think the pictures in the german WP page explain it best. – flawr Apr 5 '18 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. ;) – Kevin Cruijssen Apr 5 '18 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 – DanielIndie Apr 4 '18 at 18:18
• @DanielIndie :( fixed but now it's ugly. – orlp Apr 4 '18 at 18:25
• Still a very creative solution:) – flawr Apr 4 '18 at 18:30
• @orlp sorry :) but still a fine solution :) – DanielIndie Apr 4 '18 at 18:31
• 10**n*97/30-1/n*9 saves another byte, giving f(5) == 323333 etc. – Lynn Apr 5 '18 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: – someone Apr 5 '18 at 14:56
• @someone I was not aware of that, thanks. – Guoyang Qin Apr 5 '18 at 15:25

# C (gcc), 32 bytes

f(n){printf(".%.*f"+!!--n,n,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. – Oliver Apr 4 '18 at 19:40
• Thanks, @Oliver. Got it down to 5 bytes in the meantime, though. – Shaggy Apr 4 '18 at 19:46
• ç¬iÅ for 4 bytes ;) I've never abused Japt this much. – Oliver Apr 4 '18 at 19:57
• I was just about to do the same with Ä instead of Å :) – Shaggy Apr 4 '18 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 Apr 5 '18 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.) – Kevin Cruijssen Apr 6 '18 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


# 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 :-) – Stewie Griffin Apr 5 '18 at 15:33
• @StewieGriffin Thanks :). Managed to knock off another 5 bytes more. – Tom Carpenter Apr 5 '18 at 16:56

# 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! – caird coinheringaahing Apr 4 '18 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 Apr 5 '18 at 9:16
• @Alex, Well, Brain-Flak is the language of the month for April – Jo King Apr 5 '18 at 9:39
• Seriously or late April Fool's joke? Where do languages of the month get elected? – Alex Apr 5 '18 at 13:31
• @Alex Nominations and voting here, and then a month-specific post is made such as this one – Kamil Drakari Apr 5 '18 at 15:09
• Oh, it's on this platform. I see, thanks! :-) – Alex Apr 6 '18 at 12:12

# C#, 34 bytes

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


Try it online!

# 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. – Magic Octopus Urn Apr 5 '18 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 :-) – Kaldo Apr 6 '18 at 7:29 • In the new version of 05AB1E, 1I can be golfed to $. – Kevin Cruijssen Dec 10 '18 at 15:32
• 5 bytes (also works in the legacy version). – Kevin Cruijssen Dec 11 '18 at 7:09

# 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'. – Erik the Outgolfer Apr 4 '18 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. – J. Sallé Apr 4 '18 at 21:35
• – Adám Apr 5 '18 at 7:57
• 15 bytes with ⎕io=0: 'CE'[1(≠=∘⍳+)⎕] – ngn Apr 6 '18 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... – J. Sallé Apr 6 '18 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.

# 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


# Canvas, 11 bytes

┤C×EC×╴╷？C×


Try it here!