Last active
March 14, 2023 09:54
-
-
Save AlephAlpha/4ace9885d687c25efe03b238630721f9 to your computer and use it in GitHub Desktop.
soup search script for https://codegolf.stackexchange.com/questions/251510/live-a-longer-life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install python-lifelib | |
from math import log2 | |
from lifelib.pythlib.pattern import Pattern | |
import lifelib | |
import sys | |
def get_gen(pat: Pattern, maxexp: int) -> int | None: | |
x = pat | |
gen = 0 | |
for exponent in range(8, maxexp + 2, 2): | |
y = x.pdetect_or_advance(exponent) | |
if isinstance(y, dict): | |
if y["displacement"] != (0, 0): | |
return None | |
exp = exponent | |
p = int(y["period"]) | |
break | |
x = y | |
gen += 1 << exponent | |
else: | |
return None | |
while exp >= 0: | |
if gen >= 1 and pat[gen - 1] == pat[gen + p - 1]: | |
gen -= 1 << exp | |
gen = max(gen, 0) | |
elif pat[gen] != pat[gen + p]: | |
gen += 1 << exp | |
else: | |
break | |
exp -= 1 | |
return gen | |
def randomize(pat: Pattern, size: int, density: float): | |
# pat[0:size, 0:size] = density | |
half = size // 2 + size % 2 | |
pat ^= pat | |
pat[0:half, 0:half] = density | |
pat += pat("rot90", size % 2 - 1, 0) | |
pat += pat("rot180", 0, size % 2 - 1) | |
def lall(size: int): | |
lifetree = lifelib.load_rules("b3s23").lifetree(memory=4096) | |
pat = lifetree.pattern() | |
soup = 0 | |
mingen = 1 | |
while True: | |
if soup % 10000 == 0: | |
print("Soup", soup) | |
soup += 1 | |
randomize(pat, size, 0.3) | |
x = pat[mingen] | |
if isinstance(x.pdetect_or_advance(8), dict): | |
continue | |
gen = get_gen(x, max(int(log2(mingen)) + 2, 10)) | |
if gen is not None: | |
print("Soup:", soup, "Gen:", mingen + gen) | |
print(pat.rle_string()) | |
mingen += gen | |
if __name__ == "__main__": | |
lall(int(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment