Created
August 4, 2021 21:00
-
-
Save guillaumebdx/d6b165df2db459a89de068b35417d3d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//require 'dataset.php'; | |
$data = [42, 88, 100, 103, 112, 115, 124, 161, 170, 185]; | |
$possibilities = createPossibilities($data); | |
$result = getResult($possibilities, $data); | |
var_dump($result); | |
//output array(5) { | |
// [0]=> | |
// int(15) | |
// [1]=> | |
// int(27) | |
// [2]=> | |
// int(73) | |
// [3]=> | |
// int(88) | |
// [4]=> | |
// int(97) | |
//} | |
function createPossibilities(array $data): array | |
{ | |
$possibilities = []; | |
for ($k=0; $k<count($data); $k++) { | |
for ($i=1; $i<=100; $i++) { | |
for ($j=1; $j<=100; $j++) { | |
if ($i !== $j && $i + $j === $data[$k]) { | |
$possibilities[$data[$k]][] = [$i, $j]; | |
} | |
} | |
} | |
} | |
return $possibilities; | |
} | |
function getResult(array $possibilities, array $data): ?array | |
{ | |
foreach ($possibilities[$data[0]] as $key => $paires) { | |
foreach ($possibilities as $secondPaires) { | |
foreach ($secondPaires as $pairesToMerge) { | |
for ($i=1; $i<=100; $i++) { | |
$probables = array_merge($paires, $pairesToMerge, [$i]); | |
if (isCorrect($data, $probables)) { | |
return $probables; | |
} | |
} | |
} | |
} | |
} | |
return null; | |
} | |
function isCorrect(array $inputs, array $probables): bool | |
{ | |
$find = 0; | |
foreach ($inputs as $number) { | |
if (isThereAPair($probables, $number)) { | |
$find += 1; | |
} | |
} | |
return $find === count($inputs); | |
} | |
function isThereAPair(array $probables, int $number): bool | |
{ | |
for ($i=0; $i<count($probables); $i++) { | |
for ($j=0; $j<count($probables); $j++) { | |
if ($probables[$i] + $probables[$j] === $number) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment