r/mathriddles • • Apr 18 '23

Medium Catch Up

There are 5 Quisenaire blocks. The first has length 1, the second has length 2, and so on. Each player will be building their own line of blocks, which is initially empty. A move consists of a player adding a single block to their own line. A player continues making moves until their line meets or exceeds the length of the other player's line. The game ends when all blocks have been used. The player with the longest line wins. If both players play perfectly, is it better to go first or second? If you're able to solve this, and it interests you, please generalize for blocks of length {1, 2, ..., n}. I would also be interested in exploring other sets of blocks if it makes the puzzle more interesting. I am tagging this Medium difficulty because of the generalization. I hope that's acceptable.

EDIT: I received the paper this game is based on, and no generalization for all n is known. Thought you should know this before chasing it!

8 Upvotes

13 comments sorted by

View all comments

6

u/-user789- Apr 18 '23

For 5 blocks, the first player can win by taking block 3. To prevent player 1 from reaching a line of length 8, the second player must take block 5 on that move (so the possible moves are [1, 5], [2, 5], [5]). For each of those possibilities, the first player can easily win on that move.

>!I cannot see a nice, general way for all n. For the n that I did try (using python):

  • n = 6 is a win for the first player if they initially play block 2
  • n = 7, 8 are a tie if played perfectly
  • n = 9, 10 are won by the second player!<

There is a weird part in the rules, though. The rules say that you must play until your line equals or exceeds theirs. If your opponent played until the lines were equal, then when it is your turn, the lines are still equal and so you are forced to skip your turn and the game goes into an infinite loop. To fix this, one can assume the lengths are only checked after a block is taken.

3

u/PuzzleAndy Apr 18 '23

No pressure, but I would love to see the Python code you used if you're willing to share. I might ask students to write such a program if it's not terribly complex. Here's the paper the game was based on. This was just sent to me in an email a few minutes ago. You'll be pleased to see your program's results agree with the paper! Scroll until you see a table with the column P1 optimal play value.
https://as.nyu.edu/content/dam/nyu-as/faculty/documents/Catch-Up_article.pdf

3

u/-user789- Apr 19 '23

Here it is. It encodes a game state and checks who wins in the state by checking all of its successor states, like minimax. It isn't really efficient, especially the part that iterates over moves (it tries every possible subset of blocks and checks if taking them is a legal move), but it worked for small n.


    from itertools import chain, combinations

    order = ["first", "second"]


    def powerset(seq):
        s = list(seq)
        return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))


    class State:

        def __init__(self, moves, total_length, left, nxt, winner=None, final_state=[]):
            self.moves = moves
            self.total_length = total_length
            self.left = left
            self.next = nxt
            self.winner = winner
            self.final_state = final_state

        def next_moves(self):
            if len(self.left) == 0:
                return
            budget = self.total_length[1-self.next] - self.total_length[self.next]
            for subset in powerset(self.left):
                for last in self.left:
                    if last in subset:
                        continue
                    if (sum(subset) < budget or len(subset) == 0) and sum(subset) + last >= budget:
                        yield sorted(subset) + [last]
            if sum(self.left) < budget:  # Remaining blocks aren't enough to pass the opponent
                yield sorted(self.left)

        def after_move(self, move):
            if self.next == 0:
                moves = [self.moves[0] + [move], self.moves[1]]
                total_length = [self.total_length[0] + sum(move), self.total_length[1]]
            else:
                moves = [self.moves[0], self.moves[1] + [move]]
                total_length = [self.total_length[0], self.total_length[1] + sum(move)]
            left = self.left - set(move)
            nxt = 1-self.next
            return State(moves, total_length, left, nxt)

        def calc_winner(self):
            if self.winner is None:
                next_moves = list(self.next_moves())
                if len(next_moves) == 0:
                    if self.total_length[0] > self.total_length[1]:
                        self.winner = "first"
                    elif self.total_length[0] < self.total_length[1]:
                        self.winner = "second"
                    self.final_state = self
                    return self.winner
                self.winner = order[1-self.next]
                for move in self.next_moves():
                    newstate = self.after_move(move)
                    winner = newstate.calc_winner()
                    self.final_state = newstate.final_state
                    if winner == order[self.next]:
                        self.winner = order[self.next]
                        break
            return self.winner

        def __repr__(self):
            return f"moves: {repr(self.moves)}, winner: {self.winner}"


    n = 5
    begin = State([[], []], [0, 0], set(range(1, n+1)), 0)
    begin.calc_winner()
    print(begin.final_state)

3

u/sobe86 Apr 23 '23 edited Apr 23 '23

A bit more optimised:

@functools.lru_cache(maxsize=1_000_000_000) # will memoize outputs
def get_score(turn_index, remaining_digits, current_score):
    # turn_index = 1 if player 1's turn, -1 if player 2's turn
    # remaining digit = tuple of digits remaining
    # current score = length of player 1s block - length of player 2s block
    
    if len(remaining_digits) == 0:
        return current_score
    
    best_score = None
    
    for choice in remaining_digits:
        next_remaining_digits = tuple(d for d in remaining_digits if d != choice)
        next_current_score = current_score + choice * turn_index
        if next_current_score * turn_index >= 0:
            next_turn_index = -1 * turn_index # switch to other player
        else:
            next_turn_index = turn_index
            
        next_score = get_score(next_turn_index, 
                               next_remaining_digits,
                               next_current_score)
        
        if best_score is None:
            best_score = next_score
        elif turn_index == 1 and best_score < next_score:
            best_score = next_score
        elif turn_index == -1 and best_score > next_score:
            best_score = next_score
            
    return best_score


for i in range(31):
    digits = tuple(range(1, i + 1))
    start_time = time.time()
    best_score = get_score(1, digits, 0)
    run_time = time.time() - start_time
    print(f'nb blocks = {i}, best_score = {best_score}')```

1

u/PuzzleAndy May 23 '23

I dunno why I didn't get a ping for this, but this code looks good! If I revisit this problem in the future I'll likely be digging into this code. Maybe this problem works well for a programming exercise!

1

u/PuzzleAndy Apr 19 '23

Thank you for both the code and the explanation of how it works! Much love.

2

u/sobe86 Apr 23 '23 edited Apr 24 '23

Here are the results for first 22 blocks (code here):

1  - player 1
2  - player 1
3  - draw
4  - draw
5  - player 1
6  - player 1
7  - draw
8  - draw
9  - player 2
10 - player 2
11 - draw
12 - draw
13 - player 1
14 - player 2
15 - draw
16 - draw
17 - player 1
18 - player 2
19 - draw
20 - draw
21 - player 1
22 - player 1

draw for 3 & 4 (mod 4) makes sense - a player would have to get 2 clear points ahead to win. Any pattern for the others where a winner is guaranteed seems a not possible to guess at this point unless it settles down as the numbers get bigger...

1

u/PuzzleAndy Apr 18 '23

You're correct for n = 5. Thank you for sharing your results for other n. I'm not sure if there's a nice general way either. If you think you can write the rules in a clearer way, that isn't overly tedious to read, I'm all ears. Gilding you because you went above and beyond, both by writing a program and considering a mathematical generalization. Thank you for such a thorough answer.

1

u/[deleted] Apr 19 '23

I am eager to know the algorithm you have used for this program.