Skip to main content
3 of 4
edited body
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

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!

How?
Ungolfed code: uses the decreasing argument of sort to control sorting order:

f=function(e,d,l){      # e=entry, d=destination, l=list of passenger floors
    u=e<d               # u=boolean: TRUE=going up; FALSE=going down
    i=!(l>e)-u          # i=elements of l in the first direction
    c(                  # join together the first & second directions
    sort(c(d,l[i])      # sort the stops in the first direction...
                  ,!u)  # using 2nd arg of sort: decreasing TRUE/FALSE=!u
    ,sort(l[!i]         # sort the stops in the second direction
              ,u)       # in the other order: descending TRUE/FALSE=u
    )
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60