Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2.  
  3. require_once './vendor/autoload.php';
  4.  
  5. use Symfony\Component\HttpClient\HttpClient;
  6.  
  7. function prime($count)
  8. {
  9.     if ($count < 1) {
  10.         return false;
  11.     }
  12.     $primes = [];
  13.     $i = 2;
  14.     while (count($primes) < $count) {
  15.         foreach ($primes as $prime) {
  16.             if (0 == $i % $prime) {
  17.                 ++$i;
  18.  
  19.                 continue 2;
  20.             }
  21.         }
  22.         $primes[] = $i++;
  23.     }
  24.  
  25.     return end($primes);
  26. }
  27.  
  28. function flatten(array $array)
  29. {
  30.     $return = [];
  31.     array_walk_recursive($array, function ($a) use (&$return) { $return[] = $a; });
  32.  
  33.     return $return;
  34. }
  35.  
  36. function answer($client, $token, $qid, $answer)
  37. {
  38.     $response = $client->request(
  39.         'GET',
  40.         "http://hackathon.internal.ats-digital.com:3334/api/nuggets/{$qid}/claim?trigger={$answer}",
  41.         [
  42.             'headers' => [
  43.                 'Authorization' => 'Bearer '.$token,
  44.             ],
  45.         ]
  46.     );
  47.  
  48.     var_dump($response->getContent());
  49. }
  50.  
  51. $AUTH_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGNlYjQ2YmMwM2JkOTY5MzEwZjk4MGMiLCJpYXQiOjE1NzM4NDEzNDF9.r63B4_p4sk2WDi0fAtXrsFa8S5UDplosq0NiZUYJjw0';
  52.  
  53. $client = HttpClient::create();
  54. $response = $client->request(
  55.     'GET',
  56.     'http://hackathon.internal.ats-digital.com:3334/api/nuggets/discover',
  57.     [
  58.         'headers' => [
  59.             'Authorization' => 'Bearer '.$AUTH_TOKEN,
  60.         ],
  61.     ]
  62. );
  63.  
  64. $questions = json_decode($response->getContent(), true);
  65.  
  66. $primeQuestions = array_filter($questions, function ($question) {
  67.     return 'PRIME' === $question['type'];
  68. });
  69.  
  70. foreach ($primeQuestions as $question) {
  71.     sleep(4);
  72.     preg_match_all('!\d+!', $question['hint'], $matches);
  73.     $results = flatten($matches);
  74.     $number = intval(reset($results));
  75.     $answer = prime($number);
  76.     $id = $question['_id'];
  77.  
  78.     echo 'Answering '.$question['hint'].' with '.$answer;
  79.  
  80.     try {
  81.         answer($client, $AUTH_TOKEN, $question['_id'], prime($number));
  82.     } catch (\Throwable $th) {
  83.         echo $th->getMessage();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement