Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once './vendor/autoload.php';
- use Symfony\Component\HttpClient\HttpClient;
- function prime($count)
- {
- if ($count < 1) {
- return false;
- }
- $primes = [];
- $i = 2;
- while (count($primes) < $count) {
- foreach ($primes as $prime) {
- if (0 == $i % $prime) {
- ++$i;
- continue 2;
- }
- }
- $primes[] = $i++;
- }
- return end($primes);
- }
- function flatten(array $array)
- {
- $return = [];
- array_walk_recursive($array, function ($a) use (&$return) { $return[] = $a; });
- return $return;
- }
- function answer($client, $token, $qid, $answer)
- {
- $response = $client->request(
- 'GET',
- "http://hackathon.internal.ats-digital.com:3334/api/nuggets/{$qid}/claim?trigger={$answer}",
- [
- 'headers' => [
- 'Authorization' => 'Bearer '.$token,
- ],
- ]
- );
- var_dump($response->getContent());
- }
- $AUTH_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGNlYjQ2YmMwM2JkOTY5MzEwZjk4MGMiLCJpYXQiOjE1NzM4NDEzNDF9.r63B4_p4sk2WDi0fAtXrsFa8S5UDplosq0NiZUYJjw0';
- $client = HttpClient::create();
- $response = $client->request(
- 'GET',
- 'http://hackathon.internal.ats-digital.com:3334/api/nuggets/discover',
- [
- 'headers' => [
- 'Authorization' => 'Bearer '.$AUTH_TOKEN,
- ],
- ]
- );
- $questions = json_decode($response->getContent(), true);
- $primeQuestions = array_filter($questions, function ($question) {
- return 'PRIME' === $question['type'];
- });
- foreach ($primeQuestions as $question) {
- sleep(4);
- preg_match_all('!\d+!', $question['hint'], $matches);
- $results = flatten($matches);
- $number = intval(reset($results));
- $answer = prime($number);
- $id = $question['_id'];
- echo 'Answering '.$question['hint'].' with '.$answer;
- try {
- answer($client, $AUTH_TOKEN, $question['_id'], prime($number));
- } catch (\Throwable $th) {
- echo $th->getMessage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement