# 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, 2018 at 18:01
• @PmanAce I really don't think flawr needs our help for this :P
– orlp
Apr 4, 2018 at 18:04
• +1 because it actually works in Germany. Was in the situation last weekend. Apr 4, 2018 at 19:48
• @msh210 I think the pictures in the german WP page explain it best. 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. ;) Apr 5, 2018 at 11:32

# Canvas, 11 bytes

┤C×EC×╴╷？C×


Try it here!

# Jelly, 14 bytes

⁵*×109:90+Ị9×Ɗ


Try it online!

Port of orlp's python answer which only uses arithmetic.

Jelly, 15 bytes

ḊṬ1;,2Ṭ¤LÐṀị⁾CE


Try it online!

# C (gcc), 48 bytes

f(n){for(printf("CEC"+!--n);--n>0;putchar(67));}


Try it online!

# Python 3, 39 bytes

lambda n:'C'*(n>1)+'E'+'C'*(n-1+(n==1))


Try it online!

• You can replace n==1 with n<2 since n will always be >0. Apr 4, 2018 at 21:16
• 'C'*(n>1) can be replaced with 1%n*'C'
– ovs
Apr 5, 2018 at 13:14

# Gol><>, 10 bytes

IR1~I{lRn;


Try it online!

One of a few 10-byte solutions I found. I like this one the most because it uses the - as the clear lane, where the - is a result of printing -1.

The other 10-byte solutions:

• IR0P{lPRn;
• IRCP{C}H

Pity Gol><> doesn't have a halt and output the stack numbers command, like it does for chars, or this could be 3 bytes smaller.

# MATL, 7 bytes

Q:G2Xl=


Try it online!

Uses 1 for emergency, 0 for normal traffic.

Q:      % Range 1...n+1, implicit input n
G2Xl  % Explicitly push input, limit to 2 (so, either 1 or 2)
= % Element-wise equality of range and limited input.
% Implicit display.


This answer assumes (like some other answers) that separating spaces are OK. If not, append VXz for ten bytes.

# Excel, 29 bytes

=IF(A1=1,0,10&REPT(1,A1-2))&1


Uses 0 for Emergency lane. 1 for Cars:

1  01
2  101
3  1011
4  10111
5  101111


# J, 20 bytes

'CECC'#~1(<,[,=,-~)]


Try it online!

# Python 2, 30 bytes

lambda n:1%n*'0'+10**(n-1%n)


Try it online!

If n is larger than 1, 1%n gets 1: 1*'0'+10**(n-1)
If n is equal to 1, 1%n gets 0: 0*'0'+10**(1-0)

# Python 2, 31 bytes

lambda n:1/n*'.'+'%%.%df'%~-n%0


Try it online!

1/n*'.'        # if n is 1, insert a dot ...
+              # before
'%%.%df'%~-n%0 # the decimal representation of 0 with n-1 digits after the decimal point


'%%.%df'%~-n expands to '%.<n-1>f'.

# Python, 53 bytes

lambda n:-1*((n+1)//n-2)*'C'+'E'+(n+((n+1)//n-2))*'C'


Try it online!

• Welcome to the site! You may not assume that input is stored in a variable, so please change the method you take input. A function is completely acceptable, so prepending your code with lambda n: would be perfectly fine. Apr 5, 2018 at 13:05
• Hi, welcome to PPCG! You can make the "Python, pure math+str, 45 bytes" title-case by adding a # in front of it. With Python lambdas you'll have to include lambda n: to the code and byte-count. So your current answer should be 53 bytes. Also, you might want to add a TIO-link with test code. Here is an example of how to do that with your code. Something to golf: both (n+1) can be -~n. Tips for golfing in Python might also be interesting to read through. Enjoy your stay! Apr 5, 2018 at 13:07

## Excel, 28 bytes

=REPT(2,A1+1)-10^MAX(A1-1,1)


Uses 2 for cars, and 1 for Corridor

Makes a list of 2s n+1 long, then subtracts the larger of 10^(n-1) and 10:

n=1 : 22 - MAX(1,10) = 12
n=2 : 222 - MAX(10,10) = 212
n=3 : 2222 - MAX(100,10) = 2122
n=10 : 22222222222 - MAX(1000000000,10) = 21222222222

# [GNU sed 4.2.2], 14 bytes

• Score includes 1 byte for the -r sed parameter

• 3 bytes saved thanks to @Leo

Input integer is given in unary. Output characters are E for emergency vehicles and 1 for regular cars.

s/(.?)1/\1E1/


Try it online!

• You know already what the second group will contain, so you can avoid it Try it online!
– Leo
Apr 5, 2018 at 2:32
• @Leo thanks - very good! Apr 5, 2018 at 17:13

# C# (.NET Core), 39 bytes

n=>(n--<2?"EC":"CE")+new string('C',n);


Try it online!

• The solution is wrong. You have on C to many at the end of each line for n>1. Apr 5, 2018 at 13:49
• @raznagul I missed that, thanks for pointing it out. I fixed it. Apr 5, 2018 at 18:10

a 1=12;a 2=212;a n=a(n-1)*10+2


1 is a corridor, 2 is a car.

Example:

GHCi, version 8.2.2: http://www.haskell.org/ghc/  :? for help
Prelude> a 1=12;a 2=212;a n=a(n-1)*10+2
Prelude> a 1
12
Prelude> a 2
212
Prelude> a 3
2122
Prelude> a 4
21222
Prelude> a 5
212222

• Welcome to the site! I wrote a similar answer earlier but commenters pointed out that answers need to output a string, whereas this outputs a number, in order to get the string you need to also use show. Apr 5, 2018 at 23:23
• The current top Python answer uses a number printing in an interactive prompt too and doesn't use print.
– FRex
Apr 5, 2018 at 23:27
• Huh. I didn't see that. I would leave a comment on the OP to make sure that this output form is permissible. Apr 6, 2018 at 0:31
• @user56656 Or just let the language be "Haskell (interactive)". Done. (or does it work? consider this is a function submission) Apr 6, 2018 at 3:25
• ghci requires let to bind a, the example would result in a parse error
– Angs
Apr 8, 2018 at 18:30

# Perl 5-lp, 19 bytes

#!/usr/bin/perl -lp
$_=--$_?CE.C x$_:EC  Try it online! # Ruby, 25 bytes ->n{n>1?'CE'+?C*~-n:'EC'}  Try it online! # Java 10, 5351454238373635 34 bytes n->n+=Math.pow(10,n)*97/30-1/n*9-n  Prints 3 instead of C and 2 instead of E. Try it online. Port of @orlp's Python 2 answer. n+=...-n is used instead of (long)... to save a byte. Old 35 bytes answer: n->(10/3f+"").substring(1/n,1/n-~n)  Prints 3 instead of C and . instead of E. Explanation: Try it online. n->{ // Method with integer parameter and String return-type (10/3f+"") // Divide 10 by 3 and convert it to a String ("3.333333333") .substring(1/n // And take the substring from index 1 if n=1, 0 otherwise ,1/n // to index 1 if n=1, 0 otherwise -~n) // + n+1  • Nice, you beat my best guess at 39 bytes: n->n--<2?"10":"".format("01%0"+n+"d",0). Apr 5, 2018 at 11:16 • @OlivierGrégoire My 42-byte answer used String#format as well, but by using 0 and . :) (n->(n--<2?".":"")+"".format("%."+n+"f",0f)). So many nice approached possible for this challenge. Apr 5, 2018 at 11:23 • @OlivierGrégoire Your answer could have been golfed to 37 bytes by returning an Object instead of a String, so "10" becomes 10. :) I'm currently at 35 however by creating a port of the top Python 2 answer. EDIT: Just realized my 38 byte answer becomes 36 using the same Object instead of String trick however.. ;p Apr 5, 2018 at 14:13 # APL (Dyalog Classic), 11 bytes ⊃∘⍕¨1∘<=0,⍳  Try it online! uses ⎕io←1 # PowerShell, 4937 35 bytes param($n)'C'*(1%$n)+1+'C'*($n-1%$n)  Try it online! Saved a pair of quotes by just making the E into a number. # Windows Batch, 75 bytes To be improved... @if %1==1 echo EC&exit/b @cd|set/p=CE @for /l %%G in (2,1,%1)do @cd|set/p=C  # Retina 0.8.2, 17 bytes .+ E$&$*C ECC CEC  Try it online! I could save a byte by using 1 for a car. • "I could save a byte by using 1 for a car." Then why don't you do so?.. In the challenge rules it states "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." Apr 5, 2018 at 13:33 # Octave, 3634 33 bytes x((n=input(''))+1)=0;x(1+(n>1))=1  Try it online! # Python 3, 31 bytes lambda n:'ECE'[n>1:n+1]+'C'*~-n  Try it online! At first I tried reversing EC if n>1, but converting a boolean to (-1,+1) turned out longer than using a different string instead. Alternative 31 byte solutions: All of these also work in Python 2. # Swift, 74 bytes func r(i:Int)->String{return i>1 ?"CE"+(1..<i).map{_ in"C"}.joined():"EC"}  Try it online! ## PHP, 103 Bytes Try it online! Code, recursive function function f($n,$i=1,$s=''){if($i<$n){$s.="CE".str_repeat("C",$i)."
";f($n,$i+1,$s);}else{echo"EC ".$s;}}


Explanation

function f($n,$i=1,$s=''){ if($i<$n){ #condition to get out, it counts to$n
$s.="CE".str_repeat("C",$i)."  # "C" is repeated and concatenated
";f($n,$i+1,$s); #note that "\n" = "(Enter key)" }else{ echo"EC #the first line "EC" ".$s;
}
}


Note that "\n" = " ", you save one byte

Output

f(5);
EC
CEC
CECC
CECCC
CECCCC

f(10);
EC
CEC
CECC
CECCC
CECCCC
CECCCCC
CECCCCCC
CECCCCCCC
CECCCCCCCC
CECCCCCCCCC

• I think you only have to print the result for one line. Apr 8, 2018 at 17:02

# Jelly, 8 bytes

«2=‘R\$;⁷


A full program printing the output with car lanes as 0 and the emergency lanes as 1.

Try it online!

# Yabasic, 54 bytes

Input""n
If n>1Then?"C";Fi
?"E";
For i=1To n:?"C";Next


Try it online!

# ><>, 26 bytes

::?v~1)?o"!">o<
00.>1-"~"}


Watch it run !

Results :

1.     !~
2.     ~!~
3.     ~!~~
4.     ~!~~~
etc.


# JavaScript, 28 bytes

f=n=>n-->2?f(n)+'1':n?121:21


# Excel VBA, 32 bytes

An anonymous VBE immediate window function that takes input from range [A1] and outputs to the VBE immediate window.

?[If(A1-1,10,"01")&Rept(1,A1-1)]
''  or
?[If(A1-1,10,0)&Rept(1,A1-A1<1)]

• Fails on A1=1 - outputs 0 instead of 01 Apr 6, 2018 at 10:16
• @Chronocidal why you are right, its been corrected Apr 7, 2018 at 16:04