Project Euler #220: Heighway Dragon

  • + 0 comments

    i have this lengthy solution, if anybody can suggest better

    a = 'arbfr'
    b = 'lfalb'
    
    def moves(d0, m):
        x,y = 0,0
        directions = ["north","east","south","west"]
        current = "north"
        count = 0
        for i in range(len(d0)):
            if d0[i]=="l":
                current = directions[(directions.index(current)+3)%4]
            elif d0[i]=="r":
                current = directions[(directions.index(current)+1)%4]
            elif d0[i]=="f":
                count = count +1
                if current=="north":
                    y = y+1
                elif current=="east":
                    x = x+1
                elif current=="south":
                    y = y-1
                elif current=="west":
                    x = x-1  
            if count == m:
                return (x,y)
            else:
                continue
    
    for _ in range(int(input())):
        d0 = 'fa'
        n = int(input())
        m = int(input(),16)
        
        for i in range(n):
            d0 = list(d0)
            for j in range(len(d0)):
                if d0[j]=="a":
                    d0[j] = a
                    j = j+5
                elif d0[j]=="b":
                    d0[j] = b
                    j = j+5
            d0 = "".join(d0)
            
        final_pos = moves(d0,m)
        print('{:X}'.format(final_pos[0]))
        print('{:X}'.format(final_pos[1]))