Skip to content

Instantly share code, notes, and snippets.

@rmeertens
Created December 23, 2018 20:01
Show Gist options
  • Save rmeertens/71da6b32a0b1d00e649c72096e0892a4 to your computer and use it in GitHub Desktop.
Save rmeertens/71da6b32a0b1d00e649c72096e0892a4 to your computer and use it in GitHub Desktop.
Wanted to know what lego technic gears I needed to make something rotate once a year... guess I found it :)
from collections import defaultdict
def get_gears_for_ratio(ratio, gears, maxdepth=10):
shortestpath = dict()
shortestpath[1] = list()
expanded = defaultdict(lambda: False)
depth = 0
while ratio not in shortestpath.keys():
depth +=1
if depth > 14:
return False, shortestpath
for startratio in list(shortestpath.keys()):
if expanded[startratio]:
continue
expanded[startratio] = True
path = shortestpath[startratio]
for front in gears:
for back in gears:
reached = startratio * (back/front)
if reached not in shortestpath:
shortestpath[reached] = path + [(front, back)]
return True, shortestpath
lego_technic_gears = [8, 12, 14, 16, 20, 24, 36, 40]
searching = 3.5
foundit, dictionary = get_gears_for_ratio(3.5, lego_technic_gears, maxdepth=5)
if foundit:
print(dictionary[searching])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment