You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.1 KiB

#!/usr/bin/env python
import subprocess
import sys
import json
def stringToCommandSequence(string):
commandsSequence = ""
phrase = ""
string = string.lower()
for char in string:
found = False
for i in range(0, len(characters)):
if char in characters[i]:
phrase += characters[i][0]
commandsSequence += commands[i]
found = True
continue
if found == False:
print "error in conversion: %s, %s"%(string, char)
sys.exit(1)
return phrase
commands = ['MoveW',
'MoveE',
'MoveSW',
'MoveSE',
'RotateClockwise',
'RotateCounterclockwise']
characters = [['p', "'", '!', '.', '0', '3'],
['b', 'c', 'e', 'f', 'y', '2'],
['a', 'g', 'h', 'i', 'j', '4'],
['l', 'm', 'n', 'o', ' ', '5'],
['d', 'q', 'r', 'v', 'z', '1'],
['k', 's', 't', 'u', 'w', 'x']]
with open('Phrases of power') as f:
phrases = f.readlines()
f.close()
process = subprocess.Popen(['curl --user :2aaFCkjNIDHAsAIh9iQHc+Y+FGhkM5Z0RQgpO6TL6EA= -X GET https://davar.icfpcontest.org/teams/235/solutions'], shell = True, stdout=subprocess.PIPE)
out, err = process.communicate()
if err == None:
serverResults = json.loads(out)
else:
print "unable to fetch server"
sys.exit(1)
powerPhrases = []
for phrase in phrases:
if phrase != "\n" and phrase[0] != '#':
phrase = phrase.lower()[:-1]
found = False
for serverResult in serverResults:
if serverResult['solution'].encode('ascii','replace') == phrase :
found = True
if serverResult['powerScore'] > 0:
print "Eureka! %s powerScore: %s score: %s"%(phrase , serverResult['powerScore'], serverResult['score'])
powerPhrases.append(phrase )
break
elif serverResult['powerScore'] is None:
print " Evaluation pending on %s"%phrase
break
if found:
continue
for i in range (0, 25):
process = subprocess.Popen(['src/TestSolution -f sample_problems/problem_'+str(i)+'.json -n 0 -p ' + stringToCommandSequence(phrase )], shell = True, stdout=subprocess.PIPE)
out, err = process.communicate()
if err == None and out.find("Error") == -1 and out.find("Irrefutable") == -1:
break
if err == None and out.find("Error") == -1 and out.find("Irrefutable") == -1:
OUTPUT=('[{"seed":0,"tag":"shapeShifter","solution":"%s","problemId":12}]'%phrase ).replace('"', "\\\"")
process = subprocess.Popen(['curl --user :2aaFCkjNIDHAsAIh9iQHc+Y+FGhkM5Z0RQgpO6TL6EA= -X POST -H "Content-Type: application/json" -d "' + OUTPUT + '" https://davar.icfpcontest.org/teams/235/solutions'], shell = True, stdout=subprocess.PIPE)
out, err = process.communicate()
if err == None and out == "created":
print "%s sended"%phrase
else:
print out
print " %s unable to comunicate with server"%phrase
else:
print " %s return error"%phrase
print "\n%d power phrases found:"%len(powerPhrases)
for powerPhrase in powerPhrases:
print powerPhrase
#print "\n%d power phrases found only on server:"%len(powerPhrases)
#for serverResult in serverResults:
# if serverResult['powerScore'] > 0:
# if serverResult['solution'].replace('\n', '') not in powerPhrases:
# print serverResult['solution'], serverResult['powerScore']