25
\$\begingroup\$

You and some buddies are going bowling. There are a total of N bowlers. However, there are only N-1 chairs to sit in. The solution is simple: whoever's turn it currently is doesn't get a chair. Then when their turn is over, they sit in the chair of the person that goes next.

Lets take an example. Say You are named A, and your four friends are named B, C, D, and E. Every player moves in alphabetical order, so you get to go first. Since there are 5 players, there are only 4 seats. Your friends sit in the four seats in this order:

CEBD

You go, and yay you get a strike! It's B's turn next, so you sit in his chair. Now it looks like this:

CEAD

B goes. Gutterball! Then he sits in C's spot, and C goes next turn.

BEAD

then C sits in D's chair.

BEAC

and D sits in E's chair

BDAC

and lastly, E sits in your chair.

BDEC

You'll notice that now everybody's seat is (pseudo) shuffled. You must find out, after X turns, who will be sitting where?

Input

Your program must take two inputs from the user, a string and a number. No prompts are needed. The string will be 1-51 alphabetic characters (B-Z and a-z) with no repeats. This represents the order your friends chose to sit. There will be no uppercase A because that is you, and you always go first. The number will be the total number of rounds (not games) that you and your friends play. This number will be positive and reasonably sized (less than 1000).

Output

Your program must print out the order your friends are sitting in after X turns, and whose turn it is. So for example, if after X turns the order was BEDGCAHF and it was Z's turn, your program must print exactly this:

BEDGCAHF
It is Z's turn.

Here are a few sample input and outputs.

input: E, 4 
E
It is A's turn.

input: E, 5 
A
It is E's turn.

input: Bb, 2
AB
It is b's turn.

input: dgOPZXKDQYioHflFhpqzUsSaeILwckVNEtGTCJBvnruRyWMmjxb, 999
JNuvFDqjwEPVnMSlOWXgAZyGKordIRBtkamziphcUYbxfCsTQeH
It is L's turn.

Rules

  • Everybody goes in alphabetical order, with capital letters taking precedence over lower case.

  • This is code-golf, so standard loopholes apply, and submissions are scored in bytes.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I have spent hours working on this for brainflak. I just noticed that I was doing it wrong. \$\endgroup\$
    – user63187
    Jul 28, 2017 at 17:42
  • 1
    \$\begingroup\$ {({}[()])({}<({}<(([])<{{}({}<>)<>([])}{}<>>)<>>)<>{({}[()]<({}<>)<>>)}{}<>>)} \$\endgroup\$
    – user63187
    Jul 28, 2017 at 17:43
  • \$\begingroup\$ It shuffles it and the first item on stack is the answer. \$\endgroup\$
    – user63187
    Jul 28, 2017 at 17:44
  • \$\begingroup\$ That was my surprise for you :P \$\endgroup\$
    – user63187
    Jul 28, 2017 at 17:51

12 Answers 12

8
\$\begingroup\$

Pyth, 39 38 bytes

L@+\ASzbuXGyhHyHQzpyQ"It is ""'s turn.

This is based around repeated applications of the find and replace operation, X. The first bit defines a lookup function, y, which finds the bth player in the player order. Then, we repeatedly perform substitutions to find the final seating order, and finally print out whose turn it is.

Amusingly, the code to find the final seating order is shorter (18 bytes) than the code to print whose turn it is (21 bytes).

The code takes the seating string on the first line of STDIN, and the number of turns on the second.

Demonstration.

Explanation:

L@+\ASzbuXGyhHyHQzpyQ"It is ""'s turn.
                                          Implicit:
                                          z = input()
                                          Q = eval(input())

L                                         def y(b): return
  +\ASz                                    "A" + sorted(z)
 @     b                                  (               )[b]
        u       Qz                        reduce for H in range(len(Q)),
                                          G starts as z.
         XGyhHyH                          replace in G y(H+1) with y(H).
                  pyQ"It is ""'s turn.    Print out whose turn it is.
\$\endgroup\$
3
  • \$\begingroup\$ @Sp3000 Thanks for the catch. \$\endgroup\$
    – isaacg
    May 4, 2015 at 5:16
  • \$\begingroup\$ Never use %, if you only insert one thing. Even ++ would save one byte, but the best way (2 bytes) is using p: pyQ"It is ""'s turn \$\endgroup\$
    – Jakube
    May 4, 2015 at 8:08
  • \$\begingroup\$ Oops. Missed the point at the end. So ++ has the same byte-count as %, and p only saves 1 byte. \$\endgroup\$
    – Jakube
    May 4, 2015 at 8:28
7
\$\begingroup\$

CJam, 49 45 43 bytes

l_'A+$ri{:L2<(erL(+}*1<"
It is "\"'s turn."

I think this works. It just runs the algorithm as is.

Try it online.

Explanation

l                       Read line (initial seating order)
_'A+$                   Copy, add "A" and sort to give bowling order

ri{          }*         Do <number of turns> times...
   :L                     Save bowling order as L
     2<(                  Get next and current bowlers
        er                Replace next with current in seating
          L(+             Push bowling order again and update (rotate)

1<                      Get current bowler from start of bowling order
"                  
It is "\"'s turn."      Output message
\$\endgroup\$
4
\$\begingroup\$

Python 3, 110

s=input()
S=sorted(s+'A')*999
exec("y,*S=S;s=s.replace(S[0],y);"*int(input()))
print(s+"\nIt is %s's turn."%y)

An optimized version of Sp3000's solution using replace. The list S cycles though the letters present in order. We perform repeated replacements in the given string of each character of S by the previous one.

\$\endgroup\$
2
  • \$\begingroup\$ Well played, every time :) \$\endgroup\$
    – Sp3000
    May 4, 2015 at 5:54
  • \$\begingroup\$ @Sp3000 I used your solution though. \$\endgroup\$
    – xnor
    May 4, 2015 at 5:55
3
\$\begingroup\$

Pyth, 37 bytes

uXGK<.<+\ASzH2)QzpeK"It is ""'s turn.

Online Demonstration: Pyth Compiler/Executor

The algorithm is kinda based on @isaacg's solution. Like him I start with the initial seat-order and repeatedly use the replace-functionality of X to replace the next player with the current player.

But unlike his implementation, which replaces the char of the next player by the current player in the seating order, I use it in a more broad way. I replace each char of the current player by the next player and each char of the next player by the current player. This is accomplished by passing both players as second arg and omit the third arg (XG"ab") instead of XG"a""b"). Since the current player is not part of the string (he is playing), the first replacement has no effect at all. But it allows me to generate both players at the same time, while @isaacg has to generate them individually.

Another crazy new feature I use is the assignment operator. Until recently =N1 got translated into N = 1, which was executed with Python. But nowadays it compiles to assign('N',1). This function assigns N with 1 and returns the value (but doesn't print it). This allows saving intermediate results, which occur for instance in a reduce operation. Using this I was able to store the pair of players, which changed positions last, and print second player.

Detailed Explanation

                      implicit: z = input string, Q = input number
u              Qz     reduce for H in range(Q), start with G = z
                        update G with:
       +\ASz              "A" + sorted(z)
     .<     H             cyclic shifted by H
    <        2            get the first 2 elements (current + next player)
   K                      store the result in K
 XG           )           replace next player by current player in G
                      implicit print 

peK"It is ""'s turn.  print "It is" + K[-1] (current player) + "'s turn."
\$\endgroup\$
3
\$\begingroup\$

Clip 10, 59 56 bytes

[t{)k[qa)qglqtg(lqt}wx)nyN"It is "gnyt"'s turn.."`]s,x'A

Example

[t{)k[qa)qglqtg(lqt}wx)nyN"It is "gnyt"'s turn.."`]s,x'A
dgOPZXKDQYioHflFhpqzUsSaeILwckVNEtGTCJBvnruRyWMmjxb
999
JNuvFDqjwEPVnMSlOWXgAZyGKordIRBtkamziphcUYbxfCsTQeH
It is L's turn.

Explanation

The first input is the list of players, assigned to the variable x.

The second input is the number of turns, which the program obtains with ny.

[t                                        ]s,x'A .-t is the sorted list of players including A-.
  {                                      `       .-Print the following:            -.
    k[q            }wx)ny                        .-In each round (q is all the previous rounds)-.
       a)q                                       .-Replace                         -.
          glqt                                   .-the next player                 -.
              g(lqt                              .-with the previous player        -.

                     N                           .-Also print a newline            -.
                      "It is "    "'s turn.."
                              gnyt               .-The ny'th player in t           -.

Thanks to Sp3000 for the idea of using "replace".

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

Python 3, 128 bytes

L=input()
S=sorted(L)+["A"]
i=0
exec("L=L.replace(S[i],S[i-1]);i=-~i%len(S);"*int(input()))
print(L+"\nIt is %s's turn."%S[i-1])

Takes two lines of input via STDIN — initial seating order then number of turns.

This is basically the same search-and-replace idea as my CJam solution. The only tricky part is that we stick A at the back of the bowling order and make our index i the index of the next bowler, thus taking advantage of indexing by -1 and avoiding IndexErrors.

This is a few bytes shorter in Python 2, but I'm posting Python 3 for comparison with OP's solution.

\$\endgroup\$
2
  • \$\begingroup\$ What improvements does python 2 have? I see that input on line 1 becomes raw_input (+4) and int(input()) on line 4 becomes input (-4) so that cancels out. Then remove parentheses from print and add a space for a total of 127. Am I missing something? \$\endgroup\$
    – DJMcMayhem
    May 5, 2015 at 0:07
  • \$\begingroup\$ @DJMcMayhem You forgot the parentheses for exec \$\endgroup\$
    – Sp3000
    May 5, 2015 at 3:20
2
\$\begingroup\$

JavaScript (ES6) 116

116 bytes as program with I/O via popup window. 114 as a testable function.

Run code snippet in Firefox to test.

// as a program with I/O
for(t=[...s=(P=prompt)()].sort(),x=P(),p='A';x--;p=n)t.push(p),s=s.replace(n=t.shift(),p);P(`${s}
It is ${p}' turn`)

// as a function with 2 parameters, returning output as a 2 lines string
f=(s,x)=>{for(t=[...s].sort(),p='A';x--;p=n)t.push(p),s=s.replace(n=t.shift(),p);return(`${s}
It is ${p}' turn`)}

// Test suite
test=[
['CEBD',5], ['E', 4],['E',5],['Bb', 2],
['dgOPZXKDQYioHflFhpqzUsSaeILwckVNEtGTCJBvnruRyWMmjxb', 999]];

Out=x=>OUT.innerHTML=OUT.innerHTML+x;

test.forEach(([p,x])=>Out(p+' '+x+':\n'+f(p,x)+'\n'))
Test cases from OP:
<pre id=OUT></pre>

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

PowerShell, 168 bytes

function x($s,$t){$p="A"+$s-split''|?{$_}|%{[int][char]$_}|sort|%{[char]$_};$l=$p.count;1..$t|%{$s=$s.replace($p[$_%$l],$p[($_-1)%$l])};$s;"It is $($p[$t%$l])'s turn."}

I've decided all my answers on this site will be in PowerShell. One day I'll have an answer that can compete...

call the function like this: x Bb 2

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

This answer isn't going to win, but I'll throw it out there anyway.

Python 3, 167 bytes

s=input()
n=int(input())
c='A'
t=sorted(set(s+c))
F=len(t)
f=list(s)
for i in range(n):
 I=f.index(t[(i+1)%F]);c,f[I]=f[I],c
print(''.join(f)+'\nIt is '+c+"'s turn.")
\$\endgroup\$
1
\$\begingroup\$

Pip, 54 bytes

Not very competitive, but at least I get to show off Pip's mutable strings and the Swap command. Takes the seating order and number of rounds as command-line arguments (which get assigned to a and b, respectively).

u:'Ao:SN A^u.aLbSu(aa@?C(o++i))Pa"It is ".u."'s turn."

Explanation:

u:'A                   u = player who's currently up

o:SN A^u.a
      ^u.a             Append "A" to the seating order, split into list of characters
o:SN A                 ASCII value of each char, sort the resulting list, assign to o

LbSu(aa@?C(o++i))
Lb                     Repeat b times:
  Su                   Swap u with:
    (a          )      The character of a at index:
      a@?              Find in a:
         C(o++i)       chr(ASCII value of next player from o)
                       (Subscripts wrap around, as in CJam, so no need for mod)

Pa                     Print the final lineup
"It is ".u."'s turn."  Current player (auto-printed)

It would have been 49 if I'd bothered to implement SS (sort as strings) at the same time I did SN (sort numeric)... Ah well, the perils of having a language in development.

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

Python 2, 105 bytes

a,s='A',input()
exec"b=a<max(s)and min(e for e in s if e>a)or'A';s=s.replace(b,a);a=b;"*input()
print s,b

Try it online!

Golf of:

s=input()
n=input()
a='A'
for i in range(n):
 try:
  b=min(e for e in s if e>a)
 except:
  b='A'
 s=s.replace(b,a)
 a=b
print s
print b
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 102 + 1 (-n) = 103 bytes

s/\d+//;$t=$&;@p=sort'A',split//;for$i(1..$t){s/$p[$i%@p]/$p[--$i%@p]/}say"$_
It is $p[$t%@p]'s turn."

Try it online!

Input

Seating order, followed by number of turns without spaces:

SEatingOrder###
\$\endgroup\$

Your Answer

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

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