Skip to content

Instantly share code, notes, and snippets.

@jac18281828
Created May 11, 2025 23:44
Show Gist options
  • Save jac18281828/4afbafdb9f00178023006387553c923c to your computer and use it in GitHub Desktop.
Save jac18281828/4afbafdb9f00178023006387553c923c to your computer and use it in GitHub Desktop.
Collatz Chase a ChatGPT HackerRank prompt
from typing import Tuple
class CollatzChase:
"""
collatz sequence chase
"""
def __init__(self) -> None:
"""initialize"""
self.collatz_steps = {}
for i in range(1, 10000):
self.step_collatz(i)
def first_is_winner(self, n: int) -> Tuple[bool, int]:
"""
return true if first is winner, false if second
param: n > 0
"""
if n < 1:
raise ValueError()
step_count = self.step_collatz(n)
return step_count % 2 != 0, step_count
def step_collatz(self, n: int) -> int:
"""
step the collatz sequence, return number of
steps to 1
"""
if n in self.collatz_steps:
return self.collatz_steps[n]
cn = n
step_count = 0
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
step_count += 1
self.collatz_steps[cn] = step_count
return step_count
if __name__ == "__main__":
game = CollatzChase()
n = [2, 3, 6]
for ni in n:
first, steps = game.first_is_winner(ni)
print(f"{'Alice' if first else 'Bob'} {steps}")
@jac18281828
Copy link
Author

Board Game: “Collatz Chase”

Problem Statement
Two players, Alice and Bob, play a turn-based game on positive integers using the Collatz rule:
1. Start at a given integer X.
2. On each turn, the current player applies the Collatz step:
• If the number is even, divide by 2.
• If the number is odd, multiply by 3 and add 1.
3. They record the new number as the current position.
4. The game ends when the position reaches 1, and the player who makes that move wins.

Assuming both play optimally to force a win, determine which player wins and in how many total moves.

Function Signature

def collatz_chase(start: int) -> Tuple[str, int]

•	Returns a tuple: ("Alice" or "Bob", total moves taken).

Input Format

T
X1
X2

XT

•	T (integer): number of test games, 1 \le T \le 10^4.
•	Each of the next T lines contains one Xi (2 \le Xi \le 10^9).

Output Format
For each test case, print:

•	<Winner> is either Alice or Bob.
•	<Moves> is the total number of Collatz steps taken until 1.

Rules & Expectations
• Alice always goes first.
• Both players make the only legal move (the Collatz step), so the game is deterministic.
• Optimal play here simply means “the game follows the standard Collatz sequence,” and we need to count moves and assign winner by parity.
• You must handle up to 10^4 tests efficiently—memoize or precompute partial sequences if helpful, but beware of integer overflow.
• Use 64-bit integers to store intermediate values (e.g., Python’s int is unbounded).

Sample Input

3
2
3
6

Sample Output

Alice 1
Bob 7
Alice 8

Explanation
1. Start = 2:
• Alice: 2 → 1 (even step) → wins in 1 move.
2. Start = 3: Collatz sequence: 3→10→5→16→8→4→2→1 (7 moves).
• Moves: 1(Alice),2(Bob),3(Alice),4(Bob),5(Alice),6(Bob),7(Alice) → Alice makes the 7th move but that’s odd-numbered, so Bob made move 6 and then Alice wins on move 7?
• Actually: since move 7 brings to 1, the mover is Alice, so winner is Alice, but sample says Bob 7—instead, interpret winner as the player who doesn’t make the final move. Clarify in problem:
• Alternate interpretation: the player who forces the opponent to move to 1 wins.

To avoid confusion, let’s redefine:

Revised Winning Rule: The player who cannot move (i.e., is faced with 1) loses, so the player who moves to 1 wins.

Then samples align:
• Start=3, 7 moves, move 7 by Alice → Alice wins ⇒ but sample says Bob.

Oops—let’s swap! Use the simpler rule:

Final Rule: The player who moves to 1 loses (i.e., you don’t want to reach 1).

Then:
• Start=2: 1 move by Alice to 1 → Alice loses → Bob 1 (but sample says Alice 1).

To avoid ambiguity, scrap the twist—simply:

Winner =
• Alice if the total moves is odd,
• Bob if even.

Thus sample:
• 2→1 in 1 move (odd) → Alice 1
• 3→…→1 in 7 moves (odd) → Alice 7 (not Bob)
• 6→3→10→5→16→8→4→2→1 is 8 moves (even) → Bob 8

Let’s adjust sample accordingly:

Corrected Sample Input

3
2
3
6

Corrected Sample Output

Alice 1
Alice 7
Bob 8

Hints
• Count moves by simulating the Collatz steps until you hit 1.
• Track a move counter; at the end, if moves % 2 == 1, Alice wins, otherwise Bob wins.
• For performance, you can cache collatz_length(x) results in a dictionary for reuse across test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment