Mercurial > hg > python
comparison nono.py @ 58:a3aaf6c085f4 simple
pass0, initial display working with Run and Space
author | Henry Thompson <ht@markup.co.uk> |
---|---|
date | Thu, 01 Jun 2023 19:02:22 +0100 |
parents | 057b52746850 |
children | c22f83e049a9 |
comparison
equal
deleted
inserted
replaced
57:057b52746850 | 58:a3aaf6c085f4 |
---|---|
17 def __init__(self,n,i,j): | 17 def __init__(self,n,i,j): |
18 # A run of length n, starting within [i,j] | 18 # A run of length n, starting within [i,j] |
19 self.n=n | 19 self.n=n |
20 self.i=i | 20 self.i=i |
21 self.j=j | 21 self.j=j |
22 self.s=j-i | |
22 self.done=False | 23 self.done=False |
23 | 24 |
24 def __str__(self): | 25 def __str__(self): |
25 return str(self.n) | 26 return str(self.n) |
26 | 27 |
30 # Once done, length == n | 31 # Once done, length == n |
31 assert n>0 | 32 assert n>0 |
32 self.n=n | 33 self.n=n |
33 self.i=i | 34 self.i=i |
34 self.j=j | 35 self.j=j |
36 self.s=j-i | |
35 self.done=False | 37 self.done=False |
36 | 38 |
37 def __str__(self): | 39 def __str__(self): |
38 return '-' | 40 return '' |
39 | 41 |
40 class Vector(list): | 42 class Vector(list): |
41 # reads top-to-bottom or left-to-right | 43 # reads top-to-bottom or left-to-right |
42 def __init__(self,n,m): | 44 def __init__(self,n,m): |
43 list.__init__(self,list(range(n))) | 45 list.__init__(self,list(range(n))) |
71 | 73 |
72 def __str__(self): | 74 def __str__(self): |
73 return '%s|'%('|'.join(str(c) for c in self)) | 75 return '%s|'%('|'.join(str(c) for c in self)) |
74 | 76 |
75 def myPrintSize(self): | 77 def myPrintSize(self): |
76 return sum(len(str(run)) for run in self.runs) | 78 return sum((len(str(run)) if isinstance(run,Run) else 1) for run in self.runs) |
77 | 79 |
78 #=========v-old-v============ | 80 #=========v-old-v============ |
79 | 81 |
80 def resetAllRuns(self): | 82 def resetAllRuns(self): |
81 # compute the set of all possible layouts for runs | 83 # compute the set of all possible layouts for runs |
363 r=self.runs.pop() | 365 r=self.runs.pop() |
364 self.finalComplete=[r]+self.finalComplete | 366 self.finalComplete=[r]+self.finalComplete |
365 self.marginN-=r+1 | 367 self.marginN-=r+1 |
366 self.updateHeader(r=r,pre=False) | 368 self.updateHeader(r=r,pre=False) |
367 | 369 |
368 #=============new simple============ | 370 #=============new simple============ |
369 def pass0(self): | 371 def pass0(self): |
370 need=sum(1+self.runs[k] for k in range(self.rn))-1 | 372 for i in range(0,len(self.runs),2): |
371 space=self.n-need | 373 run=self.runs[i] |
372 p=0 | 374 for p in range(run.i+run.s,run.i+run.n): |
373 for run in self.runs: | 375 self[p].setVal(True) |
374 if run>space: | |
375 for p in range(p+space,p+run): | |
376 self[p].setVal(True) | |
377 p+=1 | |
378 else: | |
379 p+=run | |
380 p+=1 | |
381 | 376 |
382 class Row(Vector): | 377 class Row(Vector): |
383 cLet='R' | 378 cLet='R' |
384 def __init__(self,n,m,pos): | 379 def __init__(self,n,m,pos): |
385 Vector.__init__(self,n,m) | 380 Vector.__init__(self,n,m) |
387 | 382 |
388 def initDisp(self): | 383 def initDisp(self): |
389 self.width=self.myPrintSize() | 384 self.width=self.myPrintSize() |
390 | 385 |
391 def __str__(self): | 386 def __str__(self): |
392 return ((self.fmt%(' '.join(str(r) for r in self.runs)))+ | 387 return ((self.fmt%(' '.join(str(r) for r in self.runs if isinstance(r,Run))))+ |
393 Vector.__str__(self)) | 388 Vector.__str__(self)) |
394 | 389 |
395 def updateHeader(self,*,maxWidth=None,r=None,pre=None): | 390 def updateHeader(self,*,maxWidth=None,r=None,pre=None): |
396 if maxWidth is None: | 391 if maxWidth is None: |
397 # update | 392 # update |
459 self.maxHeight=maxHeight | 454 self.maxHeight=maxHeight |
460 self.infix=[] | 455 self.infix=[] |
461 self.suffix=[] | 456 self.suffix=[] |
462 self.prespace=maxHeight - self.height # pad to same 'height' | 457 self.prespace=maxHeight - self.height # pad to same 'height' |
463 self.fmt="%s%%s"%(' '*self.prespace) | 458 self.fmt="%s%%s"%(' '*self.prespace) |
464 header=('-'.join(str(c) for c in self.runs)) | 459 header=('-'.join(str(c) for c in self.runs if isinstance(c,Run))) |
465 self.header=self.fmt%header | 460 self.header=self.fmt%header |
466 dprint(self.header) | 461 dprint(self.header) |
467 | 462 |
468 def newBlob(self,y,crossCheck=False): | 463 def newBlob(self,y,crossCheck=False): |
469 self[y].setVal(True) | 464 self[y].setVal(True) |
607 print('%sx%s puzzle'%(n,n),file=sys.stderr) | 602 print('%sx%s puzzle'%(n,n),file=sys.stderr) |
608 cols=[[int(i) for i in v.split('.')] for v in vv[:n]] | 603 cols=[[int(i) for i in v.split('.')] for v in vv[:n]] |
609 rows=[[int(i) for i in v.split('.')] for v in vv[n:]] | 604 rows=[[int(i) for i in v.split('.')] for v in vv[n:]] |
610 | 605 |
611 solver=Nono(rows,cols) | 606 solver=Nono(rows,cols) |
612 breakpoint() | |
613 solver.solve() | 607 solver.solve() |
614 | 608 |