comparison nono.py @ 55:68004ce55703

convert to newer source of Nonogram data
author Henry Thompson <ht@markup.co.uk>
date Mon, 29 May 2023 22:24:53 +0100
parents 6c9e371b0325
children 40273cb7c7fc
comparison
equal deleted inserted replaced
54:dd63412fc882 55:68004ce55703
7 # 19 7 # 19
8 # maps to 8 # maps to
9 # 13 9 # 13
10 # 1 1 10 # 1 1
11 # 2 9 11 # 2 9
12 # New version, input taken from Nonograms Outer HTML plus
13 # > fgrep 'task = ' nono10.xhtml|tr ';' '\n' | fgrep task\ = | sed 's/^.*= //'| tr -d \'
14 # 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
15
12 16
13 import sys 17 import sys
14 18
15 Red='' 19 Red=''
16 eRed='' 20 eRed=''
22 26
23 class Vector(list): 27 class Vector(list):
24 # reads top-to-bottom or left-to-right 28 # reads top-to-bottom or left-to-right
25 def __init__(self,n,m,runs): 29 def __init__(self,n,m,runs):
26 list.__init__(self,list(range(n))) 30 list.__init__(self,list(range(n)))
27 self.n=n 31 self.n=n # size
28 self.m=m 32 self.m=m # parent NoNo
29 self.margin0=0 # a run can start here, so if >0 then self[m0-1].val must be False 33 self.margin0=0 # a run can start here, so if >0 then self[m0-1].val must be False
30 self.marginN=n-1 # a run can end here, so if <n-1 then self[mN+1].val ditto 34 self.marginN=n-1 # a run can end here, so if <n-1 then self[mN+1].val ditto
31 self.runs=self.origRuns=runs 35 self.runs=self.origRuns=runs
32 self.initialComplete=[] 36 self.initialComplete=[]
33 self.finalComplete=[] 37 self.finalComplete=[]
58 :type j: int 62 :type j: int
59 :param pos0: left margin 63 :param pos0: left margin
60 :type pos0: int 64 :type pos0: int
61 :param posN: right margin 65 :param posN: right margin
62 :type posN: int 66 :type posN: int
67 :return: list of runlens separated, possibly prefixed, with skips as negative integers
63 """ 68 """
64 bound=posN-(pos0+runLen)+1 69 bound=posN-(pos0+runLen)+1
65 dprint('s',i,j,pos0,posN,runLen,bound) 70 dprint('s',i,j,pos0,posN,runLen,bound)
66 if j==self.rn: 71 if j==self.rn:
67 yield [] 72 yield []
534 if len(sys.argv)>1: 539 if len(sys.argv)>1:
535 f=open(sys.argv[1]) 540 f=open(sys.argv[1])
536 else: 541 else:
537 f=sys.stdin 542 f=sys.stdin
538 543
539 cols=[] 544 l = f.readline().rstrip()
540 545 vv=l.split('/')
541 for l in f: 546 n=int(len(vv)/2)
542 l=l.rstrip() 547 print('%sx%s puzzle'%(n,n),file=sys.stderr)
543 if l=='': 548 cols=[[int(i) for i in v.split('.')] for v in vv[:n]]
544 break 549 rows=[[int(i) for i in v.split('.')] for v in vv[n:]]
545 if 'x' in l:
546 vv=[int(s) for s in l.split('x')]
547 else:
548 vv=[int(c) for c in l]
549 cols.append(vv)
550
551 rows=[[int(s) for s in l.split()] for l in f]
552 550
553 solver=Nono(rows,cols) 551 solver=Nono(rows,cols)
554 solver.solve() 552 solver.solve()
553