changeset 59:c22f83e049a9 simple

check0 works for nono10, but is broken in that col 3 w/o the row 7 hit would find the row 3 hit as satisfying the last (2nd) run, which isn't _necessarily_ correct
author Henry Thompson <ht@markup.co.uk>
date Sat, 03 Jun 2023 22:11:12 +0100
parents a3aaf6c085f4
children
files nono.py
diffstat 1 files changed, 61 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/nono.py	Thu Jun 01 19:02:22 2023 +0100
+++ b/nono.py	Sat Jun 03 22:11:12 2023 +0100
@@ -68,9 +68,6 @@
           pos+=1
     self.initDisp()
 
-  def __repr__(self):
-    return "V@%s%s:%s"%(self.x,self.runs,list.__repr__(self))
-
   def __str__(self):
     return '%s|'%('|'.join(str(c) for c in self))
 
@@ -369,17 +366,68 @@
     
   #=============new simple============
   def pass0(self):
+    # simple first pass down/across a row
     for i in range(0,len(self.runs),2):
       run=self.runs[i]
       for p in range(run.i+run.s,run.i+run.n):
         self[p].setVal(True)
 
+  def check0(self):
+    # check 1st and last not-done runs for completeness
+    # forwards
+    for i in range(0,len(self.runs),2):
+      run=self.runs[i]
+      if not run.done:
+        # look for first True cell
+        for p in range(run.i,run.i+run.s+1):
+          if self[p].val:
+            l=self.trueRun(p,run.n)
+            if l==run.n:
+              if p!=0:
+                self[p-1].setVal(False)
+              e=p+l
+              if e!=self.n:
+                self[e].setVal(False)
+              break
+        break
+      # and backwards
+    m=len(self.runs)-1
+#    if isinstance(self,Column) and self.x==3:
+#      breakpoint()
+    for i in range(0,m+1,2):
+      run=self.runs[m-i]
+      if not run.done:
+        # look for first True cell
+        for p in range(run.i+run.s,run.i-1,-1):
+          if self[p].val:
+            l=self.trueRun(p,run.n,-1)
+            if l==run.n:
+              e=p-l
+              if e!=0:
+                self[e].setVal(False)
+              if p+1!=self.n:
+                self[p+1].setVal(False)
+              break
+        break
+
+  def trueRun(self,p,n,delta=1):
+    res=0
+    for i in range(p,p+(delta*n),delta):
+      if self[i].val:
+        res+=1
+      else:
+        break
+    return res
+
 class Row(Vector):
   cLet='R'
   def __init__(self,n,m,pos):
     Vector.__init__(self,n,m)
     self.y=pos
 
+  def __repr__(self):
+    return "R@%s%s:%s"%(self.y,self.runs,list.__repr__(self))
+
   def initDisp(self):
     self.width=self.myPrintSize()
 
@@ -426,6 +474,9 @@
     Vector.__init__(self,n,m)
     self.x=pos
 
+  def __repr__(self):
+    return "C@%s%s:%s"%(self.x,self.runs,list.__repr__(self))
+
   def initDisp(self):
     self.height=self.myPrintSize()
 
@@ -487,7 +538,7 @@
     self.column[y]=self
 
   def __repr__(self):
-    return "C@(%s,%s):%s"%(self.x,self.y,self.val)
+    return "c@(%s,%s):%s"%(self.x,self.y,self.val)
 
   def __str__(self):
     return ' ' if self.val is None else ('\u25A0' if self.val else 'x')
@@ -548,6 +599,10 @@
       c.pass0()
     for r in self.rows:
       r.pass0()
+    for c in self.columns:
+      c.check0()
+    for r in self.rows:
+      r.check0()
 
   def solve(self):
     self.pass0()
@@ -584,6 +639,8 @@
 def eprint(*args,**kw):
   print(*args,file=sys.stderr)
   sys.stderr.flush()
+  print(SOLVER)
+  breakpoint()
   exit(kw['err'])
 
 def wprint(*args):