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.

96 lines
3.1 KiB

  1. #!/usr/bin/env python
  2. import subprocess
  3. import sys
  4. import json
  5. def stringToCommandSequence(string):
  6. commandsSequence = ""
  7. phrase = ""
  8. string = string.lower()
  9. for char in string:
  10. found = False
  11. for i in range(0, len(characters)):
  12. if char in characters[i]:
  13. phrase += characters[i][0]
  14. commandsSequence += commands[i]
  15. found = True
  16. continue
  17. if found == False:
  18. print "error in conversion: %s, %s"%(string, char)
  19. sys.exit(1)
  20. return phrase
  21. commands = ['MoveW',
  22. 'MoveE',
  23. 'MoveSW',
  24. 'MoveSE',
  25. 'RotateClockwise',
  26. 'RotateCounterclockwise']
  27. characters = [['p', "'", '!', '.', '0', '3'],
  28. ['b', 'c', 'e', 'f', 'y', '2'],
  29. ['a', 'g', 'h', 'i', 'j', '4'],
  30. ['l', 'm', 'n', 'o', ' ', '5'],
  31. ['d', 'q', 'r', 'v', 'z', '1'],
  32. ['k', 's', 't', 'u', 'w', 'x']]
  33. with open('Phrases of power') as f:
  34. phrases = f.readlines()
  35. f.close()
  36. process = subprocess.Popen(['curl --user :2aaFCkjNIDHAsAIh9iQHc+Y+FGhkM5Z0RQgpO6TL6EA= -X GET https://davar.icfpcontest.org/teams/235/solutions'], shell = True, stdout=subprocess.PIPE)
  37. out, err = process.communicate()
  38. if err == None:
  39. serverResults = json.loads(out)
  40. else:
  41. print "unable to fetch server"
  42. sys.exit(1)
  43. powerPhrases = []
  44. for phrase in phrases:
  45. phrase = phrase.lower()
  46. if phrase != "\n" and phrase[0] != '#':
  47. found = False
  48. for serverResult in serverResults:
  49. if serverResult['solution'].encode('ascii','replace') == phrase[:-1]:
  50. found = True
  51. if serverResult['powerScore'] > 0:
  52. print "Eureka! %s powerScore: %s score: %s"%(phrase[:-1], serverResult['powerScore'], serverResult['score'])
  53. powerPhrases.append(phrase[:-1])
  54. break
  55. elif serverResult['powerScore'] is None:
  56. print " Evaluation pending on %s"%phrase[:-1]
  57. break
  58. else:
  59. #print "No powerScore on %s"%phrase[:-1]
  60. break
  61. if found:
  62. continue
  63. for i in range (0, 25):
  64. process = subprocess.Popen(['src/TestSolution -f sample_problems/problem_'+str(i)+'.json -n 0 -p ' + stringToCommandSequence(phrase[:-1])], shell = True, stdout=subprocess.PIPE)
  65. out, err = process.communicate()
  66. if err == None and out.find("Error") == -1 and out.find("Irrefutable") == -1:
  67. break
  68. if err == None and out.find("Error") == -1 and out.find("Irrefutable") == -1:
  69. OUTPUT=('[{"seed":0,"tag":"shapeShifter","solution":"%s","problemId":12}]'%phrase[:-1]).replace('"', "\\\"")
  70. 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)
  71. out, err = process.communicate()
  72. if err == None and out == "created":
  73. print "%s sended"%phrase[:-1]
  74. else:
  75. print out
  76. print " %s unable to comunicate with server"%phrase[:-1]
  77. else:
  78. print " %s return error"%phrase[:-1]
  79. print "\n%d power phrases found:"%len(powerPhrases)
  80. for powerPhrase in powerPhrases:
  81. print powerPhrase
  82. #print "\n%d power phrases found only on server:"%len(powerPhrases)
  83. #for serverResult in serverResults:
  84. # if serverResult['powerScore'] > 0:
  85. # if serverResult['solution'].replace('\n', '') not in powerPhrases:
  86. # print serverResult['solution'], serverResult['powerScore']