annotate nono.py @ 72:15e540c190d5 simple

derive Nono from list, not dict, and get [x,y] working
author Henry Thompson <ht@markup.co.uk>
date Sat, 09 Aug 2025 15:48:34 -0400
parents eea4bcb657bc
children 63e87db2bc31
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
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
6 ####### New Plan #######
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
7 # All that matters is runs of incomplete cells. We begin with n+n runs, the maximum number is
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
8 # sum(len(rn) for rn in rows U cols)
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
9 # My own strategy seems to be FIFO for pass 0, then LIFO, for the queue of new/changed runs
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
10 # By cases, wrt types of change of a cell in its runs
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
11 # Change at the margin:
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
12 # New fill: complete the run and discard it, kill the other end and queue it
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
13 # dead: advance margin and add fill to any internal sequences of fills (always?), queue the new fill(s)
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
14 # Change in the middle
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
15 # New dead: If deterministic, split the run in two and requeue the cell (now in two runs)
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
16 # fill: If deterministic single gap at any end, kill, split, and requeue the cell (now in two runs)
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
17 #----------
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
18 # Doesn't yet cover everything, e.g. killing gaps that are too small to be filled
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
19 # Do we need to actually represent run-internal segments, e.g. skips and bars?
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
20
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
21
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
22 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
23
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
24 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
25 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
26 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
27
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
28 class Run:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
29 '''What we're looking for, converted eventually into what
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
30 we've found.
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
31 "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
32 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
33 # 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
34 self.n=n
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
35 self.i=i
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
36 self.j=j
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
37 self.s=j-i
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
38 self.done=False
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
39 self.left=self.right=None
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
40
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
41 def leftEdge(self):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
42 return self.i
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
43
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
44 def rightEdge(self):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
45 if self.done:
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
46 return self.i+self.n-1
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
47 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
48
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
49 def __str__(self):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
50 return str(self.n)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
51
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
52 class Space:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
53 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
54 # 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
55 # Once done, length == n
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
56 # 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
57 # 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
58 # 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
59 # 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
60 assert n>0
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
61 self.n=n
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
62 self.i=i
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
63 self.j=j
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
64 self.s=j-i
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
65 self.done=False
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
66
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
67 def __str__(self):
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
68 return ''
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
69
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
70 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
71 # 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
72 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
73 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
74 self.n=n # size
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
75 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
76 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
77 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
78
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
79 def initRuns(self,runs):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
80 # Set runs to alternating list of Run and Skip
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
81 self.rn=len(runs)
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
82 self.runs=[]
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
83 if self.rn==0:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
84 # 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
85 for c in self:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
86 c.setVal(False)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
87 else:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
88 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
89 space=self.n-need
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
90 pos=0
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
91 while runs:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
92 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
93 pos+=r
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
94 if runs:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
95 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
96 pos+=1
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
97 # Adjacent pairs mutually restrict possible starting points
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
98 for i in range(0,self.rn-1):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
99 left=self.runs[2*i]
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
100 right=self.runs[(2*i)+2]
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
101 left.right=right
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
102 right.left=left
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
103 self.initDisp()
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
104
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
105 def __str__(self):
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
106 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
107
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
108 def myPrintSize(self):
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
109 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
110
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
111 def pass0(self):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
112 # 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
113 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
114 run=self.runs[i]
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
115 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
116 self[p].setVal(True)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
117
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
118 def check0(self):
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
119 # Check 1st and last not-done runs for completeness
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
120 # 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
121 # Forwards
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
122 # ****Assumes margin0 is 0***** and marginN is n
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
123 for i in range(0,len(self.runs),2):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
124 run=self.runs[i]
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
125 if not run.done:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
126 # look for first True cell
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
127 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
128 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
129 if self[p].val:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
130 if p>0 and self[p-1].val==False:
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
131 # We're pinned, force start here from now on
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
132 self.i=p
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
133 self.s=p+1
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
134 self.right=None #Maybe?
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
135 l=self.trueRun(p,run.n)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
136 if l==run.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
137 if p!=0:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
138 self[p-1].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
139 e=p+l
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
140 if e!=self.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
141 self[e].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
142 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
143 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
144 # and backwards
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
145 m=len(self.runs)-1
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
146 # if isinstance(self,Column) and self.x==3:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
147 # breakpoint()
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
148 for i in range(0,m+1,2):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
149 run=self.runs[m-i]
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
150 if not run.done:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
151 # look for first True cell
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
152 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
153 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
154 if self[p].val:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
155 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
156 self.i=p
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
157 self.s=p+1
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
158 self.left=None # Maybe?
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
159 l=self.trueRun(p,run.n,-1)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
160 if l==run.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
161 e=p-l
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
162 if e!=0:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
163 self[e].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
164 if p+1!=self.n:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
165 self[p+1].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
166 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
167 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
168
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
169 def trueRun(self,p,n,delta=1):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
170 res=0
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
171 for i in range(p,p+(delta*n),delta):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
172 if self[i].val:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
173 res+=1
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
174 else:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
175 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
176 return res
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
177
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
178 class Row(Vector):
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
179 cLet='R'
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
180 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
181 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
182 self.y=pos
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
183
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
184 def __repr__(self):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
185 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
186
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
187 def initDisp(self):
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
188 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
189
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
190 def __str__(self):
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
191 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
192 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
193
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
194 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
195 if maxWidth is None:
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
196 # update
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
197 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
198 if pre:
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
199 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
200 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
201 # post
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
202 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
203 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
204 else:
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
205 # init
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
206 self.maxWidth=maxWidth
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
207 self.prespace=' '*(maxWidth-self.width)
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
208 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
209 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
210 self.suffix=""
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
211
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
212 class Column(Vector):
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
213 cLet='C'
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
214 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
215 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
216 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
217
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
218 def __repr__(self):
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
219 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
220
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
221 def initDisp(self):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
222 self.height=self.myPrintSize()
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
223
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
224 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
225 dprint('CuH',r,pre)
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
226 if maxHeight is None:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
227 # update
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
228 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
229 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
230 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
231 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
232 self.infix.append('-')
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
233 else:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
234 # 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
235 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
236 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
237 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
238 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
239 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
240 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
241 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
242 (['-'.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
243 self.suffix
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
244 else:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
245 # init
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254
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 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
256 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
257 # 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
258 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
259 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
260 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
261 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
262 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
263 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
264 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
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 def __repr__(self):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
267 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
268
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
269 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
270 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
271
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
272 def setVal(self,v):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
273 # 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
274 assert v in (True, False)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
275 if v == self.val:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
276 return False
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
277 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
278 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
279 self.val=v
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
280 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
281
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
282 class Nono(list):
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
283
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
284 # 0,0 is upper left, so increasing y goes _downwards_, to match the standard layout
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
285 def __getitem__(self,xy):
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
286 return list.__getitem__(self,xy[1])[xy[0]]
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
287
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
288 def __setitem__(self,xy,v):
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
289 list.__getitem__(self,xy[1])[xy[0]] = v
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
290
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
291 def __init__(self,runsPerRow: list[int],
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
292 runsPerCol: list[int],
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
293 debug: bool) -> list[list[Cell]]:
16
a7a10e40b344 5a working after obvious bugfix,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 15
diff changeset
294 global SOLVER
13
70993b538ddb works on 5a
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 12
diff changeset
295 self.loop=0
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
296 self.dp='' # old depth hack
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
297 self.debug = debug
16
a7a10e40b344 5a working after obvious bugfix,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 15
diff changeset
298 self.dstate=[]
a7a10e40b344 5a working after obvious bugfix,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 15
diff changeset
299 SOLVER=self
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
300 n=self.n=len(runsPerCol)
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
301 list.__init__(self,list(list(list(range(n)) for _ in range(n))))
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
302 if n!=len(runsPerRow):
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
303 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
304 exit(1)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
305 self.rc=runsPerRow
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
306 self.cc=runsPerCol
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
307 # 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
308 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
309 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
310 for x in range(n):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
311 for y in range(n):
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
312 self[x,y]=Cell(rr[y],y,cc[x],x)
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
313 # 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
314 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
315 row.initRuns(runs)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
316 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
317 col.initRuns(runs)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
318 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
319 for c in cc:
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
320 c.updateHeader(maxHeight=maxCRheight)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
321 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
322 for r in rr:
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
323 r.updateHeader(maxWidth=maxRRwidth)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
324 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
325
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
326 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
327 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
328 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
329 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
330 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
331
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
332 def pass0(self): # do columns of empty layout
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
333 for c in self.columns:
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
334 c.pass0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
335 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
336 for r in self.rows:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
337 r.pass0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
338 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
339 for c in self.columns:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
340 c.check0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
341 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
342 dprint(self)
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
343 for r in self.rows:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
344 r.check0()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
345 dprint('After Check 0 for rows', self, sep = '\n')
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
346
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
347 def solve(self):
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
348 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
349
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
350 def dprint(*args, **kwargs):
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
351 '''Debug print'''
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
352 if SOLVER.debug:
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
353 print(*args, **kwargs)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
354 sys.stdout.flush()
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
355
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
356 def eprint(*args,**kw):
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
357 '''error print'''
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
358 print(*args,file=sys.stderr)
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
359 sys.stderr.flush()
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
360 print(SOLVER)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
361 breakpoint()
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
362 exit(kw['err'])
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
363
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
364 if __name__ == '__main__':
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
365 if sys.argv[1] == '-d':
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
366 sys.argv.pop(1)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
367 debug = True
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
368 else:
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
369 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
370 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
371 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
372 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
373 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
374
55
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
375 l = f.readline().rstrip()
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
376 vv=l.split('/')
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
377 n=int(len(vv)/2)
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
378 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
379 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
380 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
381
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
382 solver=Nono(rows, cols, debug)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
383 print(solver)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
384 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
385 solver.solve()
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
386 print('Done',solver, sep = '\n')
55
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
387
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
388
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
389