annotate nono_17.py @ 38:1c0d430b0fb1 actOnZero

abandoned?
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Mon, 20 Jul 2020 11:06:55 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 # Expects e.g. ^A copy from Nonograms dprint preview cols, then blank line, then rows
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 # rows are space-separated
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 # cols are one-digit-after-another, unless some 2-digit, in which case x is separator
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 # E.g.
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 # 13x1x2
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 # 19
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 # maps to
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 # 13
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 # 1 1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 # 2 9
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 import sys
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 Red=''
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 eRed=''
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 RedFmt=Red+'%s'+eRed
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 def interleave(*args):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 for vals in zip(*args):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 yield from vals
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
23 class Vector(list):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
24 # reads top-to-bottom or left-to-right
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25 def __init__(self,n,m,runs):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 list.__init__(self,list(range(n)))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 self.n=n
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28 self.m=m
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 self.margin0=0 # a run can start here, so if >0 then self[m0-1].val must be False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30 self.marginN=n-1 # a run can end here, so if <n-1 then self[mN+1].val ditto
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 self.runs=self.origRuns=runs
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 self.initialComplete=[]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33 self.finalComplete=[]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
34 self.resetAllRuns()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
35
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 def __repr__(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37 return "V@%s%s:%s"%(self.x,self.origRuns,list.__repr__(self))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
39 def __str__(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40 return '%s|'%('|'.join(str(c) for c in self))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42 def myPrintSize(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 return sum(len(str(run)) for run in self.runs)+len(self.runs)-1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
44
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 def resetAllRuns(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 # compute the set of all possible layouts for runs
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
47 self.rn=len(self.runs)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
48 rtot=sum(self.runs)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 self.allRuns=list(self.seedList(0,0,self.margin0,self.marginN+1,
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50 sum(1+self.runs[k] for k in range(self.rn))-1))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 self.nar=len(self.allRuns)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53 def seedList(self,i,j,pos0,posN,runLen):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 """
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55 :param i: starting skip before next run
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 :type i: 0 if pos==0 else 1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 :param j: next run number
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 :type j: int
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59 :param pos0: left margin
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
60 :type pos0: int
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61 :param posN: right margin
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62 :type posN: int
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63 """
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 bound=posN-(pos0+runLen)+1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
65 dprint('s',i,j,pos0,posN,runLen,bound)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
66 if j==self.rn:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67 yield []
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 return
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
69 r=self.runs[j]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
70 for v in range(i,bound):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 for sub in self.seedList(1,j+1,pos0+v+r,posN,runLen-(r+1)):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 yield [-v,r]+sub
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 def step(self,pos):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 if self.runs==[]:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 return 0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
77 scratch=[0 if c.val is None else c.val for c in self]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
78 wins=0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
79 changed=0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
80 for k,runs in enumerate(self.allRuns):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
81 if (self.m.loop,self.cLet,pos,k)==(1,'R',7,5):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
82 print(self.m)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
83 dprint('=====pass %s.%s.%s.%s======'%(self.m.loop,self.cLet,pos,k))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
84 if self.onepass(self.margin0,self.marginN+1,scratch,runs.copy()):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
85 wins+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
86 dprint(wins, scratch)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
87 maybeSeq=None
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
88 inSeq=None
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
89 j=None
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
90 for i in range(self.n):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
91 if scratch[i]>=wins:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
92 # If blobby in _every_ pass, then must be a blob
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93 if inSeq is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
94 inSeq=i if maybeSeq is None else maybeSeq
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
95 dprint('s1',inSeq)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
96 maybeSeq=None
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
97 if self[i].val is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
98 dprint('s1a',i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
99 self.newBlob(i,True) # Check cross-vector
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
100 j=i
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
101 changed=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
102 elif self[i].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
103 pass
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
104 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
105 eprint("Shouldn't happen: attempt to blob where x already present! %s at %s"%(self,i),err=101)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
106 elif inSeq is not None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
107 if self[i].val is not True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
108 inSeq=None
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
109 maybeSeq=None
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
110 dprint('s2',i-1,j)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
111 if j is not None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
112 # Only if we actually added a blob at some point
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
113 dpush('b_s2')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
114 self.checkNew(i-1)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
115 dpop('b_s2')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
116 elif self[i].val is True and maybeSeq is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
117 dprint('s3',i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
118 maybeSeq=i
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
119 dprint('s4',inSeq,i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
120 if inSeq is not None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
121 dpush('b_s4')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
122 self.checkNew(i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
123 dpop('b_s4')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
124 return changed
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
125
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
126 def onepass(self,i0,iBound,scratch,stack):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
127 """note that stack is not a simple run, but one with _negative_ numbers between
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
128 and possibly before the positive ones, indicating obligatory skips
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
129 """
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
130 i=i0 # starting index into self/scratch/maybe
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
131 maybe=[0]*self.n
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
132 dprint('r: %s'%stack,i0,iBound)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
133 req=sum((-r if r<0 else r) for r in stack)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
134 while stack and i<iBound:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
135 r=rr=stack.pop(0)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
136 dprint('pop:',r)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
137 if r<1:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
138 # obligatory skip
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
139 # (Above init of self.allRuns is easier if we allow a 0 to be ignored
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
140 for k in range(i,i-r):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
141 if self[k] is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
142 # has to be False or None to skip
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
143 dprint('x0',k)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
144 return False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
145 i-=r
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
146 req+=r
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
147 r=rr=stack.pop(0)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
148 # rr is run remaining -- how many we still need
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
149 # First, check if we can start here: 0 is OK, and n>0 iff n-1 is None or False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
150 # if i>0 and self[i-1].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
151 # dprint('x1',i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
152 # return
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
153 if (iBound-i)<req:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
154 # Can't win, give up altogether
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
155 dprint('x2',i,iBound,req)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
156 return False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
157 j=i # start of run, if we find it
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
158 gapsFilled=0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
159 dprint('top:',j)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
160 while rr>0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
161 # guaranteed by check above that we won't run over the far end
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
162 c=self[i].val
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
163 if c is False:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
164 dprint('x3',i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
165 return False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
166 if c is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
167 # we could add a blob here
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
168 gapsFilled+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
169 # and a blob already present is OK
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
170 rr-=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
171 i+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
172 # Maybe a win?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
173 # look ahead, can we stop here?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
174 if i<self.n and self[i].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
175 dprint('x4',i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
176 return False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
177 # elif gapsFilled==0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
178 # # We must have crossed at least one gap...
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
179 # print("Shouldn't happen: no gap! me:%s i:%s j:%s rr:%s"%(self,i, j, rr),file=sys.stderr)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
180 # raise Exception
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
181 # Victory!
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
182 dprint('c6',r,j,i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
183 for k in range(j,i):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
184 maybe[k]+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
185 req-=r
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
186 # on to the next run
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
187 # end of inner loop, did we win?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
188 if (not stack) or i==iBound:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
189 # yes
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
190 dprint('win:',maybe)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
191 for k in range(iBound):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
192 scratch[k]+=maybe[k]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
193 return True
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
194 eprint("Shouldn't happen? - fell through",stack,i,iBound,err=102)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
195
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
196 def checkX(self,pos,crosspos):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
197 # New x at pos, are there others that can be xed out now?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
198 # Overkill as is?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
199 dprint('cx',self.cLet+str(crosspos),pos)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
200 if self.runs:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
201 start0=self.margin0 # 0 if self.margin0==0 else self.margin0+1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
202 if self.marginal(range(start0,pos),self.runs[0]):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
203 dprint('cx1a')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
204 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
205 dprint('cx1b')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
206 # if len(self.runs)>1:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
207 startN=self.marginN # self.marginN if (self.marginN==self.n-1) else self.marginN-1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
208 if self.marginal(range(startN,pos,-1),self.runs[-1]):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
209 dprint('cx2a')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
210 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
211 dprint('cx2b')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
212
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
213 def checkNew(self,pos):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
214 # New blob at pos, can we complete anything?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
215 # Assuming @@ FTTB that it's never possible to definitively complete a
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
216 # non-marginal run, which is wrong if the length is unique and it's bounded...
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
217 start0=self.margin0 #0 if self.margin0==0 else self.margin0+1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
218 startN=self.marginN # self.marginN if (self.marginN==self.n-1) else self.marginN-1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
219 dprint('cn',start0,pos,startN)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
220 changed=False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
221 # First, find our boundaries:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
222 s=pos # start index of our run
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
223 if s>start0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
224 while s>start0 and self[s-1].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
225 s-=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
226 f=pos # finish index of our run
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
227 if f<startN:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
228 while f<startN and self[f+1].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
229 f+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
230 l=(f-s)+1 # our length
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
231 dprint('%s:%s,%s,%s,%s:<%s>'%(self.cLet,s,f,l,self.runs,self))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
232 c=self.runs.count(l)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
233 if c==0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
234 # not big enough yet
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
235 dprint('x0')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
236 return
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
237 if self.runs[0]==l:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
238 # is it safely left marginal, i.e. no blobs or big enough gaps before us?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
239 if self.marginal(range(start0,s),l):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
240 changed=True
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
241 dprint('n1')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
242 # mark our margins
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
243 for i in range(start0,s): # not sure this is still needed since
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
244 # added gap-filling in self.marginal
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
245 if self[i].val is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
246 self.newX(i,False)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
247 if f<startN:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
248 if self[f+1].val is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
249 self.newX(f+1,True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
250 self.found0(f) # pull in the start margin at least to f+2
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
251 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
252 dprint('x1a')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
253 if not changed:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
254 if self.runs[-1]==l:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
255 # is it safely _right_ marginal, i.e. no blobs or big enough gaps _after_ us?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
256 if self.marginal(range(startN,f,-1),l):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
257 changed=True
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
258 dprint('n2')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
259 # mark our margins: still needed? see above
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
260 for i in range(startN,f,-1):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
261 if self[i].val is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
262 self.newX(i,False)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
263 if s>start0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
264 if self[s-1].val is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
265 self.newX(s-1,True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
266 self.foundN(s) # pull in the finish margin at least to s-2
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
267 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
268 dprint('x2a')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
269 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
270 dprint('x2b')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
271 if changed:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
272 self.resetAllRuns()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
273
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
274 def marginal(self,rng,l):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
275 dprint('m%s'%self.cLet,rng.start,rng.stop,rng.step,l)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
276 g=0 # length of a gap
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
277 for i in rng:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
278 if self[i].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
279 # Shouldn't be possible?
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
280 dprint('mx0')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
281 return False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
282 if self[i].val is False:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
283 if g>0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
284 # Block a too-small gap
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
285 dprint('m1',i-g,i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
286 for j in range(i-g,i):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
287 self.newX(j,True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
288 g=0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
289 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
290 # None
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
291 g+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
292 if g==l:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
293 # run could fit here, so no go
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
294 dprint('mx1')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
295 return False
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
296 if g>0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
297 # Block a too-small gap
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
298 if rng.step==1:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
299 # forward
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
300 dprint('m2f',i-g,i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
301 for j in range(i+1-g,i+1):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
302 self.newX(j,True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
303 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
304 # backward
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
305 dprint('m2b',i+g,i)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
306 for j in range(i+g-1,i-1,-1):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
307 self.newX(j,True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
308 return True
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
309
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
310 def found0(self,i):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
311 dprint('found0 called on %s at %s'%(self,i))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
312 i=self.margin0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
313 while self[i].val is False:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
314 i+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
315 if self[i].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
316 r=self.runs.pop(0)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
317 self.initialComplete.append(r)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
318 self.margin0+=r+1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
319 self.updateHeader(r=r,pre=True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
320
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
321 def foundN(self,i):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
322 dprint('foundN called on %s at %s'%(self,i))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
323 i=self.marginN
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
324 while self[i].val is False:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
325 i-=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
326 if self[i].val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
327 r=self.runs.pop()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
328 self.finalComplete=[r]+self.finalComplete
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
329 self.marginN-=r+1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
330 self.updateHeader(r=r,pre=False)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
331
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
332
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
333 class Row(Vector):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
334 cLet='R'
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
335 def __init__(self,n,m,runs,pos):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
336 Vector.__init__(self,n,m,runs)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
337 self.y=pos
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
338 self.width=self.myPrintSize()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
339
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
340 def __str__(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
341 return ((self.fmt%(' '.join(str(r) for r in self.runs)))+
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
342 Vector.__str__(self))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
343
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
344 def updateHeader(self,*,maxWidth=None,r=None,pre=None):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
345 if maxWidth is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
346 # update
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
347 spacer=(" " if self.runs else "")
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
348 if pre:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
349 self.infix+=(RedFmt%r)+spacer
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
350 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
351 # post
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
352 self.suffix=spacer+RedFmt%r+self.suffix
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
353 self.fmt="%s%s%%s%s|"%(self.prespace,self.infix,self.suffix)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
354 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
355 # init
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
356 self.maxWidth=maxWidth
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
357 self.prespace=' '*(maxWidth-self.width)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
358 self.fmt="%s%%s|"%self.prespace
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
359 self.infix=""
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
360 self.suffix=""
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
361
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
362 def newBlob(self,x,crossCheck=False):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
363 self[x].setVal(True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
364 if crossCheck:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
365 dpush('b_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
366 self[x].column.checkNew(self.y)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
367 dpop('b_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
368
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
369 def newX(self,x,crossCheck=False):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
370 dprint('nx %s%s@%s'%('R',self.y,x))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
371 self[x].setVal(False)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
372 if crossCheck:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
373 dpush('x_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
374 self[x].column.checkX(self.y,x)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
375 dpop('x_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
376
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
377 class Column(Vector):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
378 cLet='C'
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
379 def __init__(self,n,m,runs,pos):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
380 Vector.__init__(self,n,m,runs)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
381 self.x=pos
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
382 self.height=self.myPrintSize()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
383
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
384 def updateHeader(self,*,maxHeight=None,r=None,pre=None):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
385 dprint('CuH',r,pre)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
386 if maxHeight is None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
387 # update
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
388 if pre:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
389 for rc in str(r):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
390 self.infix.append(RedFmt%rc)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
391 if self.runs:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
392 self.infix.append('-')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
393 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
394 # post
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
395 ins=["-"] if self.runs else []
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
396 for rc in str(r):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
397 ins.append(RedFmt%r)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
398 self.suffix=ins+self.suffix
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
399 dprint('CuH1: |%s|,%s,%s,%s'%(self.prespace,self.infix,self.suffix,self.runs))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
400 self.header=([" "]*self.prespace)+\
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
401 self.infix+\
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
402 (['-'.join(str(c) for c in self.runs)] if self.runs else [])+\
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
403 self.suffix
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
404 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
405 # init
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
406 self.maxHeight=maxHeight
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
407 self.infix=[]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
408 self.suffix=[]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
409 self.prespace=maxHeight - self.height # pad to same 'height'
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
410 self.fmt="%s%%s"%(' '*self.prespace)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
411 header=('-'.join(str(c) for c in self.runs))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
412 self.header=self.fmt%header
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
413 dprint(self.header)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
414
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
415 def newBlob(self,y,crossCheck=False):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
416 self[y].setVal(True)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
417 if crossCheck:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
418 dpush('b_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
419 self[y].row.checkNew(self.x)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
420 dpop('b_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
421
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
422 def newX(self,y,crossCheck=False):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
423 dprint('nx %s%s@%s'%('C',self.x,y))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
424 self[y].setVal(False)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
425 if crossCheck:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
426 dpush('x_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
427 self[y].row.checkX(self.x,y)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
428 dpop('x_cc')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
429
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
430 class Cell:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
431 def __init__(self,row,y,column,x):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
432 # At the intersection of row and column Vectors
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
433 self.row=row
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
434 self.column=column
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
435 self.x=x
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
436 self.y=y
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
437 self.val=None # three valued: None(unknown), True(filled), False(empty)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
438 self.row[x]=self
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
439 self.column[y]=self
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
440
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
441 def __repr__(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
442 return "C@(%s,%s):%s"%(self.x,self.y,self.val)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
443
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
444 def __str__(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
445 return ' ' if self.val is None else ('\u25A0' if self.val else 'x')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
446
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
447 def setVal(self,v):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
448 if v is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
449 if self.val is False:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
450 wprint("Warning: x -> * at %s,%s"%(self.x,self.y))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
451 elif self.val is True:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
452 # No-op
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
453 return
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
454 self.val=v
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
455 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
456 if self.val is not None:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
457 wprint("Warning: %s -> %s at %s,%s"%(self.val,v,self.x,self.y))
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
458 self.val=v
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
459
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
460
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
461 class Nono(dict):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
462 # 0,0 is upper left, so increasing y goes _downwards_, to match the standard layout
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
463 def __init__(self,runsPerRow,runsPerCol):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
464 global SOLVER
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
465 self.loop=0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
466 self.dp=''
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
467 self.dstate=[]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
468 SOLVER=self
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
469 n=self.n=len(runsPerCol)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
470 if n!=len(runsPerRow):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
471 print("losing r:%s x c:%s"%(len(runsPerRow),n),sys.stderr)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
472 exit(1)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
473 self.rc=runsPerRow
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
474 self.cc=runsPerCol
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
475 # print col nums>9 vertically :-(
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
476 self.columns=cc=[Column(n,self,runsPerCol[i],i) for i in range(n)]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
477 self.maxCRheight=maxCRheight=max(col.height for col in cc)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
478 for c in cc:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
479 c.updateHeader(maxHeight=maxCRheight)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
480 self.rows=rr=[Row(n,self,runsPerRow[i],i) for i in range(n)]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
481 maxRRwidth=max(row.width for row in rr)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
482 for r in rr:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
483 r.updateHeader(maxWidth=maxRRwidth)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
484 self.rowfmt="%s|%%s|"%(' '*maxRRwidth)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
485 for x in range(n):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
486 for y in range(n):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
487 self[(x,y)]=Cell(rr[y],y,cc[x],x)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
488
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
489 def __str__(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
490 lines=[self.rowfmt%('|'.join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate'
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
491 for j in range(self.maxCRheight)]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
492 lines+=[str(r) for r in self.rows]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
493 return "\n".join(lines)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
494
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
495 def solve(self):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
496 someChanged=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
497 while someChanged>0:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
498 self.loop+=1
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
499 someChanged=0
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
500 dprint("***Solve C %s***"%self.loop)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
501 for c in self.columns:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
502 someChanged+=c.step(c.x)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
503 print(someChanged)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
504 print(self)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
505 dprint("***Solve R %s***"%self.loop)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
506 for r in self.rows:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
507 someChanged+=r.step(r.y)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
508 print(someChanged)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
509 print(self)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
510
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
511 def dpush(s):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
512 SOLVER.dp+=' '
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
513 SOLVER.dstate.append(s)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
514
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
515 def dpop(s):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
516 assert(SOLVER.dstate.pop()==s)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
517 SOLVER.dp=SOLVER.dp[1:]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
518
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
519 def dprint(*args):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
520 print(SOLVER.dp,end='')
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
521 print(*args)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
522 sys.stdout.flush()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
523
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
524 def eprint(*args,**kw):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
525 print(*args,file=sys.stderr)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
526 sys.stderr.flush()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
527 exit(kw['err'])
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
528
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
529 def wprint(*args):
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
530 print(*args,file=sys.stderr)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
531 sys.stderr.flush()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
532
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
533 if __name__ == '__main__':
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
534 if len(sys.argv)>1:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
535 f=open(sys.argv[1])
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
536 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
537 f=sys.stdin
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
538
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
539 cols=[]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
540
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
541 for l in f:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
542 l=l.rstrip()
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
543 if l=='':
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
544 break
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
545 if 'x' in l:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
546 vv=[int(s) for s in l.split('x')]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
547 else:
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
548 vv=[int(c) for c in l]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
549 cols.append(vv)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
550
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
551 rows=[[int(s) for s in l.split()] for l in f]
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
552
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
553 solver=Nono(rows,cols)
1c0d430b0fb1 abandoned?
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
554 solver.solve()