Assign Airliner Seats

16

Inspired by last week's APL lesson.

Given an uppercase 2D seat map and a 1D list of customers, return the seat map and the customer list but modified as follows (to indicate occupied seats and seated customers):

For each unique letter in the input passenger list, lowercase that many (or all, if there are not enough) of that letter in the seat map, going left-to-right, top-to-bottom.

For each unique letter in the seat map, lowercase that many (or all, if there are not enough) of that letter in the passenger list, going left-to-right.

  1. The input seat map only contains spaces and uppercase letters from the set {F,B,P,E}, and may be:
    1. separated into rows by line breaks
    2. a list of strings
    3. a matrix of uppercase characters
    4. in any other comparable format
  2. The input customer list only contains uppercase letters from the set {F,B,P,E} and may be:
    1. a string
    2. a list of characters
    3. in any other comparable format
  3. The returned seat map must be identical to the input one, except that zero or more letters have been folded to lowercase
  4. The returned customer list must be identical to the input one, except that zero or more letters have been folded to lowercase
  5. Leading and trailing whitespace is allowed

Examples (shortened editions of United's aircraft)

ERJ145

Seat map input:

P    
E    
E  PP
E  EE
E  EE
E  EE
E  EE

P  PP
E  EE
E  EE
E  EE

Passenger list input:

FFEEEEEEEEEEEEEEEE

Seat map output:

P    
e    
e  PP
e  ee
e  ee
e  ee
e  ee

P  PP
e  eE
E  EE
E  EE

Passenger list output:

FFeeeeeeeeeeeeeeee

CRJ700

Seat map input:

F   FF
F   FF
PP  PP
PP  PP
PP  PP
EE  EE

PP  PP
EE  EE
EE  EE
EE  EE

Customer list input:

FFFFFFFFPPEEEEEEEEEEEEEEEEEEEEE

Seat map output:

f   ff
f   ff
pp  PP
PP  PP
PP  PP
ee  ee

PP  PP
ee  ee
ee  ee
ee  ee

Customer list output:

ffffffFFppeeeeeeeeeeeeeeeeEEEEE

B757

Seat map input:

F F  F F
F F  F F
F F  F F

PPP     
PPP  PPP
PPP  PPP
PPP  PPP
EEE  EEE
EEE  EEE
PPP  PPP
EEE  EEE
EEE  EEE
     EEE

EEE  EEE
EEE  EEE
EEE  EEE
EEE     

Passenger list input:

FEEEEEEEEFEEEFEEEEEEEEFEEFFEEFEFFFEE

Seat map output:

f f  f f
f f  f f
f f  F F

PPP     
PPP  PPP
PPP  PPP
PPP  PPP
eee  eee
eee  eee
PPP  PPP
eee  eee
eee  eee
     eeE

EEE  EEE
EEE  EEE
EEE  EEE
EEE     

Passenger list output:

feeeeeeeefeeefeeeeeeeefeeffeefefffee

B767

Seat map input:

 F   F   F           
 F   F   F           
BB  B B  BB          
BB  B B  BB          
BB  B B  BB          
PP       BB          
PP                   

PP  PPP  PP          
PP  PPP  PP          
PP  PPP  PP          
PP  PPP  PP          
PP  EEE  PP          
EE  EEE  EE          
EE  EEE  EE          
EE  EEE  EE          
EE  EEE  EE          
EE  EEE  EE          

Passenger list input:

PPFEFEEEEEEEBBEEFFPEBPEBBEEFEEEFEEEEEEFPEEEPB

Seat map output:

 f   f   f           
 f   f   f           
bb  b b  bb          
BB  B B  BB          
BB  B B  BB          
pp       BB          
pp                   

pp  PPP  PP          
PP  PPP  PP          
PP  PPP  PP          
PP  PPP  PP          
PP  eee  PP          
ee  eee  ee          
ee  eee  ee          
ee  eee  ee          
ee  EEE  EE          
EE  EEE  EE          

Passenger list output:

ppfefeeeeeeebbeeffpebpebbeefeeefeeeeeeFpeeepb

Adám

Posted 2018-05-08T09:56:21.140

Reputation: 37 779

3

For those who, like me, wonder what the letters mean, here's a link to relevant part of the chat

– JayCe – 2018-05-08T18:11:04.427

Answers

5

Python 2, 93 89 84 83 82 78 bytes

l=input()
for c in l[1]:l=[x.replace(c,c.lower(),c in l[0])for x in l]
print l

Try it online!

Takes input as two strings. Prints two strings


Saved

  • -5 bytes, thanks to Dead Possum
  • -4 bytes, thanks to Lynn

TFeld

Posted 2018-05-08T09:56:21.140

Reputation: 19 246

1s,p=[x.replace(c,c.lower(),c in s)for x in[s,p]] for 84 bytes – Dead Possum – 2018-05-08T10:27:35.537

1A full program is a little shorter: l=input() for c in l[1]:l=[x.replace(c,c.lower(),c in l[0])for x in l] print l – Lynn – 2018-05-08T20:30:48.827

5

05AB1E, 22 16 15 bytes

Saved 6 bytes thanks to Nit noticing that the seat map could be taken as a string.

svDyå·Fyyl.;s]»

Try it online!

Explanation

s                     # setup stack as <passengerlist>,<seatmap>,<passengerlist>
 v                    # for each passenger y
  Dyå                 # does a corresponding seat exist?
     ·F               # multiplied by 2 times do:
       yyl.;          # replace the first y with a lowercase y
            s         # and swap the seatmap and passengerlist on the stack
             ]        # end loops
              »       # join seatmap and passengerlist on newline and output

Emigna

Posted 2018-05-08T09:56:21.140

Reputation: 50 798

1The seatmap can be input as a single string with line breaks, wouldn't that save you a byte or two? – Nit – 2018-05-08T13:38:22.550

@Nit: Ah, It should indeed save me some. Thanks :) – Emigna – 2018-05-08T14:49:40.570

5

C (clang), 75 68 bytes

f(char*s,char*p){char*r;while(*s){if(r=strchr(p,*s))*r=*s+=32;s++;}}

Takes two char * (seats & passengers) whose contents are modified in-place.

Try it online!

I'm less used to golfing in C than in Python, but that's fun as well !

If anyone has an idea for a trick to shorten the *r+=32,*i+=32 part I'd be grateful. -> Thanks to @Dave for helping me golf some more bytes !

etene

Posted 2018-05-08T09:56:21.140

Reputation: 448

1since i and r will be the same, you can use r=i+=32. You can also shave off a few more chars by getting rid of i, and post-incrementing s in the strchr call. – Dave – 2018-05-08T12:31:23.000

Thank you, I'll update when l can ! I thought about using s directly but that didn't work for some reason, I'll get back to it. – etene – 2018-05-08T15:28:59.190

59 bytes – ceilingcat – 2019-07-31T03:55:20.517

4

C (gcc), 63 bytes

f(x,y,z)char*x,*y,*z;{for(;*y;++y)if(z=strchr(x,*y))*z=*y+=32;}

Try it online!

Lots of credit to etene for the basic concept. Just applied heavy golfing to his answer's strategy.

LambdaBeta

Posted 2018-05-08T09:56:21.140

Reputation: 2 499

Suggest index() instead of strchr() – ceilingcat – 2018-05-08T21:59:42.257

nice improvement. I won't include it here because it is fully deprecated in POSIX and in my experience is less supported by compilers. Plus its only a 1 byte change. (PS: thanks for all the suggestions recently :) <xkcd.com/541>) – LambdaBeta – 2018-05-08T22:16:09.173

Thanks for the credit ! This kind of out-golfing is my favorite way of learning how to golf better. – etene – 2018-05-09T06:55:33.540

Same, I'd recommend checking out some of the comments made on my posts by ceilingcat as well as others recently. I love when we all learn from each other. – LambdaBeta – 2018-05-09T13:48:26.303

3

C (gcc), 64 bytes

Borrowing from @etene's answer, I removed the if with the ?: tertiary operator and reused the passenger pointer as its own index.

f(s,p,j)char*s,*p,*j;{for(;*p;p++)(j=strchr(s,*p))?*j=*p|=32:0;}

Try it online!

ErikF

Posted 2018-05-08T09:56:21.140

Reputation: 2 149

2

Jelly, 19 bytes

,ṚŒlẹЀḣ"ċЀ`}Ẏʋ¦/€

Try it online!

Erik the Outgolfer

Posted 2018-05-08T09:56:21.140

Reputation: 38 134

2

Scala, 104 bytes

def f(l:Array[Char]*)=(l(0).map(? =>{val i=l(1)indexOf?;if(i>=0){l(1)(i)= ?toLower;l(1)(i)}else?}),l(1))

Try it online!

Takes 2 seq of chars in input and returns 2 seq of chars.

Explanation:

def f(l: Array[Char]*) =          // input are varargs of Array[Char]; in our case 2 arrays. Arrays are chosen since mutable (the seatmap will be updated while the passenger list is mapped)
  (                               // we return a tuple with the 2 arrays of Chars
    l(0).map(                     // l(0) is the passenger list. We map (transform) each element of this list to lowercase or not and this is what's returned as 1st part of the tuple
      ? => {                      // ? is the current element of the passenger list being mapped (it's ? and not let's say m in order to be able to stick it next to functions)
        val i = l(1) indexOf ?   // i is the index (or -1) of the letter ? in the seat map
        if (i >= 0) {             // if index found
          l(1)(i) = ? toLower     // then we update the seatmap with the lower case version of this seat
          l(1)(i)                 // and the passenger list elmt is mapped to its lower case version (same as ?.toLower)
        }                         //
        else ?                    // if not found, the seatmap is not updated and the passenger list elmt stays in upper case
      }                           //
    ),                            // 
    l(1)                          // the updated seat map
  )

Xavier Guihot

Posted 2018-05-08T09:56:21.140

Reputation: 223

1Welcome to PPCG! – Giuseppe – 2018-05-08T21:30:24.480

1

Retina, 36 bytes

+`(([A-Z])(.*¶)+.*?)(\2.*$)
$l$1$l$4

Try it online! Assumes the customer list is the last line of the input. Explanation: Finds pairs of matching uppercase characters and lowercases them using $l thus avoiding the intermediate characters.

Neil

Posted 2018-05-08T09:56:21.140

Reputation: 95 035

1

APL (Dyalog Classic), 29 27 bytes

(~2∊/3⍴(⍴⍴,\∘,∩¨,)¨)819⌶¨¨⊢

Try it online!

ngn

Posted 2018-05-08T09:56:21.140

Reputation: 11 449

1

Perl 5 -pF, 48 bytes

$\=join'',<>;say map$\=~s/$_/lc$_/e&&lc||$_,@F}{

Try it online!

First line of input is the passenger list. All subsequent lines are the seat map. Output is the same.

Xcali

Posted 2018-05-08T09:56:21.140

Reputation: 7 671