16
\$\begingroup\$

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

10 Answers 10

5
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The seatmap can be input as a single string with line breaks, wouldn't that save you a byte or two? \$\endgroup\$
    – Etheryte
    May 8, 2018 at 13:38
  • \$\begingroup\$ @Nit: Ah, It should indeed save me some. Thanks :) \$\endgroup\$
    – Emigna
    May 8, 2018 at 14:49
5
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ s,p=[x.replace(c,c.lower(),c in s)for x in[s,p]] for 84 bytes \$\endgroup\$ May 8, 2018 at 10:27
  • 1
    \$\begingroup\$ A 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 \$\endgroup\$
    – Lynn
    May 8, 2018 at 20:30
5
\$\begingroup\$

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 !

\$\endgroup\$
3
  • 1
    \$\begingroup\$ since 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. \$\endgroup\$
    – Dave
    May 8, 2018 at 12:31
  • \$\begingroup\$ 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. \$\endgroup\$
    – etene
    May 8, 2018 at 15:28
  • \$\begingroup\$ 59 bytes \$\endgroup\$
    – ceilingcat
    Jul 31, 2019 at 3:55
4
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Suggest index() instead of strchr() \$\endgroup\$
    – ceilingcat
    May 8, 2018 at 21:59
  • \$\begingroup\$ 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>) \$\endgroup\$
    – LambdaBeta
    May 8, 2018 at 22:16
  • \$\begingroup\$ Thanks for the credit ! This kind of out-golfing is my favorite way of learning how to golf better. \$\endgroup\$
    – etene
    May 9, 2018 at 6:55
  • \$\begingroup\$ 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. \$\endgroup\$
    – LambdaBeta
    May 9, 2018 at 13:48
3
\$\begingroup\$

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!

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

Jelly, 19 bytes

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

Try it online!

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

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
  )
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Giuseppe
    May 8, 2018 at 21:30
1
\$\begingroup\$

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.

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

APL (Dyalog Classic), 29 27 bytes

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

Try it online!

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

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.

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