pythonchallenge Level 32

第32关地址:http://www.pythonchallenge.com/pc/rock/arecibo.html

用户名:repeat  密码:switch

是个nonograms游戏

查看源码:

得到 warmup.txt

打开:http://www.pythonchallenge.com/pc/rock/warmup.txt

是个9*9的nonograms游戏

做出来是个向上的箭头

打开:http://www.pythonchallenge.com/pc/rock/up.txt

 

 是个32*32的nonograms游戏,参考:https://www.jianshu.com/p/378482e69f51 

import base64
from PIL import Image
import requests

def getNonograms(url):
    Authorization = format(base64.b64encode(b'kohsamui:thailand').decode())
    headers = {'Authorization': 'Basic %s' % Authorization}
    response = requests.request("GET", url, headers=headers)
    lines = response.text.splitlines()
    lines = [line.strip() for line in lines if (not line.startswith('#')) and line.strip() != '']
    raw = [list(map(int, line.split())) for line in lines]
    return raw[0][0], raw[0][1], raw[1:raw[0][0] + 1], raw[raw[0][0] + 1:]

def load(file):
    with open(file, encoding='utf-8') as f:
        lines = f.readlines()
        lines = [line.strip() for line in lines if (not line.startswith('#')) and line.strip()!='']
        raw = [list(map(int, line.split())) for line in lines]
        return raw[0][0], raw[0][1], raw[1:raw[0][0] + 1], raw[raw[0][0] + 1:]

def game(hor, ver, hl, vl):
    hor_alts = [[],] * hl
    ver_alts = [[],] * vl
    line = ['-',] * vl
    board = []
    for h in range(hl):
        board.append(line+[])
    dires = [0, 1]

    init(hor, hor_alts, dires[0])
    init(ver, ver_alts, dires[1])

    count = 0

    while True:
        rever_map(hor_alts, hl, board, dires[0])
        rever_map(ver_alts, vl, board, dires[1])
        count = count +1
        if count == 15:
            break
    for b in board:
        if '-' in b:
            print('未解决,还应该再改进!')
    return board

def init(hor, alts, d):
    for i, line in enumerate(hor):
        x, d, pos = getData(line, hl)
        alts[i] = []
        getP(x, d, pos, alts[i])

def rever_map(schemes, length, board, d):
    for i in range(length):
        pss, ass = [], []
        line = getLine(i, board, d)
        getIndex('#', line, pss)
        getIndex('*', line, ass)
        schemes[i] = del_im(pss, ass, schemes[i])
        times = getTimes(schemes[i], length)
        if len(schemes[i]) !=0:
            setMap(i, schemes[i], times, board, d)

def setMap(i, schemes, times, board, d):
    l = len(schemes)
    for j, t in enumerate(times):
        if t == l and d == 1:
            board[j][i] = '#'
        elif t == 0 and d == 1:
            board[j][i] = '*'
        elif t == l and d == 0:
            board[i][j] = '#'
        elif t == 0 and d == 0:
            board[i][j] = '*'
    if l == 1:
        schemes = []

def getLine(i, board, d):
    if d == 0:
        return board[i]
    else:
        return [board[j][i] for j in range(len(board))]

def del_im(pss, ass, scheme):
    res = []
    for s in scheme:
        flag = True
        for p in pss:
            if s[p] != 1:
                flag = False
                break
        for p in ass:
            if s[p] != 0:
                flag = False
                break
        if flag:
            res.append(s)
    return res

def getTimes(source, l):
    res = [0,] * l
    for s in source:
        for i in range(l):
            res[i] = res[i] + s[i]
    return res

def getIndex(s, source, res):
    t = 0
    while True:
        if s in source[t:]:
            i = source.index(s, t)
            res.append(i)
            t = i + 1
        else:
            break

def getP(x, d, pos, res):# 获取可能性
    if len(pos) == 1:
        d = d + [0,] * x
        res.append(d)
        return
    for m in range(x,-1,-1):
        if m > 0:
            new_d = d[:pos[0]] + [0,] * m + d[pos[0]:]
            new_pos = [p+m for p in pos]
            if x - m > 0:
                getP(x-m, new_d, new_pos[1:], res)
            elif x - m == 0:
                res.append(new_d)
        elif m == 0:
            getP(x, d, pos[1:], res)

def getData(data, n):
    d = []
    for i in data: # 转化序列,[2, 1, 3] → [1, 1, 0, 1, 0, 1, 1, 1]
        d = d + [1,] * i + [0]
    d = d[:-1]
    x = n - len(d)
    pos = {0}
    i = 0
    try:
        while True:
            t = d.index(0, i)
            pos.add(t)
            i = t + 1
    except:
        if len(d) > 1:
            pos.add(len(d) - 1)
    pos = list(pos)
    pos.sort()
    return x, d, pos

def drawPic(board, x, y):
    img = Image.new('L', (x, y))
    data = []
    for b in board:
        for p in b:
            if p == '#':
                data.append(0)
            else:
                data.append(255)
    img.putdata(data)
    return img

url = "http://www.pythonchallenge.com/pc/rock/up.txt"
hl, vl, hor, ver = getNonograms(url)
board = game(hor, ver, hl, vl)
img = drawPic(board, vl, hl)
img.save("python.png")

得到一张图

 打开:http://www.pythonchallenge.com/pc/rock/python.html

搜索:"Free" as in "Free speech"

得到:"Free as in beer" "Free as in speech"

 

得到下一关地址:http://www.pythonchallenge.com/pc/rock/beer.html

posted @ 2021-12-07 10:09  OTAKU_nicole  阅读(76)  评论(0编辑  收藏  举报