Alice's Tea Party

7

There are n places set around a circular table. Alice is sat on one of them. At each place, there's a cake. Alice eats her cake, but it doesn't taste very nice. Then the Mad Hatter comes in. He gives Alice a list of n-1 numbers, each between 1 and n-1. For each item i in the list, Alice has to move around i places and eat the cake there if there is one. However, Alice will be able to choose whether to move clockwise or anticlockwise for each item in the list. She doesn't like the cakes, so she is trying not to eat them.

Given an integer n, output all the possible lists the Mad Hatter could use to force Alice to eat all the cakes (represented as a tuple/list of tuples/lists of integers or some tuples/lists of integers separated by newlines). Note that it is possible for the Mad Hatter to force Alice to eat all the cakes if and only if n is a power of two, so you may assume that n is a power of two. Make your code as short as possible.

You may also take in x such that 2^x=n if that helps. For example, you may take in 2 instead of 4, 3 instead of 8, etc.

The order in which your program returns the possible lists does not matter.

Test Cases

1 -> [[]]
2 -> [[1]]
4 -> [[2, 1, 2], [2, 3, 2]]
8 -> [[4, 2, 4, 1, 4, 2, 4], [4, 2, 4, 1, 4, 6, 4], [4, 6, 4, 1, 4, 2, 4], [4, 6, 4, 1, 4, 6, 4], [4, 2, 4, 3, 4, 2, 4], [4, 2, 4, 5, 4, 2, 4], [4, 2, 4, 7, 4, 2, 4], [4, 2, 4, 3, 4, 6, 4], [4, 6, 4, 3, 4, 2, 4], [4, 2, 4, 5, 4, 6, 4], [4, 6, 4, 5, 4, 2, 4], [4, 2, 4, 7, 4, 6, 4], [4, 6, 4, 7, 4, 2, 4], [4, 6, 4, 3, 4, 6, 4], [4, 6, 4, 5, 4, 6, 4], [4, 6, 4, 7, 4, 6, 4]]

0WJYxW9FMN

Posted 2018-02-04T13:34:14.120

Reputation: 2 663

Related. – user202729 – 2018-02-04T14:13:53.833

Can we return [] instead of [[]] for n = 1? – Οurous – 2018-02-05T05:57:34.310

Can we do this in c#?If yes,can someone explain – pradeep reddy – 2018-02-05T11:45:57.427

@Ourous No. The answer should be [[]] because the Mad Hatter's list has length 0. This means that [] is the only possible list (which works since Alice eats the only cake she has before the Mad Hatter comes in), so [[]] is the list of all lists that work. – 0WJYxW9FMN – 2018-02-05T18:38:04.450

Answers

2

Python 3, 235 191 177 136 134 122 119 bytes

lambda n:[*product(*[[*(i==n/2and range(1,n,2)or[[n/4,3*n/4],[n/2]][i%2])]for i in range(1,n)])]
from itertools import*

Try It Online

Thanks To Dennis for -31 bytes and ChasBrown for -14 bytes and Mr.XCoder for -1 byte.

Numbers appear in the decimal form, eg:4.0.

Manish Kundu

Posted 2018-02-04T13:34:14.120

Reputation: 1 947

239 bytes (switches to Python 2) – caird coinheringaahing – 2018-02-04T19:30:00.460

@cairdcoinheringaahing ty. Added that. – Manish Kundu – 2018-02-04T19:43:13.323

Edited and fixed some error – Manish Kundu – 2018-02-04T20:23:17.610

219 bytes – caird coinheringaahing – 2018-02-04T20:25:02.437

You could do from itertools import* and product(*k) (without the itertools. bit on the beginning). In fact, you could do print([*product(*k)]) instead of print([p for p in product(*k)]). – 0WJYxW9FMN – 2018-02-04T20:25:59.980

1This (133 bytes) should be equivalent. – Dennis – 2018-02-04T20:36:17.763

1

Using your same logic, but with a lambda, 120 bytes.

– Chas Brown – 2018-02-05T06:48:36.860

@ChasBrown Thanks, but as per the rules I cannot keep the x=\ in the header, so it would make it 122 bytes. – Manish Kundu – 2018-02-05T07:02:15.583

@Mainsh Kandu: That constraint only applies when the function is recursive; which yours is not - so you can keep those precious 2 bytes :). (Contrast with my solution below, where I cannot put f=\ in the header). – Chas Brown – 2018-02-05T07:07:07.550

121 bytes. list(...) -> [*(...)] – Mr. Xcoder – 2018-02-05T07:36:33.997

2

Perl, 78 75 72 bytes

Includes +1 for n

perl -nE '$"=",";say<@{[map"{@{[map$`*($_-.5)/@z,@z=1..$_&-$_]}}",/$/..$_-1]}\\ >' <<< 8

Prints a space separated sequence of comma separated numbers (replace the final space by \n for a more readable output)

Uses a string substitution inside a string substitution.

Ton Hospel

Posted 2018-02-04T13:34:14.120

Reputation: 14 114

1

Jelly, 18 bytes

żN$Œp+\€%ṬȦ
’ṗ’çÐf

This is a brute-force solution that tests all (n - 1)n - 1 potential solutions one by one.

Try it online!

Verification

$ time jelly eun 'żN$Œp+\€%ṬȦ¶’ṗ’çÐf' 8
[[4, 2, 4, 1, 4, 2, 4], [4, 2, 4, 1, 4, 6, 4], [4, 2, 4, 3, 4, 2, 4], [4, 2, 4, 3, 4, 6, 4], [4, 2, 4, 5, 4, 2, 4], [4, 2, 4, 5, 4, 6, 4], [4, 2, 4, 7, 4, 2, 4], [4, 2, 4, 7, 4, 6, 4], [4, 6, 4, 1, 4, 2, 4], [4, 6, 4, 1, 4, 6, 4], [4, 6, 4, 3, 4, 2, 4], [4, 6, 4, 3, 4, 6, 4], [4, 6, 4, 5, 4, 2, 4], [4, 6, 4, 5, 4, 6, 4], [4, 6, 4, 7, 4, 2, 4], [4, 6, 4, 7, 4, 6, 4]]

real    85m33.780s
user    85m35.903s
sys     0m0.060s

How it works

’ṗ’çÐf       Main link. Argument: n (integer)

’ ’          Yield n-1.
 ṗ           Cartesian power; yield all vector of length n-1 over [1, ..., n-1].
   çÐf       Filter; keep vectors v for which the helper link, called with left
             argument v and right argument n, returns a truthy value.


żN$Œp+\€%ṬȦ  Helper link. Left argument: v (vector). Right argument: n (integer)

żN$          Zip v with -v, mapping [v(1), ... v(n-1)] to
             [[v(1), -v(1)], ..., [v(n-1), -v(n-1)]].
   Œp        Take the Cartesian product of all pairs.
     +\€     Take the cumulative sum of each resulting vector.
        %    Take all cumulative sums modulo n.
             A valid solution results in a permutation of [1, ..., n-1].
         Ṭ   Untruth; map each resulting vector to a Boolean array with 1's at
             the specified indices, 0's elsewhere.
          Ȧ  Test if the resulting array contains only non-zero integers.

Dennis

Posted 2018-02-04T13:34:14.120

Reputation: 196 637

1

Python 2,130 129 127 123 121 bytes

f=lambda n:n>1and(lambda D:[l+[k]+r for l in D for r in D for k in range(1,n,2)])([[i*2for i in a]for a in f(n/2)])or[[]]

Try it online!

Edited: Takes as input n (which must be a power of 2) as the number of seats at the Hatter's table.

Chas Brown

Posted 2018-02-04T13:34:14.120

Reputation: 8 959

Here the input 'n' refers to 2^n ? Please mention that. – Manish Kundu – 2018-02-05T05:17:45.067

@Manish Kundu: quite right. – Chas Brown – 2018-02-05T05:24:24.273

1

Python 2, 81 bytes

f=lambda n,i=1,*l:i/n*[l]or sum([f(n,i+1,x,*l)for x in range(n)if x*i%n==n/2],[])

Try it online!


Python 2, 92 bytes

f=lambda n,c=1:[x+[c*y]+z for y in range(1,n,2)for x in f(n/2,c*2)for z in f(n/2,c*2)]or[[]]

Try it online!

xnor

Posted 2018-02-04T13:34:14.120

Reputation: 115 687

0

Clean, 194 ... 172 bytes

Still in the progress of cutting more off.

import StdEnv

\n=let?_[]_=1>0;?l[i:j]m=all((\e= ?(updateAt e(1<0)l)j e&&l!!e)o\e=e-e/n*n)[m+i,n+m-i]in[k\\k<-iter(n-1)(\e=[[h:t]\\h<-[1..n],t<-e])[[]]| ?[q>1\\q<-[1..n]]k 0]

Try it online!

Lambda function which does a brute-force over every combination of indices up to length n-1.
It still runs in about 5 seconds, because of compiler magic.

Οurous

Posted 2018-02-04T13:34:14.120

Reputation: 7 916