Last active
November 19, 2019 03:02
-
-
Save apaap/6d613f9fa0ef2427b6812832fee1a641 to your computer and use it in GitHub Desktop.
Find the smallest ship at each period in the Omosso p392_pairs census
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
# Before using this code the textcensus needs to be fetched from Catagolue. | |
# $curl -o stdin_c98_pairs.txt https://catagolue.appspot.com/textcensus/b2k3acijr4ijqy6i7cs2aek3ijnqr4it5n/stdin_c98_pairs | |
# Format: "{apgcode}", {count} | |
import lifelib | |
import sys | |
omosso = "b2k3acijr4ijqy6i7cs2aek3ijnqr4it5n" | |
lt = lifelib.load_rules(omosso).lifetree(memory=4000) | |
def get_minima(ship, period): | |
minpop = ship.population | |
bbox = ship.bounding_box | |
minbbox = bbox[2] * bbox[3] | |
for _ in range(period): | |
ship = ship[1] | |
pop = ship.population | |
if pop < minpop: | |
minpop = pop | |
bbox = ship.bounding_box | |
minbbox = bbox[2] * bbox[3] | |
elif pop == minpop: | |
bbox = ship.bounding_box | |
bboxarea = bbox[2] * bbox[3] | |
minbbox = min(minbbox, bboxarea) | |
return minpop, minbbox | |
def maxminofabs(v): | |
v = [abs(x) for x in v] | |
return max(v), min(v) | |
# Process all c/98 ships in Omosso census | |
shipMinima = {} | |
minShips = {} | |
N = 0 | |
with open("stdin_c98_pairs.txt") as Fin: | |
for line in Fin.readlines(): | |
N += 1 | |
shipstr = line.split(",")[0].strip('"') | |
if not shipstr.startswith("xq"): | |
continue | |
period = int(shipstr.split("_")[0][2:]) | |
if period in [12, 16, 100, 98, 196, 294, 392, 490, 588, 686, 784, 882, 980]: | |
# Ignore periods already known to 5S | |
continue | |
ship = lt.pattern(shipstr) | |
speed = maxminofabs(ship.displacement) + (period,) | |
minima = get_minima(ship, period) | |
try: | |
if minima < shipMinima[speed]: | |
shipMinima[speed] = minima | |
minShips[speed] = shipstr | |
except KeyError: | |
shipMinima[speed] = minima | |
minShips[speed] = shipstr | |
if (N % 100) == 0: | |
print('.', end='') | |
sys.stdout.flush() | |
print() | |
# Write minimum ships to file | |
with open("omosso_ships.txt", "w") as Fout: | |
for speed, shipstr in sorted(minShips.items()): | |
minima = shipMinima[speed] | |
shiprle = lt.pattern(shipstr).rle_string() | |
print(', '.join(map(str, minima[0:1] + speed))) | |
print(shiprle, file=Fout) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment