46
\$\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\$
  • 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 '18 at 18:01
  • 16
    \$\begingroup\$ @PmanAce I really don't think flawr needs our help for this :P \$\endgroup\$ – orlp Apr 4 '18 at 18:04
  • 8
    \$\begingroup\$ +1 because it actually works in Germany. Was in the situation last weekend. \$\endgroup\$ – ElPedro Apr 4 '18 at 19:48
  • 10
    \$\begingroup\$ @msh210 I think the pictures in the german WP page explain it best. \$\endgroup\$ – flawr Apr 5 '18 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\$ – Kevin Cruijssen Apr 5 '18 at 11:32

69 Answers 69

1
\$\begingroup\$

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.

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

><>, 16 bytes

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

Try it online!

Prints 0 as the corridor and 1 as the cars.

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

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

Prolog (SWI), 63 51 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 35 bytes

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

Run as pipe with -n or try it online.

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ @JoKing thanks for the pointing. I think that I got it right now \$\endgroup\$ – Hille May 4 '18 at 8:06
  • \$\begingroup\$ You're still using L as a predefined variable, which is not allowed by default \$\endgroup\$ – Jo King May 4 '18 at 9:14
  • \$\begingroup\$ 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. \$\endgroup\$ – Jo King May 7 '18 at 9:02
0
\$\begingroup\$

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

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\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.