This year’s Isle of Man TT Race is going to be around Douglas and your job is to find the exact coordinates for it and the names of the racers. Every racer starts from a different place. You’re going to receive the coordinates in the form of a geohash code.
Write a program that decrypts messages. You’re going to receive a few notes that contain the following information:
- Name of racer: Consists only of letters. It is surrounded from the both sides by any of the following symbols – "#, $, %, *, &". Both symbols – in the beginning and at the end of the name should match.
- Length of geohashcode: Begins after the "=" sign and it is consisted only of numbers.
- Encrypted geohash code: Begins after these symbols - “!!”. It may contain anything and the message always ends with it.
Examples for valid input:
#SteveHislop#=16!!tv5dekdz8x11ddkc
Examples of invalid input:
%GiacomoAgostini$=7!!tv58ycb – The length is the same, but the name is not surrounded by matching signs.
$GeoffDuke$=6!!tuvz26n35dhe4w4 – The length doesn't match the lengh of the code.
&JoeyDunlop&!!tvndjef67t=14 – The length should be before the code.
The information must be in the given order, otherwise it is considered invalid. The geohash code you are looking for is with length exactly as much as the given length in the message. To decrypt the code you need to increase the value of each symbol from the geohashcode with the given length. If you find a match, you have to print the following message:
"Coordinates found! {nameOfRacer} -> {geohashcode}"
and stop the program. Otherwise, after every invalid message print:
"Nothing found!"
Input / Constraints:
- You will be receiving strings.
- There will always be a valid message.
Output:
- If you find the right coordinates, print: "Coordinates found! {nameOfRacer} -> {geohashcode}".
- Otherwise, print: "Nothing found!".
Examples:
Here is my solution:
<?php $input = readline(); $pattern = '/^([\#\$\%\&\*])(?<name>[a-zA-Z]+)(\1)(\=)(?<length>\d+)(\!\!)(?<geohash>.*)$/m'; while (true) { preg_match($pattern, $input, $matches); if (isset($matches["name"]) && isset($matches["length"]) && isset($matches["geohash"]) && (strlen($matches["geohash"]) == $matches["length"])) { $geohash = $matches["geohash"]; $decryptedGeoHash = ""; for ($i = 0; $i < strlen($geohash); $i++) { $letter = ord($geohash[$i]) + strlen($geohash); $decryptedGeoHash .= chr($letter); } echo "Coordinates found! " . $matches["name"] . " -> " . $decryptedGeoHash; break; } else { echo "Nothing found!" . PHP_EOL; } $input = readline(); }