Skip to content

Instantly share code, notes, and snippets.

@aragaer
Last active December 20, 2015 07:19
Show Gist options
  • Save aragaer/6092094 to your computer and use it in GitHub Desktop.
Save aragaer/6092094 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import sys
# basic list of actions and their AP rewards
ACTIONS = {
'recharge': 10,
'upgrade': 65,
'destroy resonator': 75,
'hack enemy portal': 100,
'place resonator': 125,
'destroy link': 187,
'last resonator': 250,
'create link': 313,
'first resonator': 500,
'destroy field': 750,
'create field': 1250,
}
# a few more actions
ACTIONS['deploy portal fully'] = ACTIONS['place resonator'] * 8 + ACTIONS['last resonator'] + ACTIONS['first resonator']
ACTIONS['neutralize enemy portal'] = ACTIONS['destroy resonator'] * 8
ACTIONS['capture enemy portal'] = ACTIONS['deploy portal fully'] + ACTIONS['neutralize enemy portal']
# calculate the total ap gain from a list of pairs ('aсtion name', number)
def calc(actions):
return sum([ACTIONS[a] * c for (a, c) in actions])
# print the list of actions in a readable way
def show(actions):
res = 0
for action in actions:
print("%s x%d" % action)
res += ACTIONS[action[0]] * action[1]
print("Total:", res)
# list of minimal sets of actions that give a number of AP with required last digit
LAST_DIGITS = [
[], # 0 - nothing
[('create link', 2), ('upgrade', 1)], # 691
[('destroy link', 1), ('destroy resonator', 1)], # 262
[('create link', 1)], # 313
[('destroy link', 2), ('destroy resonator', 1), ('upgrade', 1)], #514
[('upgrade', 1)], # 65
[('create link', 2)], # 626
[('destroy link', 1), ('destroy resonator', 1), ('upgrade', 1)], #327
[('create link', 1), ('upgrade', 1)], #378
[('destroy link', 2), ('destroy resonator', 1)] # 449
]
if __name__ == '__main__':
# One argument - target number. Two arguments - "from" and "to". Everything else is an error.
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: %s <ap to get> OR %s <start ap> <end ap>" % (sys.argv[0], sys.argv[0]))
sys.exit(1)
if len(sys.argv) == 2:
start, end = 0, int(sys.argv[1])
else:
start, end = int(sys.argv[1]), int(sys.argv[2])
# How much AP we want to gain
diff = end - start
# First we want to have the required last digit
last_digit = diff % 10
minimum = calc(LAST_DIGITS[last_digit])
if diff < minimum:
print("Impossible")
sys.exit(1)
out = LAST_DIGITS[last_digit].copy()
diff -= minimum
# the rest is now a multiple of 10, so we have some freedom now
# below is the list of actions we are willing to perform in order of decreasing AP gain
# 'recharge' is the last to guarantee we do get the required amount of AP after all
for action in ['capture enemy portal', 'hack enemy portal', 'recharge']:
ap = ACTIONS[action] # ap gain of action
n = diff // ap # how much times we can do that without overflowing
if n: # if at least one, add it to result
out.append((action, n))
diff -= n * ap
show(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment