CleverWebAdmin

Riddle: Bit Play #1

Jul 7th, 2014
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This is a PHP riddle, the object of the game is to guess the correct answer
  4.  * based on the code provided, WITHOUT looping through all the possible answers.
  5.  * The code can be tricky to read, however is completely valid.
  6.  *
  7.  * As you decipher the code below, there will be various hints to help you
  8.  * determine the answer. Example: the code may require an answer with a specific
  9.  * number of caracters.
  10.  *
  11.  *
  12.  *
  13.  * RULES:
  14.  *  - You may run this through the PHP compiler up to 3 times before you are
  15.  *    "wrong"
  16.  *  - You may use an online luhn calculator, and php.net; but no other sites
  17.  *  - You may not add any valid code to this file (comments and whitespace
  18.  *    characters are welcome)
  19.  *  - HARDCORE MODE: Both the internet and localhost are off limits for anything
  20.  *    but confirming your answer
  21.  *  - HARDCORE MODE: You only get 2 tries to get it "right"
  22.  */
  23.  
  24.  
  25. /**
  26.  * Riddle: Bit Play #1
  27.  * -------------------
  28.  * This riddle may move, add, remove, compair, and evaluate bit values. It is
  29.  * suggested that you brush up on your binary and bitwise operators before
  30.  * starting.
  31.  */
  32. $answer = 0; // put your answer here and run to check if you are right
  33.  
  34.  
  35. // The Setup
  36. function luhn($number) {
  37.     settype($number, 'string');
  38.     $sumTable = array(
  39.         array(0,1,2,3,4,5,6,7,8,9),
  40.         array(0,2,4,6,8,1,3,5,7,9));
  41.     $sum = 0;
  42.     $flip = 0;
  43.     for ($i = strlen($number) - 1; $i >= 0; $i--) {
  44.         $sum += $sumTable[$flip++ & 0x1][$number[$i]];
  45.     }
  46.     return $sum % 10 === 0;
  47. }
  48. settype($answer, 'integer');
  49. $s = (string) $a = &$answer;
  50. $ns = (string) $n = (isset($s[1])) ? ($a >> 1)+$s[1] : ($a >> 1)+$s[0];
  51. $f = ((int) $ns[0] ^ (int) $s[1]);
  52.  
  53. // The Riddle
  54. function riddle($a, $n, $s, $f, $ns) {
  55.     if (
  56.         ($a != 0 && luhn($a)) &&
  57.         (!$f) &&
  58.         (($n | $a) == 63) &&
  59.         (!isset($s[2]))
  60.     ) {
  61.         return TRUE;
  62.     }
  63.     return FALSE;
  64. }
  65.  
  66. // The Result
  67. if (riddle($a, $n, $s, $f, $ns)) {
  68.     echo "You got the correct answer! ($a)";
  69. } else {
  70.     echo "Sorry, wrong answer. ($a)";
  71. }
Advertisement
Add Comment
Please, Sign In to add comment