usr bin env python from time import sleep import math import pygame fr

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
from time import sleep
import math
import pygame
from vm import getVM
from xa4a.computer import get_hohmann, get_hohmann_delay, split_vector
from a2k.logger import Logger
from solver_nope import SimpleSolver
INPUT_PORTS = {0x2: 'dVx',
0x3: 'dVy',
0x3e80: 'Configuration'}
OUTPUT_PORTS = dict(enumerate('Score Fuel sxE syE sxT syT'.split()))
SCALE=20e7
DISPLAY_SKIP=400
class SecondSolver(SimpleSolver):
def __init__(self, problem_num=2001):
self.o = getVM('task/bin2.obf')
self.iports = [0.] * (1 << 14)
self.iports[0x3e80] = problem_num
self.logger = Logger(129, problem_num, [0x2, 0x3, 0x3e80])
self.pygame_init()
def run(self):
screen, o, iports, logger = self.screen, self.o, self.iports, self.logger
prev_sx, prev_sy = None, None
state = 0
dv1, dv2, t1, t3, c = None, None, None, None, -1
color = 120
for x in xrange(1000000):
c+=1
oports = o.iterate(iports)
logger(iports)
score, fuel, sx, sy, sxtB, sytB = oports[:6]
# sxtB is Between satellites, sxt is from target to Earth
sxt, syt = sxtB - sx, sytB - sy
if not x % 100:
color += 1
if color > 240:
color -= 100
self.draw(x, sx, sy, sxt, syt, color)
if state == 0:
t1 = get_hohmann_delay(sx, sy, sxt, syt)
if t1 > 0:
print "Wait for %s seconds" % t1
state = 10
c = -1
elif state == 10:
if c >= t1:
state = 11
target = math.sqrt(sxt**2 + syt**2)
dv1, dv2, t2, dir = get_hohmann((sx, sy), (prev_sx, prev_sy), target)
dv1 = split_vector(sx, sy, dv1, dir)
iports[0x2], iports[0x3] = dv1
print (dv1, dv2, t1+t2), "\nTHRUST!!! (%s)" % ((dv1,))
print "BEGIN", sx, sy
state = 1
c = -1
elif state == 1:
iports[0x2], iports[0x3] = 0,0
state = 2
elif state == 2:
if c >= t2:
cR = math.sqrt(sx**2 + sy**2)
dv2 = split_vector(sx, sy, dv2, dir)
iports[0x2], iports[0x3] = dv2
state = 4
print "THRUST!!!", (sx, sy)
elif state == 4:
iports[0x2], iports[0x3] = 0,0
state = 5
elif state == 6:
break
prev_sx, prev_sy = sx, sy
if score != 0.0:
print "Score: %s" % score
state = 6
#logger.write('bin1.osf')
def draw(self, x, sx, sy, sxT, syT, color=120):
if sx is None:
return
scale = lambda x, y: (int(600*(x + SCALE/2) / SCALE), int(600*(SCALE/2 - y) / SCALE))
pygame.draw.circle(self.screen, (color,0,0), scale(sx,sy), 3)
pygame.draw.circle(self.screen, (0,color,0), scale(sxT,syT), 3)
cR = math.sqrt(sx**2 + sy**2)
tR = math.sqrt(sxT**2 + syT**2)
dR = math.sqrt((sx-sxT)**2 + (sy-syT)**2)
status = "x=%s, cR=%s, tR=%s, dR=%s" % (x, cR, tR, dR)
statusbar = self.font.render(status, True, (255,255,255))
self.screen.blit(self.bg, (0, 580))
self.screen.blit(statusbar, (0, 580))
pygame.display.flip()
if __name__ == '__main__':
SecondSolver(2003).run_forever()