User:Hibiscus/shipsearch

From LifeWiki
Jump to navigation Jump to search

import subprocess import re import random import math numships=5 foundships=0 commonspeeds=[(2,1,0),(3,1,0),(3,1,1),(4,1,0),(4,1,1),(5,1,0),(5,2,0),(5,1,1),(6,1,0),(6,1,1)] with open('new-gliders.db.txt', 'r') as file:

   shipslist = [line.strip() for line in file]

def parse(r):

   rb=list(r.split("/")[0])
   rs=list(r.split("/")[1])
   rb.remove("B")
   rs.remove("S")
   return set(int(x) for x in rb), set(int(x) for x in rs)

def inrange(r1,r2,r3):

   possible=list(range(9))    
   rb1, rs1 = parse(r1)
   rb2, rs2 = parse(r2)
   rb3, rs3 = parse(r3)
   reqb,reqs=list(set(rb1)&set(rb2)),list(set(rs1)&set(rs2))
   forb=list(set(possible) - rb1 - rb2)
   fors=list(set(possible) - rs1 - rs2)
   if set(rb3) & set(forb) or set(rs3) & set(fors):
       return False
   if set(reqb).issubset(rb3) and set(reqs).issubset(rs3):
       return True
   else:
       return False

def canonize(period, dx, dy):

   period2=int(period.replace("/2",""))
   dx2, dy2=abs(int(dx)),abs(int(dy))
   gcdr = math.gcd(period2, dx2, dy2)
   period2, dx2, dy2 = period2 // gcdr, dx2 // gcdr, dy2 // gcdr
   if dx2>=dy2:
       return period2, dx2, dy2
   else:
       return period2, dy2, dx2

def getships(r):

   chosen = r
   shipperiods=[]
   for line in shipslist:
       parts = line.split(':')
       minrule = parts[2]
       maxrule = parts[3]
       period = parts[4]
       dx = parts[5]
       dy = parts[6]
       if inrange(minrule, maxrule, chosen):
           addtolist=canonize(period,dx,dy)
           if not addtolist in shipperiods:
               shipperiods.append(addtolist)
   return shipperiods

def findsmallestunknown(r):

   periods=getships(r)
   for k in commonspeeds:
       if k not in periods:
           return k
   return None

def findship(rule, speed, width, symmetry):

   global foundships
   found=0
   if (speed[1] != speed[2]) and (speed[1] == 0 or speed[2] == 0):
       # print("Searching for " + str(speed[1]) + "c/" + str(speed[0]) + " in " + rule + ", width " + width + (" even " if symmetry=="e" else " odd ") + "symmetry.")
       process=subprocess.run(["./qfind", "-r", rule, "-v", str(str(speed[1])+"c/"+str(speed[0])), "-w", width, "-s", symmetry, "-f", "1"], capture_output=True, text=True)
       if "0 spaceships" in process.stdout:
           pass
       else:
           found=1
           foundships+=1
           get=0
           print(str(speed[1])+"c/"+str(speed[0]) + " in " + rule)
           for k in process.stdout.splitlines():
               if "x =" in k:
                   get=1
               if get==1:
                   print(k)
                   if k.strip()=="":
                       break
           return found
   elif speed[1] == speed[2]:
       print("Searching for " + str(speed[1]) + "c/" + str(speed[0]) + "d in " + rule + ", width " + "width")
       level = int(width)*2*speed[0]
       process=subprocess.run(["./gfind", rule+"/N"+str(speed[1])+"D"+str(speed[0])+"/L"+str(level)], capture_output=True, text=True)
       if not 'x =' in process.stdout:
           pass
       else:
           found=1
           foundships+=1
           get=0
           print(str(speed[1])+"c/"+str(speed[0]) + "d in " + rule)
           for k in process.stdout.splitlines():
               if "x =" in k:
                   get=1
               if get==1:
                   print(k)
                   if k.strip()=="":
                       break
           return found
   else:
       print("Oblique searching is currently not supported.")
       return 0

def pickrule():

   pick="B0"
   while "B0" in pick:
       pick=random.choice(shipslist).split(':')[2]
   return str(pick)

print("SHIPSEARCH v0.64, by hibiscus") while foundships < numships:

   chos=pickrule()
   for j in range(4,9):
       if findsmallestunknown(chos)==None:
           break
       search=findship(chos,findsmallestunknown(chos),str(j),"e")
       if search==1:
           break