#P1949. Efficient Password Entry
Efficient Password Entry
Efficient Password Entry
A secret typing challenge: given an initial 6-digit password and a target 6-digit password, compute the minimum number of key presses required using a special keyboard.
The keyboard has only six keys: swap0
, swap1
, up
, down
, left
, and right
. The six positions of the password are numbered from 1 to 6 (from left to right). Their functions are defined as follows:
- swap0: Pressing this key swaps the digit at the current cursor position with the digit in position 1. (If the cursor is at position 1 then no change occurs.)
- swap1: Pressing this key swaps the digit at the current cursor position with the digit in position 6. (If the cursor is at position 6 then no change occurs.)
- up: Pressing this key increases the digit at the current cursor position by 1, unless it is 9. In LaTeX: if the digit is $d$ with $d<9$, then it becomes $d+1$.
- down: Pressing this key decreases the digit at the current cursor position by 1, unless it is 0.
- left: Moves the cursor one position to the left (if not already at position 1).
- right: Moves the cursor one position to the right (if not already at position 6).
Before each password entry, a random 6-digit password appears and the cursor is positioned at location 1. By using the available keys an appropriate number of times, one may transform the initial password into the target password. The final cursor position is not important. Your task is to determine the minimum number of key presses needed to achieve the target password.
inputFormat
The input consists of two lines. The first line contains the initial 6-digit password (possibly with leading zeros). The second line contains the target 6-digit password.
outputFormat
Output a single integer representing the minimum number of key presses required to transform the initial password into the target password.
sample
123456
123456
0