# HG changeset patch # User Henry Thompson # Date 1754769418 14400 # Node ID 63e87db2bc31127af2aa5a5c8246c47001bc8c28 # Parent 15e540c190d5616cfd8992a998ca2704a1640d4b update to modern syntax using ruff on lochinver diff -r 15e540c190d5 -r 63e87db2bc31 nono.py --- a/nono.py Sat Aug 09 15:48:34 2025 -0400 +++ b/nono.py Sat Aug 09 15:56:58 2025 -0400 @@ -14,105 +14,109 @@ # Change in the middle # New dead: If deterministic, split the run in two and requeue the cell (now in two runs) # fill: If deterministic single gap at any end, kill, split, and requeue the cell (now in two runs) -#---------- +# ---------- # Doesn't yet cover everything, e.g. killing gaps that are too small to be filled # Do we need to actually represent run-internal segments, e.g. skips and bars? import sys -Red='' -eRed='' -RedFmt=Red+'%s'+eRed +Red = "" +eRed = "" +RedFmt = Red + "%s" + eRed + class Run: '''What we're looking for, converted eventually into what - we've found. - "left" and "right" make sense for Rows, for Columns read "above" and "below"''' - def __init__(self,n,i,j): + we've found. + "left" and "right" make sense for Rows, for Columns read "above" and "below"''' + + def __init__(self, n, i, j): # A run of length n, starting within [i,j] - self.n=n - self.i=i - self.j=j - self.s=j-i - self.done=False - self.left=self.right=None + self.n = n + self.i = i + self.j = j + self.s = j - i + self.done = False + self.left = self.right = None def leftEdge(self): return self.i def rightEdge(self): if self.done: - return self.i+self.n-1 - return self.j+self.n-1 + return self.i + self.n - 1 + return self.j + self.n - 1 def __str__(self): return str(self.n) - + + class Space: - def __init__(self,n,i,j): + def __init__(self, n, i, j): # A space of length 1..n, starting within [i,j] # Once done, length == n # i and j don't change, but s, which is used by check0 # to decide where to look, is optimistic and may need to # be restricted if there is an overlap with the next (forward or backward) # run (see self.right and self.left, if any, respectively) - assert n>0 - self.n=n - self.i=i - self.j=j - self.s=j-i - self.done=False + assert n > 0 + self.n = n + self.i = i + self.j = j + self.s = j - i + self.done = False def __str__(self): - return '' + return "" + class Vector(list): # reads top-to-bottom or left-to-right - def __init__(self,n,m): - list.__init__(self,list(range(n))) - self.n=n # size - self.m=m # parent NoNo - self.margin0=0 # a run can start here, so if >0 then self[m0-1].val must be False - self.marginN=n-1 # a run can end here, so if 0 then self[m0-1].val must be False + self.marginN = n - 1 # a run can end here, so if run.n) else 100)): + for p in range( + run.i, + min( + run.i + run.s + 1, + run.right.leftEdge() - run.n if (run.right and run.i > run.n) else 100, + ), + ): if self[p].val: - if p>0 and self[p-1].val==False: + if p > 0 and self[p - 1].val == False: # We're pinned, force start here from now on - self.i=p - self.s=p+1 - self.right=None #Maybe? - l=self.trueRun(p,run.n) - if l==run.n: - if p!=0: - self[p-1].setVal(False) - e=p+l - if e!=self.n: + self.i = p + self.s = p + 1 + self.right = None # Maybe? + l = self.trueRun(p, run.n) + if l == run.n: + if p != 0: + self[p - 1].setVal(False) + e = p + l + if e != self.n: self[e].setVal(False) break break # and backwards - m=len(self.runs)-1 -# if isinstance(self,Column) and self.x==3: -# breakpoint() - for i in range(0,m+1,2): - run=self.runs[m-i] + m = len(self.runs) - 1 + # if isinstance(self,Column) and self.x==3: + # breakpoint() + for i in range(0, m + 1, 2): + run = self.runs[m - i] if not run.done: # look for first True cell - for p in range(run.i+run.s+(run.n-1), - max(run.i-1,run.left.rightEdge()+run.n if (run.left and run.i %s at %s,%s"%(self.val, v, self.x,self.y),err=103) - self.val=v + eprint( + "Reset not allowed: %s -> %s at %s,%s" % (self.val, v, self.x, self.y), + err=103, + ) + self.val = v return True + class Nono(list): - # 0,0 is upper left, so increasing y goes _downwards_, to match the standard layout - def __getitem__(self,xy): - return list.__getitem__(self,xy[1])[xy[0]] + def __getitem__(self, xy): + return list.__getitem__(self, xy[1])[xy[0]] - def __setitem__(self,xy,v): - list.__getitem__(self,xy[1])[xy[0]] = v + def __setitem__(self, xy, v): + list.__getitem__(self, xy[1])[xy[0]] = v - def __init__(self,runsPerRow: list[int], - runsPerCol: list[int], - debug: bool) -> list[list[Cell]]: + def __init__( + self, runsPerRow: list[int], runsPerCol: list[int], debug: bool + ) -> list[list[Cell]]: global SOLVER - self.loop=0 - self.dp='' # old depth hack + self.loop = 0 + self.dp = "" # old depth hack self.debug = debug - self.dstate=[] - SOLVER=self - n=self.n=len(runsPerCol) - list.__init__(self,list(list(list(range(n)) for _ in range(n)))) - if n!=len(runsPerRow): - print("losing r:%s x c:%s"%(len(runsPerRow),n),sys.stderr) + self.dstate = [] + SOLVER = self + n = self.n = len(runsPerCol) + if n != len(runsPerRow): + print("losing r:%s x c:%s" % (len(runsPerRow), n), sys.stderr) exit(1) - self.rc=runsPerRow - self.cc=runsPerCol + self.rc = runsPerRow + self.cc = runsPerCol # print col nums>9 vertically :-( - self.columns=cc=[Column(n,self,i) for i in range(n)] - self.rows=rr=[Row(n,self,i) for i in range(n)] + self.columns = cc = [Column(n, self, i) for i in range(n)] + self.rows = rr = [Row(n, self, i) for i in range(n)] for x in range(n): for y in range(n): - self[x,y]=Cell(rr[y],y,cc[x],x) + self[x, y] = Cell(rr[y], y, cc[x], x) # Need cells in place for the following - for row,runs in zip(rr,runsPerRow): + for row, runs in zip(rr, runsPerRow): row.initRuns(runs) - for col,runs in zip(cc,runsPerCol): + for col, runs in zip(cc, runsPerCol): col.initRuns(runs) - self.maxCRheight=maxCRheight=max(col.height for col in cc) + self.maxCRheight = maxCRheight = max(col.height for col in cc) for c in cc: c.updateHeader(maxHeight=maxCRheight) - maxRRwidth=max(row.width for row in rr) + maxRRwidth = max(row.width for row in rr) for r in rr: r.updateHeader(maxWidth=maxRRwidth) - self.rowfmt="%s|%%s|"%(' '*maxRRwidth) + self.rowfmt = "%s|%%s|" % (" " * maxRRwidth) def __str__(self): - lines=[self.rowfmt%('|'.join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate' - for j in range(self.maxCRheight)] - lines+=[str(r) for r in self.rows] + lines = [ + self.rowfmt + % ("|".join([(self.columns[i]).header[j] for i in range(self.n)])) # 'rotate' + for j in range(self.maxCRheight) + ] + lines += [str(r) for r in self.rows] return "\n".join(lines) - def pass0(self): # do columns of empty layout + def pass0(self): # do columns of empty layout for c in self.columns: c.pass0() - dprint('After Pass 0 for columns', self, sep = '\n') + dprint("After Pass 0 for columns", self, sep="\n") for r in self.rows: r.pass0() - dprint('After Pass 0 for rows', self, sep = '\n') + dprint("After Pass 0 for rows", self, sep="\n") for c in self.columns: c.check0() - dprint('After Check 0 for columns', self, sep = '\n') + dprint("After Check 0 for columns", self, sep="\n") dprint(self) for r in self.rows: r.check0() - dprint('After Check 0 for rows', self, sep = '\n') + dprint("After Check 0 for rows", self, sep="\n") def solve(self): self.pass0() + def dprint(*args, **kwargs): - '''Debug print''' + """Debug print""" if SOLVER.debug: print(*args, **kwargs) sys.stdout.flush() -def eprint(*args,**kw): - '''error print''' - print(*args,file=sys.stderr) + +def eprint(*args, **kw): + """error print""" + print(*args, file=sys.stderr) sys.stderr.flush() print(SOLVER) breakpoint() - exit(kw['err']) + exit(kw["err"]) + -if __name__ == '__main__': - if sys.argv[1] == '-d': +if __name__ == "__main__": + if sys.argv[1] == "-d": sys.argv.pop(1) debug = True else: debug = False - if len(sys.argv)>1: - f=open(sys.argv[1]) + if len(sys.argv) > 1: + f = open(sys.argv[1]) else: - f=sys.stdin + f = sys.stdin l = f.readline().rstrip() - vv=l.split('/') - n=int(len(vv)/2) - print('%sx%s puzzle'%(n,n),file=sys.stderr) - cols=[[int(i) for i in v.split('.')] for v in vv[:n]] - rows=[[int(i) for i in v.split('.')] for v in vv[n:]] + vv = l.split("/") + n = int(len(vv) / 2) + print("%sx%s puzzle" % (n, n), file=sys.stderr) + cols = [[int(i) for i in v.split(".")] for v in vv[:n]] + rows = [[int(i) for i in v.split(".")] for v in vv[n:]] - solver=Nono(rows, cols, debug) + solver = Nono(rows, cols, debug) print(solver) print() solver.solve() - print('Done',solver, sep = '\n') - - - + print("Done", solver, sep="\n")