Created
July 12, 2011 10:06
-
-
Save whs/1077717 to your computer and use it in GitHub Desktop.
HLP Hackathon Thursday round
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json, sys | |
| inp = json.load(open("input.json")) | |
| #inp = json.load(open("file145")) | |
| #print inp | |
| turn = 0 | |
| curPage = 0 | |
| lastPage = 0 | |
| while turn <= inp['time']: | |
| if len(inp['switches']) > 0: | |
| if inp['switches'][0]['time'] == turn: | |
| lastPage = curPage | |
| curPage = int(inp['switches'][0]['pageIndex']) | |
| inp['switches'].pop(0) | |
| sys.stderr.write(str(turn) + ": Looking at page "+str(curPage)+"\n") | |
| if turn == 0: | |
| turn = 1 | |
| continue | |
| # now look at the page | |
| mePage = inp['pages'][curPage]['images'] | |
| cnt = 0 | |
| maxThread = 5 | |
| for k,i in enumerate(mePage): | |
| if i['downloaded']: | |
| continue | |
| cnt += 1 | |
| mePage[k]['downloadedTime'] += 1 | |
| if mePage[k]['downloadedTime'] >= i['totalTime']: | |
| mePage[k]['downloaded'] = True | |
| if cnt > maxThread: | |
| break | |
| if cnt < maxThread and lastPage != curPage: | |
| sys.stderr.write(str(turn) + ": Having "+str(maxThread-cnt)+" extra time! Looking at "+str(lastPage)+"\n") | |
| mePage = inp['pages'][lastPage]['images'] | |
| for k,i in enumerate(mePage): | |
| if i['downloaded']: | |
| continue | |
| cnt += 1 | |
| mePage[k]['downloadedTime'] += 1 | |
| if mePage[k]['downloadedTime'] >= i['totalTime']: | |
| mePage[k]['downloaded'] = True | |
| if cnt > maxThread: | |
| break | |
| turn += 1 | |
| print json.dumps({"pages": inp['pages']}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment