Skip to content

Instantly share code, notes, and snippets.

@wolph
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolph/5bb7cb146d8bd7d09d70 to your computer and use it in GitHub Desktop.
Save wolph/5bb7cb146d8bd7d09d70 to your computer and use it in GitHub Desktop.
Solver for "math with sticks" puzzles
import re
sticks = {
0: 6,
1: 2,
2: 5,
3: 5,
4: 4,
5: 5,
6: 6,
7: 3,
8: 7,
9: 6,
}
add_stick = {
0: (8,),
1: (7,),
3: (9,),
5: (6, 9),
6: (8,),
9: (8,),
}
move_stick = {
0: (6, 9),
2: (3,),
3: (2, 5),
5: (3,),
6: (0, 9),
9: (0, 6),
}
remove_stick = {
6: (5,),
7: (1,),
8: (0, 6, 9),
9: (3, 5),
}
def get_options(numbers):
numbers = map(int, '%04s' % numbers)
for i, a in enumerate(numbers):
for option in move_stick.get(a, []):
out = numbers[:]
out[i] = option
yield 'move %d' % i, ''.join(map(str, out))
for option_a in add_stick.get(a, []):
for j, b in enumerate(numbers):
if i == j:
continue
for option_b in remove_stick.get(b, []):
out = numbers[:]
out[i] = option_a
out[j] = option_b
yield 'add %s/%d, remove %s/%d' % (a, i, b, j), ''.join(
map(str, out))
while True:
expression = raw_input('Please input number:')
number = re.sub('[^0-9]', '', expression)
operator = re.sub('[0-9]', '', expression)
for operation, option in get_options(number):
a = ''.join((
option[0],
operator[0],
option[1],
))
b = ''.join((
option[2],
operator[2],
option[3],
))
try:
if eval(a + '.') == eval(b + '.'):
print operation,
out = ''.join((
option[0],
operator[0],
option[1],
operator[1],
option[2],
operator[2],
option[3],
))
print out
except ZeroDivisionError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment