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

71 Answers 71

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

naz, 104 bytes

2x0v1a2x1v1x1f1o1s1o0x1x2f2x2v0m1o0m1a1f2v2s2x2v3f0x1x3f2v3x0v4e1s2x2v0m1o3f0x1x4f0a0x1x5f1r3x1v1e2f0x5f

There may be some byte saves I haven't immediately seen. I'm starting to be out of practice with my own language.

This program uses 1 for the emergency corridor and 0 for the cars, and accepts its input as a character code, which is allowed by meta consensus.

Due to integer constraints, this works for all N where 0 < N < 128. Use the -u flag to remove the constraints.

Explanation (with 0x commands removed)

2x0v                         # Set variable 0 equal to 0
1a2x1v                       # Set variable 1 equal to 1
1x1f                         # Function 1
    1o1s1o                   # Output, then subtract 1 and output again
1x2f                         # Function 2
    2x2v                     # Set variable 2 equal to the current register value
        0m1o0m1a1f           # Output "010" with the help of function 1
                  2v2s2x2v   # Subtract 2 from variable 2
                          3f # Call function 3
1x3f                         # Function 3
    2v3x0v4e                 # Goto function 4 if variable 2 is equal to variable 0 (0)
            1s2x2v           # Otherwise, subtract 1 from variable 2,
                  0m1o       # output 0,
                      3f     # and jump back to the start of function 3
1x4f                         # Function 4
    0a                       # Add 0 to the register
1x5f                         # Function 5
    1r                       # Read a byte of input
      3x1v1e                 # Goto function 1 if it equals variable 1
            2f               # Otherwise, call function 2
5f                           # Call function 5
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 6 bytes

↵S$ċ[ǔ

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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