War game/pythonchallenge

pythonchallenge level14

badcob 2009. 11. 17. 19:35
http://www.pythonchallenge.com/pc/return/italy.html

이 문제에서 참 많이 헤멨다.  포럼의 힌트들을 몇번씩 읽어봐도 감이 전혀 오질 않았다.

여기저기서 헤메던 중 쭌사마가 준 힌트가 결정적 이었다.
풀어야지 풀어야지 하다가 안보이는 눈으로 더듬더듬 풀어냈다.
얼마나 채워야  그림이 완성될지 모르겠어서 rounds 값을 조금씩 늘려 외곽부터 채워서 완성.

import Image

def get_croissant(something, count, rounds, nw):
    
    wire_pixel = 0
    while wire_pixel < something:
        for i in range(count, 100-count):
            tmp = im.getpixel((wire_pixel,0))
            nw.putpixel((i,count),tmp)
            wire_pixel += 1
        
        for i in range(count+1, 100-count):
            tmp = im.getpixel((wire_pixel,0))
            nw.putpixel((100-count-1,i),tmp)
            wire_pixel += 1

        for i in range(100-count-2,count-1,-1):
            tmp = im.getpixel((wire_pixel,0))
            im_new.putpixel((i,100-count-1),tmp)
            wire_pixel += 1
    
        for i in range(100-count-2,count,-1):
            tmp = im.getpixel((wire_pixel,0))
            nw.putpixel((count,i),tmp)
            wire_pixel += 1

        count += 1
        if count >= rounds:
            break
    nw.show()
        
im = Image.open('wire.png')
max = im.size[0]

im_new = Image.new('RGB',(100,100))

get_croissant(max, 0, 40, im_new)


http://www.pythonchallenge.com/pc/return/cat.html