예제 #1
0
def run(x, y, z, num_lines=0, max_retry=1, basename=None):
    """
    指定されたサイズ、線数の問題データと正解データを自動生成して、ファイルbasename*.txtに書き出す。
    @param x,y,z 盤のサイズ
    @param num_lines 線の本数
    @param basename 出力先ファイル名。問題ファイルはbasename_adc.txt、正解ファイルはbasename_adc_sol.txtになる。
    """
    Q = {'size': (x, y, z)}
    num_lines, ban = generate(x,
                              y,
                              z,
                              num_lines=num_lines,
                              max_retry=max_retry,
                              Q_data=Q)
    Q['line_num'] = num_lines
    Q['empty_cells'] = ban.empty_cells()
    print('number of lines:', Q['line_num'])
    print('number of empty cells:', Q['empty_cells'])
    #if verbose: ban.print()
    #if verbose: print('Q=', Q)
    txtQ = Q_text(Q)
    txtA = ban.A_data()

    # nlcheckする
    nlc = NLCheck()
    q = nlc.read_input_str(txtQ)
    a = nlc.read_target_str(txtA)
    #nlc.verbose = verbose
    judges = nlc.check(q, a)
    print("judges = ", judges)

    # 描画する
    nldraw2.setup_font('nonexistent')  # あとで考える
    images = nldraw2.draw(q, a, nlc)
    for num, img in enumerate(images):
        ifile = "%s.%d.gif" % (basename, num + 1)  # 層の番号は1から始まる
        img.save(ifile, 'gif')
        print(ifile)
    if 1 < len(images):
        nldraw2.merge_images(images).save(basename + '.gif', 'gif')

    # QとAを出力する
    if basename is None:
        print(txtQ)
        print(txtA)
    else:
        qfile = '%s_adc.txt' % basename
        with open(qfile, 'w') as f:
            f.write(txtQ)
        afile = '%s_adc_sol.txt' % basename
        with open(afile, 'w') as f:
            f.write(txtA)
        excel(ban, basename)
예제 #2
0
def check_A_data(a_text, q_text):
    """回答データのチェックをする"""
    nlc = NLCheck()
    #nlc.debug = True
    #print "Q=",q_text
    #print "A=",a_text
    out = io.BytesIO() # io.StringIO()だとTypeError: unicode argument expected, got 'str'
    #out = open("/tmp/nlcheck_log.txt","w")
    #--------------------------------------
    sys.stdout = out # 標準出力を付け替える
    input_data  = nlc.read_input_str(q_text)  # 問題データ
    target_data = nlc.read_target_str(a_text) # 回答データ
    nlc.verbose = True
    judges = nlc.check(input_data, target_data)
    print "judges = ", judges
    #results = escape(out.getvalue()) # from xml.sax.saxutils import escape, unescape ここでは不要か?!
    msg = out.getvalue()
    out.close()
    sys.stdout = sys.__stdout__ # もとに戻す
    #--------------------------------------
    return judges, msg
예제 #3
0
def check_A_data(a_str, q_uni):
    """回答データのチェックをする"""
    # 問題データ q_uni (unicode)
    # 回答データ a_str (str)
    nlc = NLCheck()
    #nlc.debug = True
    q_str = q_uni.encode('utf-8')  # unicode -> str
    #print "Q=",q_str
    #print "A=",a_str
    #--------------------------------------
    out = io.BytesIO(
    )  # io.StringIO()だとTypeError: unicode argument expected, got 'str'
    sys.stdout = out  # 標準出力を付け替える
    input_data = nlc.read_input_str(q_str)
    target_data = nlc.read_target_str(a_str)
    nlc.verbose = True
    judges = nlc.check(input_data, target_data)
    print "judges = ", judges
    #results = escape(out.getvalue()) # from xml.sax.saxutils import escape, unescape ここでは不要か?!
    msg = out.getvalue()
    out.close()
    sys.stdout = sys.__stdout__  # もとに戻す
    #--------------------------------------
    return judges, msg
예제 #4
0
def solve(num):
    global project_dir
    o = NLCheck()
    output_info = info['output_info'][num]
    via_to_line = get_via_to_line(info, num)
    adata = 'SIZE %dX%dX%d\r\n' % info['size']
    fail = False
    cwd = os.getcwd()
    #---------------------------------------
    os.chdir(project_dir)
    for layer, info2 in sorted(output_info.items()):
        if layer[0] != 'L' or not layer[1:].isdigit():
            continue
        qfile = info2['ofile']
        afile = exec_solver(qfile)
        #print layer, qfile, afile
        input_data = o.read_input_file(qfile)
        target_data = o.read_target_file(afile)
        target_data2 = o.clean_a(input_data, target_data)
        judges = o.check(input_data, target_data2)
        best = best_target(judges)
        print "judges = ", judges
        print "best = ", best
        #print "best target_data = ", target_data2[best[0]]
        ilayer = int(layer[1:])  # 1,2,…
        via_linenum = get_via_linenum_mat(info, ilayer, via_to_line)
        adata += 'LAYER %d\r\n' % ilayer
        if best[0] is None:
            fail = True
            break
        else:
            bd = target_data2[best[0]]
            adata += generate_A_data(bd, info2['map_line_num'], via_linenum)
            if 0:
                root, ext = os.path.splitext(afile)
                gfile = root + '.gif'
                gif = o.graphic_gif(input_data, [bd], gfile)
                print "gif = ", gif
    os.chdir(cwd)
    #---------------------------------------
    if fail:
        output_info['judges'] = None
        return None
    else:
        # afile: 2016年版ルールの回答ファイルの名前
        root, ext = os.path.splitext(os.path.basename(info['convert_q']))
        afile = root + ('.%d.A.txt' % num)
        afile2 = os.path.join(project_dir, afile)
        with open(afile2, 'w') as fp:
            fp.write(adata)
        # 2016年版ルールで回答チェック
        input_data = o.read_input_file(info['convert_q'])
        target_data = o.read_target_str(adata)
        judges = o.check(input_data, target_data)
        print "afile = ", afile
        print "judges = ", judges
        output_info['afile'] = afile
        output_info['judges'] = judges
        if judges[0][0]:
            # 正解が出た
            gfile = root + ('.%d.A.gif' % num)
            gfile2 = os.path.join(project_dir, gfile)
            gif = o.graphic_gif(input_data, target_data, gfile2)
            print "gif = ", gif

        return afile