35
4
Introduction
Clarence is a data entry clerk who works at an internet service provider. His job is to manually enter the IP addresses of all of the ISP's customers into the database. He does this using a keypad which has the following layout:
1
2
3
4
5
6
7
8
9
.
0
The distance between the centre of horizontally or vertically adjacent keys is exactly one centimetre. For instance, the distance between the centres of 3 and 9 would be two centimetres. The distance between the centres of 3 and 5 would be √2cm. The Pythagoras theorem is sufficient to calculate the distance between any two keys.
Clarence, as you might expect from one who works in an ISP, uses a very slow and inefficient system of typing. He uses a single finger and searches for the key, and then moves his finger to the key, then presses it, and repeats for all of the digits in the number. You might know of this style as the "eagle search system" since the finger searches above the keyboard for the correct key before plunging down for the keypress, like an eagle plunging down for a kill.
For example, here is how Clarence would type out the number 7851
:
- He starts his finger at 7 and pushes the key.
- He moves his finger to the right 1cm to 8 and pushes the key.
- He moves his finger upwards 1cm to 5 and pushes the key.
- He moves his finger diagonally upwards and left √2cm to 1 and pushes the key.
Therefore the total distance that Clarence moved his finger to type in 7851
is 1 + 1 + √2
which is about 3.41cm.
Your task is to write a program that calculates the distance Clarence must move his finger to type in arbitrary IP addresses.
Input Description
Input is a string that will be in the form
().().().()
where each ()
is an integer in the range 0
- 999
. This represents the IP address that Clarence must type in. An example input might be:
219.45.143.143
I would also like to point out that inputs such as 0.42.42.42
or 999.999.999.999
are still valid inputs, despite the fact that they are invalid IP addresses. So you don't need to include any IP address verification code in your program.
Output Description
Output the distance that Clarence must move his finger in order to type in the specified IP address. Round answers to two decimal places where needed, and use the cm
unit in your output. The output for the example input is 27.38cm
(1 + √8 + √5 + 2 + 1 + √5 + 3 + 1 + √5 + √13 + 3 + 1 + √5).
29Man, ISPs have weird keyboards... – Dennis – 2015-05-25T02:15:11.517
I wonder if anyone's entry will actually make use of the dotted quad format, or if most/all will just accept any arbitrary string of [0-9.]+ – Sparr – 2015-05-25T03:20:51.943
@Sparr: At least it excludes the possibility of a one- character input, which would be real pain in CJam. – Dennis – 2015-05-25T05:30:28.297
Can you clarify what forms of input are allowed? Since you say "program", does this imply that you expect a full program that takes input from stdin or command line arguments? Or is a function that takes a string as argument also acceptable? – Reto Koradi – 2015-05-25T05:57:08.430
1@RetoKoradi I expect a program, yes. stdin, command line arguments, or user input functions are acceptable. – absinthe – 2015-05-25T05:59:07.813
2@dacapoaria - 'eagle search' is also known as 'hunt and peck' or 'search and destroy' for the more heavy-handed typists. – None – 2015-05-25T09:51:33.733
1@MathiasFoster Search and Destroy typing gives me an interesting mental image. Thanks for telling me these different terms. – absinthe – 2015-05-25T10:45:45.017
Why 0-999 when IPs can only be 0-255? – ArtOfCode – 2015-05-25T11:50:29.213
12@ArtofCode Clarence works at an ISP, and sometimes the ISP sends him the invalid data to type into the database. Clarence types the data in anyway. That's the canonical reason anyway. The actual reason is because I overlooked that when writing the spec. – absinthe – 2015-05-25T11:59:29.750
3Considering only the valid range (0-255) IP Addresses, which should be the optimum arrangement of the keyboard to type all that addresses in the shortest path? – Israel Morales – 2015-05-25T17:18:26.000
1@IsraelMorales Hmmm... my hunch would be that you'd have the
.
in the middle,2
,5
,1
, and3
adjacent,9
and8
outside of the square and the remaining ones in the corners. That's just my guess though. – absinthe – 2015-05-26T00:25:23.953