47
\$\begingroup\$

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
\$\endgroup\$
12
  • 18
    \$\begingroup\$ I won't fall for this! You're just looking for a lane of your own to slither through you sneaky snake. \$\endgroup\$
    – orlp
    Apr 4, 2018 at 18:01
  • 16
    \$\begingroup\$ @PmanAce I really don't think flawr needs our help for this :P \$\endgroup\$
    – orlp
    Apr 4, 2018 at 18:04
  • 8
    \$\begingroup\$ +1 because it actually works in Germany. Was in the situation last weekend. \$\endgroup\$
    – ElPedro
    Apr 4, 2018 at 19:48
  • 10
    \$\begingroup\$ @msh210 I think the pictures in the german WP page explain it best. \$\endgroup\$
    – flawr
    Apr 5, 2018 at 6:55
  • 9
    \$\begingroup\$ 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. ;) \$\endgroup\$ Apr 5, 2018 at 11:32

70 Answers 70

2
\$\begingroup\$

Canvas, 11 bytes

┤C×EC×╴╷?C×

Try it here!

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 48 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 39 bytes

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

Try it online!

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

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;
  • IR`CP{`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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

J, 20 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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'.

\$\endgroup\$
2
\$\begingroup\$

Python, 53 bytes

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 5, 2018 at 13:05
  • \$\begingroup\$ 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! \$\endgroup\$ Apr 5, 2018 at 13:07
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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

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

C# (.NET Core), 39 bytes

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

Try it online!

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

Haskell (interactive), 30 bytes

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
\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Apr 5, 2018 at 23:23
  • \$\begingroup\$ The current top Python answer uses a number printing in an interactive prompt too and doesn't use print. \$\endgroup\$
    – FRex
    Apr 5, 2018 at 23:27
  • \$\begingroup\$ Huh. I didn't see that. I would leave a comment on the OP to make sure that this output form is permissible. \$\endgroup\$
    – Wheat Wizard
    Apr 6, 2018 at 0:31
  • \$\begingroup\$ @user56656 Or just let the language be "Haskell (interactive)". Done. (or does it work? consider this is a function submission) \$\endgroup\$
    – DELETE_ME
    Apr 6, 2018 at 3:25
  • \$\begingroup\$ ghci requires let to bind a, the example would result in a parse error \$\endgroup\$
    – Angs
    Apr 8, 2018 at 18:30
2
\$\begingroup\$

Perl 5 -lp, 19 bytes

#!/usr/bin/perl -lp
$_=--$_?CE.C x$_:EC

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Java 10, 53 51 45 42 38 37 36 35 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`
\$\endgroup\$
3
  • \$\begingroup\$ Nice, you beat my best guess at 39 bytes: n->n--<2?"10":"".format("01%0"+n+"d",0). \$\endgroup\$ Apr 5, 2018 at 11:16
  • \$\begingroup\$ @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. \$\endgroup\$ Apr 5, 2018 at 11:23
  • \$\begingroup\$ @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 \$\endgroup\$ Apr 5, 2018 at 14:13
2
\$\begingroup\$

APL (Dyalog Classic), 11 bytes

⊃∘⍕¨1∘<=0,⍳

Try it online!

uses ⎕io←1

\$\endgroup\$
2
\$\begingroup\$

PowerShell, 49 37 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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 17 bytes

.+
E$&$*C
ECC
CEC

Try it online! I could save a byte by using 1 for a car.

\$\endgroup\$
1
  • \$\begingroup\$ "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." \$\endgroup\$ Apr 5, 2018 at 13:33
1
\$\begingroup\$

Octave, 36 34 33 bytes

x((n=input(''))+1)=0;x(1+(n>1))=1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Swift, 74 bytes

func r(i:Int)->String{return i>1 ?"CE"+(1..<i).map{_ in"C"}.joined():"EC"}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you only have to print the result for one line. \$\endgroup\$
    – Titus
    Apr 8, 2018 at 17:02
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Yabasic, 54 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

><>, 26 bytes

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

Watch it run !

Results :

1.     !~
2.     ~!~
3.     ~!~~
4.     ~!~~~
etc.
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 28 bytes

f=n=>n-->2?f(n)+'1':n?121:21
\$\endgroup\$
1
\$\begingroup\$

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)]
\$\endgroup\$
2
  • \$\begingroup\$ Fails on A1=1 - outputs 0 instead of 01 \$\endgroup\$ Apr 6, 2018 at 10:16
  • \$\begingroup\$ @Chronocidal why you are right, its been corrected \$\endgroup\$ Apr 7, 2018 at 16:04
1
\$\begingroup\$

Ruby, 25 bytes

->n{n>1?'CE'+?C*~-n:'EC'}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.