annotate nono.py @ 73:63e87db2bc31 simple

update to modern syntax using ruff on lochinver
author Henry Thompson <ht@markup.co.uk>
date Sat, 09 Aug 2025 15:56:58 -0400
parents 15e540c190d5
children 0245bc07f16c
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)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
17 # ----------
72
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
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
24 Red = ""
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
25 eRed = ""
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
26 RedFmt = Red + "%s" + eRed
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
27
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
28
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
29 class Run:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
30 '''What we're looking for, converted eventually into what
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
31 we've found.
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
32 "left" and "right" make sense for Rows, for Columns read "above" and "below"'''
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
33
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
34 def __init__(self, n, i, j):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
35 # A run of length n, starting within [i,j]
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
36 self.n = n
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
37 self.i = i
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
38 self.j = j
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
39 self.s = j - i
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
40 self.done = False
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
41 self.left = self.right = None
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
42
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
43 def leftEdge(self):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
44 return self.i
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
45
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
46 def rightEdge(self):
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
47 if self.done:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
48 return self.i + self.n - 1
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
49 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
50
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
51 def __str__(self):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
52 return str(self.n)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
53
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
54
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
55 class Space:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
56 def __init__(self, n, i, j):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
57 # 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
58 # Once done, length == n
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
59 # 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
60 # 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
61 # 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
62 # run (see self.right and self.left, if any, respectively)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
63 assert n > 0
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
64 self.n = n
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
65 self.i = i
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
66 self.j = j
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
67 self.s = j - i
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
68 self.done = False
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
69
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
70 def __str__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
71 return ""
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
72
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
73
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
74 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
75 # reads top-to-bottom or left-to-right
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
76 def __init__(self, n, m):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
77 list.__init__(self, list(range(n)))
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
78 self.n = n # size
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
79 self.m = m # parent NoNo
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
80 self.margin0 = 0 # a run can start here, so if >0 then self[m0-1].val must be False
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
81 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
82
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
83 def initRuns(self, runs):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
84 # Set runs to alternating list of Run and Skip
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
85 self.rn = len(runs)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
86 self.runs = []
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
87 if self.rn == 0:
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
88 # 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
89 for c in self:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
90 c.setVal(False)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
91 else:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
92 need = sum(1 + runs[k] for k in range(self.rn)) - 1
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
93 space = self.n - need
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
94 pos = 0
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
95 while runs:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
96 self.runs.append(Run(r := runs.pop(0), pos, pos + space))
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
97 pos += r
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
98 if runs:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
99 self.runs.append(Space(space, pos, pos + space))
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
100 pos += 1
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
101 # Adjacent pairs mutually restrict possible starting points
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
102 for i in range(0, self.rn - 1):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
103 left = self.runs[2 * i]
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
104 right = self.runs[(2 * i) + 2]
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
105 left.right = right
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
106 right.left = left
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
107 self.initDisp()
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
108
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
109 def __str__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
110 return "%s|" % ("|".join(str(c) for c in self))
6
a56d5285575b drafted Vector.checkNew, still need found0 and foundN
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 0
diff changeset
111
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
112 def myPrintSize(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
113 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
114
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
115 def pass0(self):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
116 # simple first pass down/across a row
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
117 for i in range(0, len(self.runs), 2):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
118 run = self.runs[i]
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
119 for p in range(run.i + run.s, run.i + run.n):
58
a3aaf6c085f4 pass0, initial display working with Run and Space
Henry Thompson <ht@markup.co.uk>
parents: 57
diff changeset
120 self[p].setVal(True)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
121
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
122 def check0(self):
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
123 # Check 1st and last not-done runs for completeness
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
124 # 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
125 # Forwards
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
126 # ****Assumes margin0 is 0***** and marginN is n
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
127 for i in range(0, len(self.runs), 2):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
128 run = self.runs[i]
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
129 if not run.done:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
130 # look for first True cell
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
131 for p in range(
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
132 run.i,
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
133 min(
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
134 run.i + run.s + 1,
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
135 run.right.leftEdge() - run.n if (run.right and run.i > run.n) else 100,
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
136 ),
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
137 ):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
138 if self[p].val:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
139 if p > 0 and self[p - 1].val == False:
70
578cb569d815 Maybe fixed previous bug,
Henry Thompson <ht@markup.co.uk>
parents: 59
diff changeset
140 # We're pinned, force start here from now on
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
141 self.i = p
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
142 self.s = p + 1
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
143 self.right = None # Maybe?
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
144 l = self.trueRun(p, run.n)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
145 if l == run.n:
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
146 if p != 0:
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
147 self[p - 1].setVal(False)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
148 e = p + l
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
149 if e != self.n:
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
150 self[e].setVal(False)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
151 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
152 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
153 # and backwards
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
154 m = len(self.runs) - 1
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
155 # if isinstance(self,Column) and self.x==3:
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
156 # breakpoint()
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
157 for i in range(0, m + 1, 2):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
158 run = self.runs[m - i]
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
159 if not run.done:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
160 # look for first True cell
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
161 for p in range(
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
162 run.i + run.s + (run.n - 1),
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
163 max(
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
164 run.i - 1,
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
165 run.left.rightEdge() + run.n
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
166 if (run.left and run.i < self.n - run.n)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
167 else 0,
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
168 ),
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
169 -1,
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
170 ):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
171 if self[p].val:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
172 if p < self.n - 1 and self[p + 1] == False:
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
173 self.i = p
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
174 self.s = p + 1
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
175 self.left = None # Maybe?
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
176 l = self.trueRun(p, run.n, -1)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
177 if l == run.n:
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
178 e = p - l
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
179 if e != 0:
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
180 self[e].setVal(False)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
181 if p + 1 != self.n:
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
182 self[p + 1].setVal(False)
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
183 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
184 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
185
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
186 def trueRun(self, p, n, delta=1):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
187 res = 0
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
188 for i in range(p, p + (delta * n), delta):
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
189 if self[i].val:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
190 res += 1
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
191 else:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
192 break
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
193 return res
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
194
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
195
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
196 class Row(Vector):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
197 cLet = "R"
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
198
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
199 def __init__(self, n, m, pos):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
200 Vector.__init__(self, n, m)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
201 self.y = pos
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
202
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
203 def __repr__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
204 return "R@%s%s:%s" % (self.y, self.runs, list.__repr__(self))
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
205
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
206 def initDisp(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
207 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
208
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
209 def __str__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
210 return (
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
211 self.fmt % (" ".join(str(r) for r in self.runs if isinstance(r, Run)))
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
212 ) + Vector.__str__(self)
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
213
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
214 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
215 if maxWidth is None:
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
216 # update
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
217 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
218 if pre:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
219 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
220 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
221 # post
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
222 self.suffix = spacer + RedFmt % r + self.suffix
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
223 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
224 else:
3276cf6ee6df vastly simplified onePass, printing rows working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
225 # init
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
226 self.maxWidth = maxWidth
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
227 self.prespace = " " * (maxWidth - self.width)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
228 self.fmt = "%s%%s|" % self.prespace
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
229 self.infix = ""
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
230 self.suffix = ""
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
231
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
232
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
233 class Column(Vector):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
234 cLet = "C"
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
235
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
236 def __init__(self, n, m, pos):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
237 Vector.__init__(self, n, m)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
238 self.x = pos
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
239
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
240 def __repr__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
241 return "C@%s%s:%s" % (self.x, self.runs, list.__repr__(self))
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
242
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
243 def initDisp(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
244 self.height = self.myPrintSize()
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
245
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
246 def updateHeader(self, *, maxHeight=None, r=None, pre=None):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
247 dprint("CuH", r, pre)
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
248 if maxHeight is None:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
249 # update
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
250 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
251 for rc in str(r):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
252 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
253 if self.runs:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
254 self.infix.append("-")
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
255 else:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
256 # post
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
257 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
258 for rc in str(r):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
259 ins.append(RedFmt % r)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
260 self.suffix = ins + self.suffix
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
261 dprint(
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
262 "CuH1: |%s|,%s,%s,%s" % (self.prespace, self.infix, self.suffix, self.runs)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
263 )
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
264 self.header = (
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
265 ([" "] * self.prespace)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
266 + self.infix
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
267 + (["-".join(str(c) for c in self.runs)] if self.runs else [])
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
268 + self.suffix
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
269 )
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
270 else:
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
271 # init
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
272 self.maxHeight = maxHeight
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
273 self.infix = []
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
274 self.suffix = []
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
275 self.prespace = maxHeight - self.height # pad to same 'height'
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
276 self.fmt = "%s%%s" % (" " * self.prespace)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
277 header = "-".join(str(c) for c in self.runs if isinstance(c, Run))
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
278 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
279 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
280
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
281
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
282 class Cell:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
283 def __init__(self, row, y, column, x):
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 # At the intersection of row and column Vectors
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
285 self.row = row
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
286 self.column = column
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
287 self.x = x
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
288 self.y = y
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
289 self.val = None # three valued: None(unknown), True(filled), False(empty)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
290 self.row[x] = self
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
291 self.column[y] = self
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
292
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
293 def __repr__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
294 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
295
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
296 def __str__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
297 return " " if self.val is None else ("\u25a0" if self.val else "x")
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
298
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
299 def setVal(self, v):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
300 # 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
301 assert v in (True, False)
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
302 if v == self.val:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
303 return False
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
304 if self.val is not None:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
305 eprint(
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
306 "Reset not allowed: %s -> %s at %s,%s" % (self.val, v, self.x, self.y),
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
307 err=103,
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
308 )
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
309 self.val = v
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
310 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
311
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
312
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
313 class Nono(list):
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
314 # 0,0 is upper left, so increasing y goes _downwards_, to match the standard layout
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
315 def __getitem__(self, xy):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
316 return list.__getitem__(self, xy[1])[xy[0]]
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
317
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
318 def __setitem__(self, xy, v):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
319 list.__getitem__(self, xy[1])[xy[0]] = v
72
15e540c190d5 derive Nono from list, not dict, and get [x,y] working
Henry Thompson <ht@markup.co.uk>
parents: 71
diff changeset
320
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
321 def __init__(
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
322 self, runsPerRow: list[int], runsPerCol: list[int], debug: bool
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
323 ) -> list[list[Cell]]:
16
a7a10e40b344 5a working after obvious bugfix,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 15
diff changeset
324 global SOLVER
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
325 self.loop = 0
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
326 self.dp = "" # old depth hack
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
327 self.debug = debug
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
328 self.dstate = []
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
329 SOLVER = self
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
330 n = self.n = len(runsPerCol)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
331 if n != len(runsPerRow):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
332 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
333 exit(1)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
334 self.rc = runsPerRow
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
335 self.cc = runsPerCol
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
336 # print col nums>9 vertically :-(
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
337 self.columns = cc = [Column(n, self, i) for i in range(n)]
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
338 self.rows = rr = [Row(n, self, i) for i in range(n)]
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
339 for x in range(n):
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
340 for y in range(n):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
341 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
342 # Need cells in place for the following
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
343 for row, runs in zip(rr, runsPerRow):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
344 row.initRuns(runs)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
345 for col, runs in zip(cc, runsPerCol):
57
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
346 col.initRuns(runs)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
347 self.maxCRheight = maxCRheight = max(col.height for col in cc)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
348 for c in cc:
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
349 c.updateHeader(maxHeight=maxCRheight)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
350 maxRRwidth = max(row.width for row in rr)
7
13f600be3a1b implemented newBlob and found0, refactoring display
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
351 for r in rr:
10
0a24625b33fa checkNew improving, reverse pass failing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
352 r.updateHeader(maxWidth=maxRRwidth)
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
353 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
354
0
fee51ab07d09 blanket publication of all existing python files in lib/python on maritain
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
355 def __str__(self):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
356 lines = [
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
357 self.rowfmt
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
358 % ("|".join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate'
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
359 for j in range(self.maxCRheight)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
360 ]
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
361 lines += [str(r) for r in self.rows]
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
362 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
363
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
364 def pass0(self): # do columns of empty layout
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
365 for c in self.columns:
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
366 c.pass0()
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
367 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
368 for r in self.rows:
057b52746850 use Run and Space classes in Vector.runs
Henry Thompson <ht@markup.co.uk>
parents: 56
diff changeset
369 r.pass0()
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
370 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
371 for c in self.columns:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
372 c.check0()
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
373 dprint("After Check 0 for columns", self, sep="\n")
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
374 dprint(self)
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
375 for r in self.rows:
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
376 r.check0()
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
377 dprint("After Check 0 for rows", self, sep="\n")
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
378
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
379 def solve(self):
56
40273cb7c7fc begin trivial approach
Henry Thompson <ht@markup.co.uk>
parents: 55
diff changeset
380 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
381
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
382
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
383 def dprint(*args, **kwargs):
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
384 """Debug print"""
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
385 if SOLVER.debug:
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
386 print(*args, **kwargs)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
387 sys.stdout.flush()
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
388
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
389
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
390 def eprint(*args, **kw):
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
391 """error print"""
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
392 print(*args, file=sys.stderr)
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
393 sys.stderr.flush()
59
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
394 print(SOLVER)
c22f83e049a9 check0 works for nono10,
Henry Thompson <ht@markup.co.uk>
parents: 58
diff changeset
395 breakpoint()
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
396 exit(kw["err"])
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
397
14
1e961cf10f88 10a is losing at 1R
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 13
diff changeset
398
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
399 if __name__ == "__main__":
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
400 if sys.argv[1] == "-d":
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
401 sys.argv.pop(1)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
402 debug = True
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
403 else:
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
404 debug = False
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
405 if len(sys.argv) > 1:
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
406 f = open(sys.argv[1])
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
407 else:
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
408 f = sys.stdin
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
409
55
68004ce55703 convert to newer source of Nonogram data
Henry Thompson <ht@markup.co.uk>
parents: 17
diff changeset
410 l = f.readline().rstrip()
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
411 vv = l.split("/")
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
412 n = int(len(vv) / 2)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
413 print("%sx%s puzzle" % (n, n), file=sys.stderr)
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
414 cols = [[int(i) for i in v.split(".")] for v in vv[:n]]
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
415 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
416
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
417 solver = Nono(rows, cols, debug)
71
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
418 print(solver)
eea4bcb657bc Pruned back to pass 0
Henry Thompson <ht@markup.co.uk>
parents: 70
diff changeset
419 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
420 solver.solve()
73
63e87db2bc31 update to modern syntax using ruff on lochinver
Henry Thompson <ht@markup.co.uk>
parents: 72
diff changeset
421 print("Done", solver, sep="\n")