annotate nono.py @ 71:eea4bcb657bc simple

Pruned back to pass 0
author Henry Thompson <ht@markup.co.uk>
date Fri, 18 Jul 2025 21:54:39 +0100
parents 578cb569d815
children 15e540c190d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
55
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
2 # New version, input taken from Nonograms Outer HTML plus
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
3 # > fgrep 'task = ' nono10.xhtml|tr ';' '\n' | fgrep task\ = | sed 's/^.*= //'| tr -d \'
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
4 # E.g. 8/5.1/2.3/2.1/4.3/3.4/2/1.1/3/5.2/2.2/2.2/1.3.2/5.1/3.1.1/2/2.3.1/7.1/1.1.2/3.2
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
5
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 import sys
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 Red=''
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 eRed=''
9
28a7b0e992cb lots of fails in trace of 5a wrt checkNew/found0/foundN???
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
10 RedFmt=Red+'%s'+eRed
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
12 class Run:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
13 '''What we're looking for, converted eventually into what
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
14 we've found.
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
15 "left" and "right" make sense for Rows, for Columns read "above" and "below"'''
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
16 def __init__(self,n,i,j):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
17 # A run of length n, starting within [i,j]
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
18 self.n=n
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
19 self.i=i
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
20 self.j=j
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
21 self.s=j-i
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
22 self.done=False
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
23 self.left=self.right=None
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
24
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
25 def leftEdge(self):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
26 return self.i
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
27
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
28 def rightEdge(self):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
29 if self.done:
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
30 return self.i+self.n-1
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
31 return self.j+self.n-1
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
32
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
33 def __str__(self):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
34 return str(self.n)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
35
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
36 class Space:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
37 def __init__(self,n,i,j):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
38 # A space of length 1..n, starting within [i,j]
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
39 # Once done, length == n
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
40 # i and j don't change, but s, which is used by check0
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
41 # to decide where to look, is optimistic and may need to
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
42 # be restricted if there is an overlap with the next (forward or backward)
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
43 # run (see self.right and self.left, if any, respectively)
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
44 assert n>0
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
45 self.n=n
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
46 self.i=i
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
47 self.j=j
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
48 self.s=j-i
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
49 self.done=False
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
50
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
51 def __str__(self):
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
52 return ''
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
53
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 class Vector(list):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55 # reads top-to-bottom or left-to-right
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
56 def __init__(self,n,m):
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 list.__init__(self,list(range(n)))
55
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
58 self.n=n # size
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
59 self.m=m # parent NoNo
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
60 self.margin0=0 # a run can start here, so if >0 then self[m0-1].val must be False
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
61 self.marginN=n-1 # a run can end here, so if <n-1 then self[mN+1].val ditto
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
62
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
63 def initRuns(self,runs):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
64 # Set runs to alternating list of Run and Skip
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
65 self.rn=len(runs)
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
66 self.runs=[]
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
67 if self.rn==0:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
68 # No runs means a vector of x
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
69 for c in self:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
70 c.setVal(False)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
71 else:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
72 need=sum(1+runs[k] for k in range(self.rn))-1
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
73 space=self.n-need
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
74 pos=0
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
75 while runs:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
76 self.runs.append(Run(r:=runs.pop(0),pos,pos+space))
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
77 pos+=r
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
78 if runs:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
79 self.runs.append(Space(space,pos,pos+space))
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
80 pos+=1
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
81 # Adjacent pairs mutually restrict possible starting points
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
82 for i in range(0,self.rn-1):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
83 left=self.runs[2*i]
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
84 right=self.runs[(2*i)+2]
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
85 left.right=right
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
86 right.left=left
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
87 self.initDisp()
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
88
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
89 def __str__(self):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
90 return '%s|'%('|'.join(str(c) for c in self))
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
91
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
92 def myPrintSize(self):
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
93 return sum((len(str(run)) if isinstance(run,Run) else 1) for run in self.runs)
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
94
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
95 def pass0(self):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
96 # simple first pass down/across a row
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
97 for i in range(0,len(self.runs),2):
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
98 run=self.runs[i]
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
99 for p in range(run.i+run.s,run.i+run.n):
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
100 self[p].setVal(True)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
101
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
102 def check0(self):
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
103 # Check 1st and last not-done runs for completeness
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
104 # Have to restrict lookahead/behind if there's overlap between runs...
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
105 # Forwards
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
106 # ****Assumes margin0 is 0***** and marginN is n
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
107 for i in range(0,len(self.runs),2):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
108 run=self.runs[i]
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
109 if not run.done:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
110 # look for first True cell
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
111 for p in range(run.i,min(run.i+run.s+1,
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
112 run.right.leftEdge()-run.n if (run.right and run.i>run.n) else 100)):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
113 if self[p].val:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
114 if p>0 and self[p-1].val==False:
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
115 # We're pinned, force start here from now on
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
116 self.i=p
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
117 self.s=p+1
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
118 self.right=None #Maybe?
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
119 l=self.trueRun(p,run.n)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
120 if l==run.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
121 if p!=0:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
122 self[p-1].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
123 e=p+l
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
124 if e!=self.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
125 self[e].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
126 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
127 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
128 # and backwards
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
129 m=len(self.runs)-1
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
130 # if isinstance(self,Column) and self.x==3:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
131 # breakpoint()
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
132 for i in range(0,m+1,2):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
133 run=self.runs[m-i]
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
134 if not run.done:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
135 # look for first True cell
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
136 for p in range(run.i+run.s+(run.n-1),
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
137 max(run.i-1,run.left.rightEdge()+run.n if (run.left and run.i<self.n-run.n) else 0),-1):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
138 if self[p].val:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
139 if p<self.n-1 and self[p+1]==False:
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
140 self.i=p
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
141 self.s=p+1
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
142 self.left=None # Maybe?
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
143 l=self.trueRun(p,run.n,-1)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
144 if l==run.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
145 e=p-l
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
146 if e!=0:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
147 self[e].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
148 if p+1!=self.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
149 self[p+1].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
150 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
151 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
152
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
153 def trueRun(self,p,n,delta=1):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
154 res=0
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
155 for i in range(p,p+(delta*n),delta):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
156 if self[i].val:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
157 res+=1
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
158 else:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
159 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
160 return res
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
161
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
162 class Row(Vector):
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
163 cLet='R'
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
164 def __init__(self,n,m,pos):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
165 Vector.__init__(self,n,m)
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
166 self.y=pos
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
167
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
168 def __repr__(self):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
169 return "R@%s%s:%s"%(self.y,self.runs,list.__repr__(self))
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
170
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
171 def initDisp(self):
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
172 self.width=self.myPrintSize()
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
173
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
174 def __str__(self):
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
175 return ((self.fmt%(' '.join(str(r) for r in self.runs if isinstance(r,Run))))+
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
176 Vector.__str__(self))
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
177
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
178 def updateHeader(self,*,maxWidth=None,r=None,pre=None):
8
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
179 if maxWidth is None:
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
180 # update
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
181 spacer=(" " if self.runs else "")
9
28a7b0e992cb lots of fails in trace of 5a wrt checkNew/found0/foundN???
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
182 if pre:
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
183 self.infix+=(RedFmt%r)+spacer
9
28a7b0e992cb lots of fails in trace of 5a wrt checkNew/found0/foundN???
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
184 else:
28a7b0e992cb lots of fails in trace of 5a wrt checkNew/found0/foundN???
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
185 # post
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
186 self.suffix=spacer+RedFmt%r+self.suffix
9
28a7b0e992cb lots of fails in trace of 5a wrt checkNew/found0/foundN???
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
187 self.fmt="%s%s%%s%s|"%(self.prespace,self.infix,self.suffix)
8
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
188 else:
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
189 # init
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
190 self.maxWidth=maxWidth
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
191 self.prespace=' '*(maxWidth-self.width)
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
192 self.fmt="%s%%s|"%self.prespace
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
193 self.infix=""
9
28a7b0e992cb lots of fails in trace of 5a wrt checkNew/found0/foundN???
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
194 self.suffix=""
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
195
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
196 class Column(Vector):
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
197 cLet='C'
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
198 def __init__(self,n,m,pos):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
199 Vector.__init__(self,n,m)
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
200 self.x=pos
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
201
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
202 def __repr__(self):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
203 return "C@%s%s:%s"%(self.x,self.runs,list.__repr__(self))
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
204
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
205 def initDisp(self):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
206 self.height=self.myPrintSize()
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
207
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
208 def updateHeader(self,*,maxHeight=None,r=None,pre=None):
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
209 dprint('CuH',r,pre)
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
210 if maxHeight is None:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
211 # update
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
212 if pre:
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
213 for rc in str(r):
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
214 self.infix.append(RedFmt%rc)
15
22b0894c0f4c fixed Column.updateHeader wrt 5a, but broke it wrt C0 and missing red for R2
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 14
diff changeset
215 if self.runs:
22b0894c0f4c fixed Column.updateHeader wrt 5a, but broke it wrt C0 and missing red for R2
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 14
diff changeset
216 self.infix.append('-')
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
217 else:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
218 # post
15
22b0894c0f4c fixed Column.updateHeader wrt 5a, but broke it wrt C0 and missing red for R2
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 14
diff changeset
219 ins=["-"] if self.runs else []
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
220 for rc in str(r):
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
221 ins.append(RedFmt%r)
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
222 self.suffix=ins+self.suffix
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
223 dprint('CuH1: |%s|,%s,%s,%s'%(self.prespace,self.infix,self.suffix,self.runs))
15
22b0894c0f4c fixed Column.updateHeader wrt 5a, but broke it wrt C0 and missing red for R2
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 14
diff changeset
224 self.header=([" "]*self.prespace)+\
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
225 self.infix+\
15
22b0894c0f4c fixed Column.updateHeader wrt 5a, but broke it wrt C0 and missing red for R2
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 14
diff changeset
226 (['-'.join(str(c) for c in self.runs)] if self.runs else [])+\
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
227 self.suffix
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
228 else:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
229 # init
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
230 self.maxHeight=maxHeight
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
231 self.infix=[]
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
232 self.suffix=[]
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
233 self.prespace=maxHeight - self.height # pad to same 'height'
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
234 self.fmt="%s%%s"%(' '*self.prespace)
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
235 header=('-'.join(str(c) for c in self.runs if isinstance(c,Run)))
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
236 self.header=self.fmt%header
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
237 dprint(self.header)
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
238
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
239 class Cell:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
240 def __init__(self,row,y,column,x):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
241 # At the intersection of row and column Vectors
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
242 self.row=row
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
243 self.column=column
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
244 self.x=x
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
245 self.y=y
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
246 self.val=None # three valued: None(unknown), True(filled), False(empty)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
247 self.row[x]=self
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
248 self.column[y]=self
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
249
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
250 def __repr__(self):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
251 return "c@(%s,%s):%s"%(self.x,self.y,self.val)
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
252
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
253 def __str__(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
254 return ' ' if self.val is None else ('\u25A0' if self.val else 'x')
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
255
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
256 def setVal(self,v):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
257 # Returns true iff old value was None
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
258 assert v in (True, False)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
259 if v == self.val:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
260 return False
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
261 if self.val is not None:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
262 eprint("Reset not allowed: %s -> %s at %s,%s"%(self.val, v, self.x,self.y),err=103)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
263 self.val=v
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
264 return True
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
265
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
266 class Nono(dict):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
267 # 0,0 is upper left, so increasing y goes _downwards_, to match the standard layout
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
268 def __init__(self,runsPerRow,runsPerCol,debug):
16
a7a10e40b344 5a working after obvious bugfix,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 15
diff changeset
269 global SOLVER
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
270 self.loop=0
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
271 self.dp='' # old depth hack
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
272 self.debug = debug
16
a7a10e40b344 5a working after obvious bugfix,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 15
diff changeset
273 self.dstate=[]
a7a10e40b344 5a working after obvious bugfix,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 15
diff changeset
274 SOLVER=self
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
275 n=self.n=len(runsPerCol)
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
276 if n!=len(runsPerRow):
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
277 print("losing r:%s x c:%s"%(len(runsPerRow),n),sys.stderr)
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
278 exit(1)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
279 self.rc=runsPerRow
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
280 self.cc=runsPerCol
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
281 # print col nums>9 vertically :-(
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
282 self.columns=cc=[Column(n,self,i) for i in range(n)]
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
283 self.rows=rr=[Row(n,self,i) for i in range(n)]
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
284 for x in range(n):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
285 for y in range(n):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
286 self[(x,y)]=Cell(rr[y],y,cc[x],x)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
287 # Need cells in place for the following
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
288 for row,runs in zip(rr,runsPerRow):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
289 row.initRuns(runs)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
290 for col,runs in zip(cc,runsPerCol):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
291 col.initRuns(runs)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
292 self.maxCRheight=maxCRheight=max(col.height for col in cc)
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
293 for c in cc:
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
294 c.updateHeader(maxHeight=maxCRheight)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
295 maxRRwidth=max(row.width for row in rr)
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
296 for r in rr:
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
297 r.updateHeader(maxWidth=maxRRwidth)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
298 self.rowfmt="%s|%%s|"%(' '*maxRRwidth)
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
299
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
300 def __str__(self):
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
301 lines=[self.rowfmt%('|'.join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate'
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
302 for j in range(self.maxCRheight)]
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
303 lines+=[str(r) for r in self.rows]
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
304 return "\n".join(lines)
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
305
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
306 def pass0(self): # do columns of empty layout
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
307 for c in self.columns:
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
308 c.pass0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
309 dprint('After Pass 0 for columns', self, sep = '\n')
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
310 for r in self.rows:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
311 r.pass0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
312 dprint('After Pass 0 for rows', self, sep = '\n')
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
313 for c in self.columns:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
314 c.check0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
315 dprint('After Check 0 for columns', self, sep = '\n')
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
316 dprint(self)
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
317 for r in self.rows:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
318 r.check0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
319 dprint('After Check 0 for rows', self, sep = '\n')
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
320
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
321 def solve(self):
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
322 self.pass0()
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
323
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
324 def dprint(*args, **kwargs):
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
325 '''Debug print'''
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
326 if SOLVER.debug:
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
327 print(*args, **kwargs)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
328 sys.stdout.flush()
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
329
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
330 def eprint(*args,**kw):
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
331 '''error print'''
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
332 print(*args,file=sys.stderr)
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
333 sys.stderr.flush()
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
334 print(SOLVER)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
335 breakpoint()
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
336 exit(kw['err'])
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
337
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
338 if __name__ == '__main__':
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
339 if sys.argv[1] == '-d':
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
340 sys.argv.pop(1)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
341 debug = True
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
342 else:
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
343 debug = False
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
344 if len(sys.argv)>1:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
345 f=open(sys.argv[1])
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
346 else:
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
347 f=sys.stdin
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
348
55
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
349 l = f.readline().rstrip()
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
350 vv=l.split('/')
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
351 n=int(len(vv)/2)
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
352 print('%sx%s puzzle'%(n,n),file=sys.stderr)
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
353 cols=[[int(i) for i in v.split('.')] for v in vv[:n]]
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
354 rows=[[int(i) for i in v.split('.')] for v in vv[n:]]
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
355
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
356 solver=Nono(rows, cols, debug)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
357 print(solver)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
358 print()
11
1b9daa849b0f A bit further: red col working, finding middle of row 1, formatting off, outer loop in place, but fails overall after 3rd pass achieves nothing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
359 solver.solve()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
360 print('Done',solver, sep = '\n')
55
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
361
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
362
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
363