12
\$\begingroup\$

There is a building with an infinite number of floors and you are the only passenger in an elevator which can lift an infinite number of people. You are going home, but before the elevator gets to your floor, the elevator stops at another floor to allow people to enter the elevator and input their destination floors. Your program should handle the floor order correctly


You will be given 3 inputs (the order of input doesn't matter but you need to specify the expected order of inputs):

  • Integer - The number of the floor where the people entered the elevator
  • Integer - Your destination floor
  • Array of integers - The destination floors of entered people

You must output an array of integers with the numbers of floors (including yours) ordered correctly

The correct order is this order, where first the floors that are in the direction of your movement go in an ordered order, and then the floors that are in the direction opposite to yours go in reverse order

  • The floor where the people entered is the floor where elevator already stopped so this floor should not be in the order of future destinations (output array)
  • There will not be any duplicates in the input array
  • Your destination floor will not be in the input array

For example let's say that elevator stopped at the ground floor, your destination is 3rd floor and people entered [4,-2,-5,2,-4,1,5]

So the output of must be [1,3,4,5,-2,-4,-5]

Test cases: (feel free to add more test cases)

0, 3, [4,-2,-5,2,-4,1,5] --> [1,2,3,4,5,-2,-4,-5]

0, -3, [4,-2,-5,2,-4,1,5] --> [-2,-3,-4,-5,1,2,4,5]

5, 10 [1,3,7,9,11,-3,-10] --> [7,9,10,11,3,1,-3,-10]

The shortest code in each programming language wins!

\$\endgroup\$
3
  • 8
    \$\begingroup\$ You are the only person in a elevator that can lift a infinite number of people? I think you'd be the only person on earth who could lift a infinite number of people. \$\endgroup\$ Commented Feb 23, 2023 at 6:46
  • 1
    \$\begingroup\$ @mousetail why does a person have to lift an infinite number? Isn't it clear that the elevator can lift an infinite number of people? If not, please, edit this sentence to make it clear. I will be grateful \$\endgroup\$
    – EzioMercer
    Commented Feb 23, 2023 at 8:37
  • \$\begingroup\$ @EzioMercer the comment by mousetail is a humorous play on words, the comment upvoters all get the joke and also get the intended meaning of your sentence \$\endgroup\$
    – Darren H
    Commented Feb 24, 2023 at 15:51

12 Answers 12

8
\$\begingroup\$

Python 3, 65 64 62 61 bytes

lambda s,m,a:sorted([m]+a,key=lambda x:(x>s)*(s-m)-(s-x)**-2)

Try it online!

I think this may not work due to floating point inaccuracy once you reach a certain floor, since values that are too large eventually will round to the same value. Not sure what can fix that though

I reread the rules and saw there will never be duplicates so -1 byte

making the function anonymous; -2 bytes

-1 byte thanks to xnor's suggestion using **-2 instead of 1/abs

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$
    – rydwolf
    Commented Feb 22, 2023 at 22:02
  • \$\begingroup\$ The output for first testcase must be [1,2,3,4,5,-2,-4,-5] not [-2, -4, -5, 1, 2, 3, 4, 5] \$\endgroup\$
    – EzioMercer
    Commented Feb 22, 2023 at 22:14
  • 1
    \$\begingroup\$ fixed, had to add 6 bytes though \$\endgroup\$
    – Bob th
    Commented Feb 22, 2023 at 22:34
  • 1
    \$\begingroup\$ Welcome and nice first answer! Since your function isn't recursive, you can remove the e= to save 2 bytes: Try it online! \$\endgroup\$
    – The Thonnu
    Commented Feb 23, 2023 at 16:18
  • 1
    \$\begingroup\$ It looks like you can do (s-x)**-2 in place of 1/abs(s-x) \$\endgroup\$
    – xnor
    Commented Feb 24, 2023 at 5:00
6
\$\begingroup\$

R, 47 bytes

\(e,d,l,m=c(d,l))m[order(!(m<e)-(e<d),(m-e)^2)]

Attempt This Online!

Approach copied from Jonah's answer - upvote that one!


Previously: R, 56 bytes

\(e,d,l,u=e<d,`+`=sort)c(c(d,l[i<-!(l>e)-u])+!u,l[!i]+u)

Attempt This Online!

Uses the decreasing argument of sort to control sorting order.

\$\endgroup\$
4
\$\begingroup\$

J, 29 27 bytes

}:@,([/:(~:&*-/),.(|@-{:))]

Try it online!

  • Sorts by:
    • First: Are you in the same direction as start - destination, as a 0 1.
    • Then: Absolute distance from start
\$\endgroup\$
4
\$\begingroup\$

R, 71 61 59 bytes

-10 bytes thanks to pajonk, -2 thanks to Dominic

\(x,y,z,i=sort(c(z,y)))c(i[i>x&y>x],rev(i[i<x]),i[i>x&y<x])

Attempt This Online!

\$\endgroup\$
2
  • \$\begingroup\$ -10 bytes. \$\endgroup\$
    – pajonk
    Commented Feb 22, 2023 at 19:59
  • 3
    \$\begingroup\$ Note that there is no necessity to name this function: you can leave it as an anonymous function to save the 2 bytes of f=. \$\endgroup\$ Commented Feb 22, 2023 at 23:01
3
\$\begingroup\$

Haskell, 89 bytes

import Data.List
g z|z=id;g _=flip;f x y v|(a,b)<-span(<x)$sort$y:v=g(x<y)(++)b$reverse a

Attempt This Online!

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 17 bytes

ïš.¡›}ε{¹I‹N^iR]˜

Inputs in the order myDestinationFloor, list, startFloor.

Try it online or verify all test cases.

Explanation:

ï              # Explicitly cast the first (implicit) input to an integer
 š             # Prepend this first input-integer to the second (implicit) input-list
  .¡           # Group the integers in the list by:
    ›          #  Check if the third (implicit) input is larger than the current integer
   }ε          # After the group-by: map over the pair of lists:
     {         #  Sort the integers in the current group in ascending order
      ¹I‹      #  Check if the first input is smaller than the third input
         N^    #  Bitwise-XOR it with the 0-based map-index
           i   #  If this is truthy:
            R  #   Reverse the list to a descending order
    ]          # Close both the if-statement and map
     ˜         # Flatten this pair of lists to a single list
               # (after which the result is output implicitly)

Two notes:

  1. The cast to integer ï is necessary for the sort {. Without it, the input would be added as a stringified integer, and the sort would put it incorrectly after all the other integers in its group.
  2. The bitwise-XOR ^ is to determine whether we need to reverse the list to a descending sorted group, which will apply as follows:
¹I‹ (first input < third input?) N (is it the second iteration of the pair of lists?) iR (reverse it?)
truthy 0 yes
truthy 1 no
falsey 0 no
falsey 1 yes
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 62 bytes

Expects (stopFloor, myFloor, array).

(p,q,a)=>[...a,q].sort((a,b)=>((a<p)-(b<p))*(q-p)+(a-p)/(a-b))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 34 bytes

⊞ζηW⁻ζυ⊞υ⌊ι≔⮌Φυ‹ιθζI⎇‹ηθ⁺ζ⁻υζ⁺⁻υζζ

Try it online! Link is to verbose version of code. Explanation:

⊞ζη

Add your destination floor to the list.

W⁻ζυ⊞υ⌊ι

Sort all of the floors in ascending order.

≔⮌Φυ‹ιθζ

Extract the floors below your starting floor and reverse them.

I⎇‹ηθ⁺ζ⁻υζ⁺⁻υζζ

Remove them from the sorted list of floors and then concatenate them to either the beginning or the end depending on your direction of travel.

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

MATL, 20 bytes

h!ti-tZSt0)=~w|h&XS)

Try it out at MATL Online

Input order is array of destinations, your destination, and the initial floor

Explanation

          % Implicitly grab input
h!        % Append your desired floor with the floors of everyone else
t         % Duplicate this
i-        % Subtract the initial floor
t         % Duplicate this delta
ZSt0)=~   % Compare the sign of our target floor with the sign of everyone else
          % and invert so same direction as us == 0, otherwise == 1
w         % Flip the top of the stack to get the deltas again
|         % Compute the absolute value to get the distance
h         % Horizontally concatenate the sign-agreement array with
          % the array of distances
&XS       % Sort the rows and retrieve the row indexes
)         % Index into our original array of all destinations
          % Implicitly display the result
\$\endgroup\$
2
\$\begingroup\$

Arturo, 55 53 bytes

$[g,d,a][arrange a++d'x->*(x<g)?->1->9(g-d)-^g-x 0-2]

Try it

Port of Bob th's Python answer.

-2 bytes thanks to a suggestion by xnor.

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

Ruby, 58 57 56 bytes

->a,b,l{(l<<b).sort_by{|x|x>a ?x-a:l.sort[a>b ?0:-1]-x}}

Try it online!

How it works

Sort all floors above the current in ascending order, then all the floors below in descending order, then put the 2 arrays together in the right order.

This is done by "shifting" the floor numbers, if we are going up, then the floors below current get a higher number in reverse order (starting from max floor + 1 for the highest floor below current). If we are going down, then the floors below get a lower number, also in reverse order (from min floor + 1 for the highest floor below current).

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

Wolfram Language (Mathematica), 99 bytes

Join@@({s,m,a}={##};t={SortBy[#1,(s-#)&],#2}&@@SplitBy[Sort@Append[a,m],#>s&];If[m>s,Reverse@t,t])&

Try it online!

Need to be golfed =(
try to do it later...

\$\endgroup\$

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.