## Instagram Puzzle solution

So. I managed to solve Instagram’s puzzle and submit it hoping to get a job (or at least a T-shirt!). I understand there are quite a few people who’d be interested to see the solution so I decided to publish it here. It goes without saying that by now thousands of people have submitted their solutions so if you’re planning to send this in, well you’re an idiot. jk feel free if you want 🙂

Before showing you the code let me tell you a cool story my bros 🙂

***

*00:00 am Tuesday Nov 13 :* I’m browsing through tech blogs and see the challenge advertised on TechCrunch. Immediately start drawing possible solutions on paper. it seems to be really easy.

*00:30 am*: I decide to stay awake and write the code, shouldn’t be hard. The challenge tips to use of PIL, a python library. Python… it’s been 3 years since last time I barely spoke any python. I Try to get DrPython and PIL installed on OS X. no luck. then I download Ubuntu, install it on my machine.

*01:30 am*: DrPython and PIL work fine on Ubuntu. Time to think of an algorithm, then draw a few flowcharts to get an idea of how to write the code.

*02:00 am*: It seems like I have a solid and logical solution, shouldn’t take much to write the code. start coding

*03:00 am*: dear lord, I can’t speak any python at all 😦 help Google!

*04:00 am*: Migrane kicks in, terrible headache. but Google’s once again proven to be helpful!

*05:00 am*: I’m making progress managed to score the shreds based on their compatibility now need to find out how to find the right order (didn’t think of this when I did the flowchart ) My head, it is in a BAD situation.

*07:30 am*: Phew! it works!! spent ages on figuring out how to crop the regions from the original and place them in the out put only to realise it was actually written on Instagram’s blog post as a “hint” 😐 at this point I feel like someone’s axing my head… from inside.

*08:00 am*: solution submitted. now I can get some sleep… oh wait. need to be in uni at 10

***

Now, these few lines took me 8 hours to write. Surely if I knew python i wouldn’t have taken so long. It sounds dead simple. yeah it is to read, but certainly not to come up with! I hereby grant permission to anyone to use any part (or the entire) code for any (peaceful) reason. Credit is not demanded but much appreciated 🙂 feel free to ask any questions.

**NOTE:** you need to download the shredded puzzle from here, name it in.png and put it in the same folder as your .py file

"""Instagram Challenge""" """http://alibros.co.uk""" """eilya.bros@gmail.com""" from PIL import Image import math import operator #functions def bigpick(matrix,i): index=[] temp=newtemp=0 for j in range(20): newtemp=matrix[i][j] if newtemp>temp: index=[newtemp,i,j] temp=newtemp return index def make_list(size): mylist = [] for i in range(size): mylist.append(0) return mylist def make_matrix(rows, cols): matrix = [] for i in range(rows): matrix.append(make_list(cols)) return matrix def getpix(x, y): width, height = image.size pixel = data[y * width + x] return pixel image = Image.open('in.png') data= image.getdata() x, y = 20, 90 scores= make_matrix(20,20) shred_width=32 shreds=[] width, height = image.size for shred_number in range(20): x1, y1 = shred_width * shred_number, 0 x2, y2 = x1 + shred_width, height points= x1,y1,x2,y2 shreds.append(points) score=0 for i in range(20): for j in range(20): score=0 for y in range(359): right=getpix((32*j),y) left=getpix((32*(i+1))-1,y) tempscore=0; for p in range(3): diff=math.fabs(left[p]-right[p]) if diff<10: tempscore=tempscore+1 if tempscore==3: score=score+1 else: score=score+0 scores[i][j]=score resault=make_matrix(20,1) for i in range(20): resault[i]=bigpick(scores,i) resault.sort(key=lambda x:x[0],reverse=True) order=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] order[19]=resault[19][1] orderIndex=19 while orderIndex>0: for item in resault: if item[2]==order[orderIndex]: newIndex=item[1] orderIndex=orderIndex-1 order[orderIndex]=newIndex unshredded=Image.new("RGBA", image.size) shred_width=32; shred_number=0 for i in order: source_region=image.crop(shreds[i]) destination_point=(shred_number*32,0) unshredded.paste(source_region, destination_point) shred_number=shred_number+1 unshredded.save("unshredded.jpg","JPEG")

brilliantly insightful post. If only it was as easy to implement some of the solutions as it was to read and nod my head at each of your points 325422