Given a lock made up of N different circular rings. Each ring has 0-9 digit printed on it. There is only one particular code which can open the lock. You can rotate each ring any number of times in either direction. Given the random sequence R and the desired sequence D, find the minimum number of rotations required to open the lock. 
 
Example 1:
Input: R = 222, D = 333
Output: 3
Explaination: Optimal number of rotations for 
getting 3 from 2 is 1. There are three 2 to 3 
transformations. So answer is 1+1+1 = 3.
 
Example 2:
Input: R = 2345, D = 5432
Output: 8
Explaination: The optimal shifts for pairs are: 
(2, 5) = 3, (3, 4) = 1, (4,3) = 1, (5,2) = 3. 
So total shifts = 3+1+1+3 = 8.
 
Your Task:
You do not need to read input or print anything. Your task is to complete the function rotationCount() which takes R and D as input parameters and return the minimum number of rotations required to make R = D.
 
Expected Time Complexity: O(logR)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ R, D < 10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def rotationCount(self, R, D):

        # code here

Solutions:
class Solution:

	def rotationCount(self, R, D):
		r = str(R)
		d = str(D)
		c = 0
		n = len(r)
		for i in range(n):
			R = int(r[i])
			D = int(d[i])
			c1 = abs(R - D)
			c2 = abs(R - (D + 10))
			c3 = abs(R + 10 - D)
			c += min(c1, c2, c3)
		return c


class Solution:

	def rotationCount(self, R: int, D: int):
		arr1 = [int(d) for d in str(R)]
		arr2 = [int(d) for d in str(D)]
		nos = 0
		for i in range(len(arr1)):
			if arr1[i] != 0 and arr2[i] != 0:
				no1 = abs(int(arr1[i]) - int(arr2[i]))
				nos += min(no1, abs(10 - no1))
			elif arr1[i] == 0:
				nos = int(nos) + min(abs(0 - int(arr2[i])), abs(10 - int(arr2[i])))
			elif arr2[i] == 0:
				nos = int(nos) + min(abs(0 - int(arr1[i])), abs(10 - int(arr1[i])))
		return nos


class Solution:

	def rotationCount(self, R, D):
		(rList, dList) = (list(str(R)), list(str(D)))
		res = []
		for (a, b) in zip(rList, dList):
			distance = abs(int(a) - int(b))
			distance = distance % 10
			res.append(min(distance, 10 - distance))
		return sum(res)


class Solution:

	def rotationCount(self, R, D):
		res = 0
		while R > 0:
			rDigit = R % 10
			dDigit = D % 10
			res += min(abs(rDigit - dDigit), 10 - abs(rDigit - dDigit))
			R //= 10
			D //= 10
		return res


class Solution:

	def rotationCount(self, R, D):
		count = 0
		while R > 0:
			ele1 = R % 10
			ele2 = D % 10
			count = count + min(abs(ele1 - ele2), 10 - abs(ele1 - ele2))
			R = R // 10
			D = D // 10
		return count


class Solution:

	def rotationCount(self, R, D):
		cnt = 0
		rem1 = 0
		rem2 = 0
		while R > 0 and D > 0:
			rem1 = R % 10
			rem2 = D % 10
			cnt += min(abs(rem1 - rem2), 10 - abs(rem1 - rem2))
			R = R // 10
			D = D // 10
		return cnt


class Solution:

	def rotationCount(self, R, D):
		i = R % 10
		j = D % 10
		total = 0
		R //= 10
		D //= 10
		while i > 0 and j > 0:
			total += min(abs(i - j), 10 - abs(i - j))
			i = R % 10
			j = D % 10
			R //= 10
			D //= 10
		return total


class Solution:

	def rotationCount(self, input, op):
		return sum([min(abs(int(str(input)[i]) - int(str(op)[i])), 10 - abs(int(str(input)[i]) - int(str(op)[i]))) for i in range(len(str(op)))])


class Solution:

	def rotationCount(self, R, D):
		cnt = 0
		while R > 0 and D > 0:
			x = R % 10
			y = D % 10
			x = abs(x - y)
			if x > 5:
				x = 10 - x
			cnt += x
			R //= 10
			D //= 10
		return cnt


class Solution:

	def rotationCount(self, r, d):
		r = str(r)
		d = str(d)
		op = 0
		for i in range(len(r)):
			a = abs(int(r[i]) - int(d[i]))
			if a > 5:
				op += 10 - a
			else:
				op += a
		return op


class Solution:

	def rotationCount(self, r, d):
		a = str(r)
		b = str(d)
		sum1 = 0
		i = 0
		j = 0
		sum1 = 0
		while i < len(a):
			while j < len(b):
				if int(a[i]) > int(b[j]):
					if abs(int(a[i]) - int(b[j])) > abs(int(a[i]) - (int(b[j]) + 10)):
						c = abs(int(a[i]) - (int(b[j]) + 10))
						sum1 += c
						j += 1
						i += 1
						continue
					else:
						c = abs(int(a[i]) - int(b[j]))
						sum1 += c
						j += 1
						i += 1
						continue
				elif abs(int(a[i]) - int(b[j])) > abs(int(a[i]) + 10 - int(b[j])):
					c = abs(int(a[i]) + 10 - int(b[j]))
					sum1 += c
					j += 1
					i += 1
					continue
				else:
					c = abs(int(a[i]) - int(b[j]))
					sum1 += c
					j += 1
					i += 1
					continue
			break
		return sum1