Solve problem 961

This commit is contained in:
2026-06-17 14:39:33 -04:00
parent fd0cecbbbe
commit 5c9e9d775f
2 changed files with 51 additions and 1 deletions
+50
View File
@@ -0,0 +1,50 @@
from functools import cache
@cache
def remove(n: str, pos: int) -> str:
assert pos < len(n)
r = n[:pos] + n[pos + 1:]
r = r.lstrip("0")
return r
@cache
def player_wins(n: str) -> bool:
if len(n) == 1:
return True
for i in range(len(n)):
if not player_wins(remove(n, i)):
return True
return False
def w(n: int) -> int:
r = 0
xs = [("", 0)]
for _ in range(n):
nxs = []
for x, x_count in xs:
nxs.append(("0" + x, x_count))
nx, nx_count = "x" + x, x_count + 1
if player_wins(nx):
r += (9 ** nx_count)
nxs.append((nx, nx_count))
xs = nxs
return r
def euler_961():
assert remove("105", 0) == "5"
assert remove("105", 1) == "15"
assert remove("105", 2) == "10"
assert remove("10005", 0) == "5"
assert w(2) == 18
assert w(4) == 1656
return w(18)
if __name__ == "__main__":
solution = euler_961()
print("e961.py: " + str(solution))
# assert(solution == 0)
+1 -1
View File
@@ -7,7 +7,7 @@ def euler_XXX():
if __name__ == "__main__":
solution = euler_XXX()
print("eXXX.py: " + str(solution))
print(f"eXXX.py: {solution}")
# assert(solution == 0)
"""