Counter game

  • + 0 comments

    This Python 3 solution has some overhead, but I think it's reasonably readable:

    import itertools
    import typing
    
    
    def counterGame(n: int) -> typing.Literal["Louise", "Richard"]:
        other_player = {"Louise": "Richard", "Richard": "Louise"}
        for player in itertools.cycle(("Louise", "Richard")):
            if n == 1:
                return other_player[player]
            power = n.bit_length() - 1
            if f"{n:b}".endswith(zeros := power * "0", 1):
                return player if power % 2 else other_player[player]
            n -= int(f"1{zeros}", 2)