Solve problem 265
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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
|
||||
Reference in New Issue
Block a user