Programming contests

Magas szintű programozási nyelvek 1, egyéni verseny, 2015. április 12.

April 12, 2015 10:00 AM – April 12, 2015 3:00 PM

Combination Lock

Now that you're back to school for another term, you need to remember how to work the combination lock on your locker. A common design is that of the Master brand, shown below.

The lock has a dial with 40 calibration marks numbered 0 to 39. A combination consists of 3 of these numbers; for example: 15–25–8. To open the lock, the following steps are taken:

  • turn the dial clockwise two full turns,
  • stop at the first number of the combination,
  • turn the dial counterclockwise one full turn,
  • continue turning counterclockwise until the 2nd number is reached,
  • turn the dial clockwise again until the 3rd number is reached,
  • pull the shank and the lock will open.

Given the initial position of the dial and the combination for the lock, how many degrees is the dial rotated in total (clockwise plus counterclockwise) in opening the lock?

Input Specification

The input consists of several test cases. For each case, there is a line of input containing 4 integers between 0 and 39. The first number is the position of the dial. The next three numbers are the combination. Consecutive numbers in the combination will be distinct. A line containing 0 0 0 0 follows the last case.

Output Specification

For each case, print a line with a single integer: the number of degrees that the dial must be turned to open the lock.

Sample Input

  1. 0 30 0 30
  2. 5 35 5 35
  3. 0 20 0 20
  4. 7 27 7 27
  5. 0 10 0 10
  6. 9 19 9 19
  7. 0 0 0 0
download as text file

Output for Sample Input

  1. 1350
  2. 1350
  3. 1620
  4. 1620
  5. 1890
  6. 1890
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019