Skip to content

Instantly share code, notes, and snippets.

@alexras
Created February 3, 2014 04:58
Show Gist options
  • Select an option

  • Save alexras/8779037 to your computer and use it in GitHub Desktop.

Select an option

Save alexras/8779037 to your computer and use it in GitHub Desktop.
Quick script to generate good moves in Calculords
#!/usr/bin/env python
import sys, argparse, itertools
operators = [
('x', lambda x, y: x * y),
('+', lambda x, y: x + y),
('-', lambda x, y: x - y)]
def calculords_recurse(card, numbers, stack):
if len(numbers) == 1:
if numbers[0] == card:
for operation in stack:
print ' '.join(map(str, operation))
return True
else:
return False
for i, j in itertools.combinations(xrange(len(numbers)), 2):
first = numbers[i]
second = numbers[j]
new_numbers = []
for index, number in enumerate(numbers):
if index != i and index != j:
new_numbers.append(number)
for symbol, operator in operators:
new_numbers.append(operator(first, second))
stack.append((first, symbol, second, new_numbers))
if calculords_recurse(card, new_numbers, stack):
return True
else:
new_numbers.pop()
stack.pop()
def calculords(card, numbers):
numbers = map(int, numbers.split(','))
return calculords_recurse(card, numbers, [])
def main():
parser = argparse.ArgumentParser(description="solve Calculords puzzle")
parser.add_argument(
'card', help="the cost of the card you're trying to play", type=int)
parser.add_argument(
'numbers', help='comma-delimited list of available numbers')
args = parser.parse_args()
return calculords(**vars(args))
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment