Files
euler/python/e265.py
T
2026-08-09 14:07:58 -04:00

48 lines
1.1 KiB
Python

def is_valid(circle_with_seen, n):
circle, seen = circle_with_seen
if len(circle) < n:
return True
end = tuple(circle[-n:])
if end in seen:
return False
seen.add(end)
return True
def overlap_valid(circle_with_seen, n):
circle, seen = circle_with_seen
for i in range(1, n):
end = tuple(circle[-i:] + circle[: n - i])
if end in seen:
return False
seen.add(end)
return True
def s(n):
length = 2**n
start = [0 for _ in range(n)]
xs = [(start, set([tuple(start)]))]
for _ in range(length - n):
nxs = []
for circle, seen in xs:
for d in [0, 1]:
nx = (circle + [d], seen.copy())
if is_valid(nx, n):
nxs.append(nx)
xs = nxs
circles = [x[0] for x in xs if overlap_valid(x, n)]
return sum(int("".join(map(str, circle)), 2) for circle in circles)
def euler_265():
assert s(3) == 52
return s(5)
if __name__ == "__main__":
solution = euler_265()
print(f"e265.py: {solution}")
assert solution == 209110240768