Morning (Codeforces 1883A)
Tags: Math, math

You are given a four-digit pin code consisting of digits from $$$0$$$ to $$$9$$$ that needs to be entered. Initially, the cursor points to the digit $$$1$$$. In one second, you can perform exactly one of the following two actions: Press the cursor to display the current digit, Move the cursor to any adjacent digit.  The image above shows the device you are using to enter the pin code. For example, for the digit $$$5$$$, the adjacent digits are $$$4$$$ and $$$6$$$, and for the digit $$$0$$$, there is only one adjacent digit, $$$9$$$.Determine the minimum number of seconds required to enter the given four-digit pin code.

Input: Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) - the number of the test cases. This is followed by their description.The single line of each test case describes the pin code as a string of length $$$4$$$, consisting of digits from $$$0$$$ to $$$9$$$.

Output: For each test case, output the minimum number of seconds required to enter the given pin code.

Note: In the first test case, the cursor needs to be pressed $$$4$$$ times.In the second test case, it can be done in $$$9$$$ seconds as follows: Press the cursor. Move the cursor to the digit $$$2$$$. Press the cursor. Move the cursor to the digit $$$3$$$. Press the cursor. Move the cursor to the digit $$$4$$$. Move the cursor to the digit $$$5$$$. Move the cursor to the digit $$$6$$$. Press the cursor.

Sample Inputs:
10
1111
1236
1010
1920
9273
0000
7492
8543
0294
8361

Sample Outputs:
4
9
31
27
28
13
25
16
33
24

Solution (Python):
import atexit
import os
import sys
from io import StringIO
from random import seed as _seed
_output = StringIO()
sys.stdin = StringIO(sys.stdin.read())
_readline = sys.stdin.readline
_write = _output.write
_output.write = lambda *args: _write(' '.join(map(str, args)) + '\n')
atexit.register(lambda : os.write(1, _output.getvalue().encode('utf-8')))
print = _output.write
input = lambda : _readline().rstrip('\r\n')
read = lambda : input()
read_int = lambda : int(input())
read_ints = lambda : [int(_) for _ in input().split()]

def solve():
    last = 1
    time = 4
    nums = [int(num) if num != '0' else 10 for num in read()]
    for num in nums:
        time += abs(num - last)
        last = num
    return time
if __name__ == '__main__':
    for _ in range(read_int()):
        print(solve())

Solution (Python):
import sys
input_data = sys.stdin.read()

def solution(input_data):
    lines = input_data.strip().split('\n')
    t = int(lines[0])
    test_cases = lines[1:]
    keypad = '1234567890'
    results = []
    for pin in test_cases:
        current_pos = keypad.index('1')
        seconds = 0
        for digit in pin:
            seconds += 1
            target_pos = keypad.index(digit)
            distance = abs(target_pos - current_pos)
            seconds += distance
            current_pos = target_pos
        results.append(seconds)
    output_data = '\n'.join(map(str, results))
    return output_data
print(solution(input_data))

Solution (Python):
import sys
input_data = sys.stdin.read()

def solution(input_data):
    lines = input_data.strip().split('\n')
    t = int(lines[0])
    test_cases = lines[1:]
    keypad = '1234567890'
    results = []
    for pin in test_cases:
        current_pos = keypad.index('1')
        seconds = 0
        for digit in pin:
            seconds += 1
            target_pos = keypad.index(digit)
            distance = abs(target_pos - current_pos)
            seconds += distance
            current_pos = target_pos
        results.append(seconds)
    output_data = '\n'.join(map(str, results))
    return output_data
print(solution(input_data))