I can prove that the upperbound of the number of rotations is 8. As previously noted, rotating A, D, F, I
rotates all gears exactly once. I'll prove that A+D+F+I <= 2
. By pigeonhole principle, since there are 4 variables and 3 values (0, 1, 2), there are one value that is assumed by two of the variables. If it is 0, then two of the gears do not need rotation. So maximum 7 gears need rotations + maximum 1 from global rotation, so maximum 8 rotations. If it is not 0, then we can make them 0 by changing the global rotation direction. So maximum 8 rotations.
I haven't founddon't know (so far) how to prove that a configuration that requires at least 8 rotations, so it might be that all solvable configuration can be solved in 7 rotations maximum. One example of initial state that my solution says it requires 8 rotations is this:
2 2 2 1 2 0 2 0 2
You might notice that the state e
is missing from the solution. This is because knowing a
, c
, f
, h
, and i
gives a unique e
for which there is a solution, and so the state of e
is immaterial to our solution that already uses the other 8 states.
Code to calculate
Below I give the code in Python to calculate the solution as above, including the heuristics to minimize the number of rotations.
"""
21 Nov 2016
To solve the GEAR puzzle at puzzlopia (if not logged in)
"""
# Import statements
from builtins import input
from itertools import product
def count_zero(arr):
return sum(1 if val==0 else 0 for val in arr)
def add_one(arr):
return [(val+1)%3 for val in arr]
def compute(*vals):
a,b,c,d,e,f,g,h,i = vals
A = ( a-b+c +g-h-i+9)%3
B = ( a+b-c -g+h+i+9)%3
C = (-a+b+c-d +9)%3
D = ( d +g-h-i+9)%3
E = ( d -g+h+i+9)%3
F = ( a-b-c-d -f-g-h+i+9)%3
G = ( -i+9)%3
H = ( -d +g+h-i+9)%3
I = (0 +9)%3
arr = [A,D,F,I]
if count_zero(arr) < 2:
arr = add_one(arr)
if count_zero(arr) < 2:
arr = add_one(arr)
A,D,F,I = arr
return (A,B,C,D,E,F,G,H,I)
def main():
a, b, c, d, e, f, g, h, i = map(int, input('Enter values:').split()) #(0, 1, 0, 1, 0, 1, 0, 1, 0)
A,B,C,D,E,F,G,H,I = compute(a,b,c,d,e,f,g,h,i)
print('A={}\nB={}\nC={}\nD={}\nE={}\nF={}\nG={}\nH={}\nI={}'.format(A,B,C,D,E,F,G,H,I))
if __name__ == '__main__':
main()