Make an Emergency Corridor

46

7

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

flawr

Posted 2018-04-04T17:58:48.690

Reputation: 40 560

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 corridor E. – user202729 – 2018-04-05T02:49:28.940

10

@msh210 I think the pictures in the german WP page explain it best.

– flawr – 2018-04-05T06:55:45.507

9You 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 – 2018-04-05T11:32:52.177

Is there a maximum n we need to support? In Java for example, long is a 64-bit number and a decimal double/float has only 15 digits after the comma, so using an arithmetic or decimal approach only works up to n=16.. I think this applies to every programming language without an arbitrary number-size. Some languages also start using 1e# 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.367

1@KevinCruijssen Using a built in integer type is sufficient. – flawr – 2018-04-06T09:19:54.927

Answers

29

Python 2, 29 26 bytes

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

Example:

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

orlp

Posted 2018-04-04T17:58:48.690

Reputation: 37 067

you need to output 21 in the n=1 case – DanielIndie – 2018-04-04T18:18:20.187

1@DanielIndie :( fixed but now it's ugly. – orlp – 2018-04-04T18:25:37.530

Still a very creative solution:) – flawr – 2018-04-04T18:30:13.987

1@orlp sorry :) but still a fine solution :) – DanielIndie – 2018-04-04T18:31:39.243

Nice solution! You can shorten 9*(n<2) to 1/n*9. – xnor – 2018-04-04T22:34:04.247

310**n*97/30-1/n*9 saves another byte, giving f(5) == 323333 etc. – Lynn – 2018-04-05T07:54:22.593

28

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!

Guoyang Qin

Posted 2018-04-04T17:58:48.690

Reputation: 543

2I think the 'straightforward' solution has unnecessary whitespace after lambda N: – my pronoun is monicareinstate – 2018-04-05T14:56:39.573

@someone I was not aware of that, thanks. – Guoyang Qin – 2018-04-05T15:25:11.083

26

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

nwellnhof

Posted 2018-04-04T17:58:48.690

Reputation: 10 037

14

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.

Shaggy

Posted 2018-04-04T17:58:48.690

Reputation: 24 623

I was going to post ç0 iQ1 for 6 bytes, but it would be better if you used it. – Oliver – 2018-04-04T19:40:53.227

Thanks, @Oliver. Got it down to 5 bytes in the meantime, though. – Shaggy – 2018-04-04T19:46:25.777

1ç¬iÅ for 4 bytes ;) I've never abused Japt this much. – Oliver – 2018-04-04T19:57:43.510

I was just about to do the same with Ä instead of Å :) – Shaggy – 2018-04-04T20:02:00.957

7

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

Punintended

Posted 2018-04-04T17:58:48.690

Reputation: 396

Save 8 bytes using 21=12+9 and forcing TRUE/FALSE to 1/0 without an if https://tio.run/##K/r/v6CossjKKk0jObFEw1JLI8/OUFPb0EinKLVAw0gnT9dQU6c4tcBWSUlT8/9/AA

– JayCe – 2018-04-27T18:43:04.683

6

Haskell, 38 34 32 bytes

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

Try it online!

Angs

Posted 2018-04-04T17:58:48.690

Reputation: 4 825

6

Python 2, 30 29 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, 33 32 31 29 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

Kevin Cruijssen

Posted 2018-04-04T17:58:48.690

Reputation: 67 575

1Your 10/3 one fails at 17 – Jo King – 2018-04-05T22:52:09.580

1@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 – 2018-04-06T09:42:44.503

5

Pyth, 10 9 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.

user48543

Posted 2018-04-04T17:58:48.690

Reputation:

5

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.

Tom Carpenter

Posted 2018-04-04T17:58:48.690

Reputation: 3 990

Very clever :-) – Stewie Griffin – 2018-04-05T15:33:47.153

@StewieGriffin Thanks :). Managed to knock off another 5 bytes more. – Tom Carpenter – 2018-04-05T16:56:45.997

5

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

Jo King

Posted 2018-04-04T17:58:48.690

Reputation: 38 234

4

Haskell, 35 33 32 bytes

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

(!!)$"":"EC":iterate(++"C")"CEC"

Try it online!

Haskell, 32 30 29 bytes

This is zero indexed so it doesn't comply with the challenge

g=(!!)$"EC":iterate(++"C")"CEC"

Try it online!

Haskell, 30 bytes

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.

Post Rock Garf Hunter

Posted 2018-04-04T17:58:48.690

Reputation: 55 382

((blah)!!) can become (!!)$blah to save a byte in your first two answers. – Lynn – 2018-04-05T12:44:06.040

@Lynn Thanks! I tried to do that earlier but I must have mis-counted the bytes. – Post Rock Garf Hunter – 2018-04-05T13:11:20.333

4

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.

J. Sallé

Posted 2018-04-04T17:58:48.690

Reputation: 3 233

1(⍵>1) doesn't need to be in parentheses. And you can save 4 bytes with a tacit function: (⊢×1<⊢)⌽'E',⍴∘'C'. – Erik the Outgolfer – 2018-04-04T20:47:18.737

@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é – 2018-04-04T21:35:24.387

(-≠∘1)⌽'E',⍴∘'C' or =∘1⌽¯1⌽'E',⍴∘'C'. – Adám – 2018-04-05T07:57:15.957

15 bytes with ⎕io=0: 'CE'[1(≠=∘⍳+)⎕] – ngn – 2018-04-06T03:50:28.300

@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é – 2018-04-06T16:17:00.283

@J.Sallé TIO link

– ngn – 2018-04-06T18:27:51.497

4

Jelly, 11 9 bytes

’0ẋ⁾0E;ṙỊ

Try it online!

Full program.

Uses 0 instead of C.

Erik the Outgolfer

Posted 2018-04-04T17:58:48.690

Reputation: 38 134

4

C (gcc), 39 bytes

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

Try it online!

Borrowed and adapted the printf trick from ErikF's answer.

kwc

Posted 2018-04-04T17:58:48.690

Reputation: 141

2Welcome to the site and nice first post! – caird coinheringaahing – 2018-04-04T21:40:34.153

4

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.
              C           padding character
               <          left align
                {n+1}     minimum field width based on n

RootTwo

Posted 2018-04-04T17:58:48.690

Reputation: 1 749

4

Brain-Flak, 100 66 bytes

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

Try it online!

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

Jo King

Posted 2018-04-04T17:58:48.690

Reputation: 38 234

+1 for using this language of all things. XD – Alex – 2018-04-05T09:16:31.260

2@Alex, Well, Brain-Flak is the language of the month for April – Jo King – 2018-04-05T09:39:44.250

Seriously or late April Fool's joke? Where do languages of the month get elected? – Alex – 2018-04-05T13:31:46.467

@Alex Nominations and voting here, and then a month-specific post is made such as this one

– Kamil Drakari – 2018-04-05T15:09:24.020

Oh, it's on this platform. I see, thanks! :-) – Alex – 2018-04-06T12:12:54.363

4

C#, 34 bytes

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

Try it online!

Hyarus

Posted 2018-04-04T17:58:48.690

Reputation: 251

4

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

Kaldo

Posted 2018-04-04T17:58:48.690

Reputation: 1 135

Oh you sly fox. $<×TìsiR was how I was thinking. – Magic Octopus Urn – 2018-04-05T22:37:42.537

@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 – 2018-04-06T07:29:05.470

In the new version of 05AB1E, 1I can be golfed to $. – Kevin Cruijssen – 2018-12-10T15:32:54.173

5 bytes (also works in the legacy version). – Kevin Cruijssen – 2018-12-11T07:09:59.110

4

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.

Kirill L.

Posted 2018-04-04T17:58:48.690

Reputation: 6 693

3

JavaScript (Node.js), 28 bytes

n=>n<2?21:"12".padEnd(n+1,1)

Try it online!

DanielIndie

Posted 2018-04-04T17:58:48.690

Reputation: 1 220

3

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

recursive

Posted 2018-04-04T17:58:48.690

Reputation: 8 616

3

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.

Graham

Posted 2018-04-04T17:58:48.690

Reputation: 3 184

16: (-2≠⍴n)⌽n←1⎕/⍕10 – Adám – 2018-05-01T08:03:50.327

@Adám Thanks. I see ngn's trick, 1⎕/⍕, is coming in useful. One for the idiom list? – Graham – 2018-05-01T15:11:50.117

What idiom list are you talking about? – Adám – 2018-05-01T15:44:41.817

@Adám The two idiom lists I use most frequently are the Finnapl and APL2idioms – Graham – 2018-05-01T15:52:21.113

I'm not sure what's so idiomatic here. It is just golfy. Anyway, my idiom list may interest you.

– Adám – 2018-05-01T17:06:10.020

3

Perl 5 -p, 27 20 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.

Xcali

Posted 2018-04-04T17:58:48.690

Reputation: 7 671

3

APL (Dyalog Unicode), 16 bytes

≡∘1⌽'CE',1↓⍴∘'C'

Try it online!

Erik the Outgolfer

Posted 2018-04-04T17:58:48.690

Reputation: 38 134

3

JavaScript (Node.js), 19 bytes

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

Try it online!

nwellnhof

Posted 2018-04-04T17:58:48.690

Reputation: 10 037

3

Whitespace, 141 104 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}
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.

Kevin Cruijssen

Posted 2018-04-04T17:58:48.690

Reputation: 67 575

Would it be easier to print the clear lane as - by abusing printing -1? – Jo King – 2018-04-05T08:40:51.023

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

– Kevin Cruijssen – 2018-04-05T09:09:10.363

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 – 2018-04-05T09:38:07.397

1

@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. :)

– Kevin Cruijssen – 2018-04-05T11:01:03.837

3

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.

Dennis

Posted 2018-04-04T17:58:48.690

Reputation: 196 637

3

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.

nelsontruran

Posted 2018-04-04T17:58:48.690

Reputation: 241

3

J, 11 bytes

1":1&<=i.,]

Try it online!

Based on ngn’s comment.

and :

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

FrownyFrog

Posted 2018-04-04T17:58:48.690

Reputation: 3 112

3

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

Kevin Cruijssen

Posted 2018-04-04T17:58:48.690

Reputation: 67 575

2

Canvas, 11 bytes

┤C×EC×╴╷?C×

Try it here!

dzaima

Posted 2018-04-04T17:58:48.690

Reputation: 19 048

2

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

Digital Trauma

Posted 2018-04-04T17:58:48.690

Reputation: 64 644

1

You know already what the second group will contain, so you can avoid it Try it online!

– Leo – 2018-04-05T02:32:46.607

@Leo thanks - very good! – Digital Trauma – 2018-04-05T17:13:04.033

2

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!

dylnan

Posted 2018-04-04T17:58:48.690

Reputation: 4 993

2

C (gcc), 48 bytes

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

Try it online!

ErikF

Posted 2018-04-04T17:58:48.690

Reputation: 2 149

2

Python 3, 39 bytes

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

Try it online!

Dat

Posted 2018-04-04T17:58:48.690

Reputation: 879

You can replace n==1 with n<2 since n will always be >0. – Oliver – 2018-04-04T21:16:38.213

'C'*(n>1) can be replaced with 1%n*'C' – ovs – 2018-04-05T13:14:06.807

2

C# (.NET Core), 39 bytes

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

Try it online!

Ian H.

Posted 2018-04-04T17:58:48.690

Reputation: 2 431

The solution is wrong. You have on C to many at the end of each line for n>1. – raznagul – 2018-04-05T13:49:35.150

@raznagul I missed that, thanks for pointing it out. I fixed it. – Ian H. – 2018-04-05T18:10:17.363

2

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.

Veskah

Posted 2018-04-04T17:58:48.690

Reputation: 3 580

2

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.

Jo King

Posted 2018-04-04T17:58:48.690

Reputation: 38 234

2

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`

Kevin Cruijssen

Posted 2018-04-04T17:58:48.690

Reputation: 67 575

Nice, you beat my best guess at 39 bytes: n->n--<2?"10":"".format("01%0"+n+"d",0). – Olivier Grégoire – 2018-04-05T11:16:16.243

@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. – Kevin Cruijssen – 2018-04-05T11:23:24.937

@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 – Kevin Cruijssen – 2018-04-05T14:13:20.307

2

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

ovs

Posted 2018-04-04T17:58:48.690

Reputation: 21 408

2

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.

Sanchises

Posted 2018-04-04T17:58:48.690

Reputation: 8 530

2

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

Wernisch

Posted 2018-04-04T17:58:48.690

Reputation: 2 534

2

J, 20 bytes

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

Try it online!

Galen Ivanov

Posted 2018-04-04T17:58:48.690

Reputation: 13 815

2

Python, 53 bytes

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

Try it online!

Alex Brunt

Posted 2018-04-04T17:58:48.690

Reputation: 21

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.

– caird coinheringaahing – 2018-04-05T13:05:39.173

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!

– Kevin Cruijssen – 2018-04-05T13:07:06.657

2

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

Chronocidal

Posted 2018-04-04T17:58:48.690

Reputation: 571

2

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

FRex

Posted 2018-04-04T17:58:48.690

Reputation: 41

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. – Post Rock Garf Hunter – 2018-04-05T23:23:03.747

The current top Python answer uses a number printing in an interactive prompt too and doesn't use print. – FRex – 2018-04-05T23:27:23.223

Huh. I didn't see that. I would leave a comment on the OP to make sure that this output form is permissible. – Post Rock Garf Hunter – 2018-04-06T00:31:10.903

@user56656 Or just let the language be "Haskell (interactive)". Done. (or does it work? consider this is a function submission) – user202729 – 2018-04-06T03:25:15.790

ghci requires let to bind a, the example would result in a parse error – Angs – 2018-04-08T18:30:29.053

Scratch that, it's new in ghci 8 it seems. Might want to add that specifier in the language header. – Angs – 2018-04-08T18:36:59.090

It's just an example of it working. It's clear it uses GHCI 8. GHCI is not a language (and neither is 'interactive Haskell'). I have gotten one vote and two nitpickers. No one has told the top Python answer they return a number or that their language is 'interactive Python' and that answer is what I looked at when writing mine. No one also has problems with lambdas/functions in Python or other languages if the question didn't say "write a full program" but I got a comment about that as well. So much for a healthy welcoming community. – FRex – 2018-04-09T00:43:28.917

2

Perl 5 -lp, 19 bytes

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

Try it online!

Ton Hospel

Posted 2018-04-04T17:58:48.690

Reputation: 14 114

2

APL (Dyalog Classic), 11 bytes

⊃∘⍕¨1∘<=0,⍳

Try it online!

uses ⎕io←1

ngn

Posted 2018-04-04T17:58:48.690

Reputation: 11 449

1

Prolog (SWI), 63 51 bytes

1+`EC`.
2+`CEC`.
N+X:-M is N-1,M+Y,append(Y,`C`,X).

Try it online!

Post Rock Garf Hunter

Posted 2018-04-04T17:58:48.690

Reputation: 55 382

1

Octave, 36 34 33 bytes

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

Try it online!

Stewie Griffin

Posted 2018-04-04T17:58:48.690

Reputation: 43 471

1

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

stevefestl

Posted 2018-04-04T17:58:48.690

Reputation: 539

1

Retina 0.8.2, 17 bytes

.+
E$&$*C
ECC
CEC

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

Neil

Posted 2018-04-04T17:58:48.690

Reputation: 95 035

"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." – Kevin Cruijssen – 2018-04-05T13:33:16.237

1

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.

Jo King

Posted 2018-04-04T17:58:48.690

Reputation: 38 234

1

JavaScript, 28 bytes

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

l4m2

Posted 2018-04-04T17:58:48.690

Reputation: 5 985

1

Swift, 74 bytes

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

Try it online!

Tamás Sengel

Posted 2018-04-04T17:58:48.690

Reputation: 211

1

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

Francisco Hahn

Posted 2018-04-04T17:58:48.690

Reputation: 591

1I think you only have to print the result for one line. – Titus – 2018-04-08T17:02:39.143

1

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!

Jonathan Allan

Posted 2018-04-04T17:58:48.690

Reputation: 67 804

1

Yabasic, 54 bytes

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

Try it online!

Taylor Scott

Posted 2018-04-04T17:58:48.690

Reputation: 6 709

1

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)]

Taylor Scott

Posted 2018-04-04T17:58:48.690

Reputation: 6 709

Fails on A1=1 - outputs 0 instead of 01 – Chronocidal – 2018-04-06T10:16:25.883

@Chronocidal why you are right, its been corrected – Taylor Scott – 2018-04-07T16:04:19.780

1

><>, 26 bytes

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

Watch it run !

Results :

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

Aaron

Posted 2018-04-04T17:58:48.690

Reputation: 3 689

1

Ruby, 25 bytes

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

Try it online!

Asone Tuhid

Posted 2018-04-04T17:58:48.690

Reputation: 1 944

1

Perl 6, 20 bytes

{1%$_*10~1 x$_-1%$_}

Try it online!

Returns

1 => 01
2 => 101
3 => 1011
4 => 10111
5 => 101111

Another 20 byte solution with -p is $_=1 x$_;s/1?<(1/E1/ which is identical to Xcali's Perl 5 solution.

nwellnhof

Posted 2018-04-04T17:58:48.690

Reputation: 10 037

1

><>, 16 bytes

1-:?!v1$
n<}1{<>

Try it online!

Prints 0 as the corridor and 1 as the cars.

Jo King

Posted 2018-04-04T17:58:48.690

Reputation: 38 234

1

Befunge-98 (FBBI), 14 bytes

Uses 0 for cars, and 1 for the empty lane.

12&:b0p`j\' k.

Try it online!

This uses a & to end the program, stalling it until TIO shuts it off, instead of ending when it's finished. In order to avoid waiting, you can hit the run button again soon after it starts to end it prematurely and see the result it would give if it waited. In case ending like this is too iffy, here is a 15 byte solution that doesn't use this method:

12&:b0p`j\' k.@

Try it online!

Explanation

This program uses the implicit zeroes at the bottom of the stack to print the cars at the end. This way we can avoid pushing N - 1 things, and only use the input for printing N + 1 things and determining whether N is 1 or not.

(stack terminology is bottom [a, b, c] top)

12                Push 1 and 2. The stack is [1, 2]
  &:              Take input and duplicate. [1, 2, N, N]
    b0p           Put N at (0, 11), which is after the '
       `          Pops the top 2 and pushes 1 if a>b, 0 if not. [1, 2>N]
        j\        Jumps the \ if 2>N (i.e. N = 1), otherwise switches the top 2.
                      The stack is [1] if N=1, otherwise [1, 0]
          '       Takes in N again from the put instruction earlier
            k.    Prints out N+1 characters: (01 or 10 depending on N) + (N-1 0s).
12                Wraps and adds irrelevant things to the stack.
  &               Waits for TIO to end the program.

MildlyMilquetoast

Posted 2018-04-04T17:58:48.690

Reputation: 2 907

0

PHP, 35 bytes

<?=str_pad(1<$argn?CE:E,C,$argn+1);

Run as pipe with -n or try it online.

Titus

Posted 2018-04-04T17:58:48.690

Reputation: 13 814

0

C# (.NET Core), 74 bytes

Additional bytes for using System; and int L

var S="0X";for(int I=1;I<L;I++)S+='0';System.Console.Write(L<2?"X0":S);

Try it online!

Hille

Posted 2018-04-04T17:58:48.690

Reputation: 349

@JoKing thanks for the pointing. I think that I got it right now – Hille – 2018-05-04T08:06:17.410

You're still using L as a predefined variable, which is not allowed by default

– Jo King – 2018-05-04T09:14:56.130

Defining L as an int is not what I meant. Either your submission is a full program (which needs the boilerplate as well as taking input from an acceptable input) or a function that can take an int. – Jo King – 2018-05-07T09:02:33.297

0

Pyt, 18 bytes

Đ1≤?ĉ01:ŕ01↔⁻⑴;áƑǰ

Try it online!

Uses 0 for the emergency lane and 1 for the normal lanes

Explanation

                          Implicit input (N)
Đ                         Duplicate N on the stack
 1≤                       Is N≤1
   ?   :       ;          If top of stack is non-zero, continue execution until reaching 
                               colon, then jump to semicolon; otherwise, jump to colon 
                               and continue executing
   ?                      - Then (i.e. i≤1)
    ĉ                     Clear the stack
     01                   Push 0 followed by 1

       :                  - Else branch (i.e. N≥2)
        ŕ                 Pop the top of the stack
         01               Push 0 followed by 1
           ↔              Flip the stack
            ⁻⑴           Decrement N and then push an array of N-1 1's
               ;          - End of if statement

                á         Push contents of stack to an array
                 Ƒ        Flatten the resulting array
                  ǰ       Join the contents of the array with no delimiters

mudkip201

Posted 2018-04-04T17:58:48.690

Reputation: 833

0

Pushy, 10 bytes

1wEs{t?};Q

Try it online!

1             \ Push 1
 w            \ 'Mirror' the stack around the central item, yielding [n, 1, n]
  E           \ Pop 1 and n, and push 1 * 10 ** n
   s          \ Split into its digits
    {         \ Get input back on top of stack
     t?       \ If input > 1:
       };     \    Cyclically shift the stack right once
         Q    \ Collect all items on stack, interpret as indicies into uppercase alphabet,
              \ and print the resulting string.

Prints A for lanes and B for corridors.

FlipTack

Posted 2018-04-04T17:58:48.690

Reputation: 13 242

0

ink, 55 33 55 bytes

=l(n)
~temp i=2
{n-1:C}EC<>
-(k){i<n:
~i++
C<>->k
}->->

Try it online!

Starts by printing C if input is not 1, then EC, then prints C another n-2 times.

Sara J

Posted 2018-04-04T17:58:48.690

Reputation: 2 576