Last active
March 12, 2021 15:08
-
-
Save jeromefitzpatrick/ce6686f92e87288a293ccd2ddef4c84d 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 | |
namespace App; | |
/** | |
* Brute force version. Will add optimised version later. | |
*/ | |
class Block | |
{ | |
protected $blocks = [ | |
['school'], | |
['gym'], | |
['gym', 'school'], | |
['school'], | |
['school','store'] | |
]; | |
protected $needs = ['gym','school','store']; | |
public function bestBlock() | |
{ | |
$max = count($this->blocks); | |
$indexes = []; | |
foreach($this->blocks as $blockIndex => $blocks) { | |
$indexes[$blockIndex] = []; | |
foreach ($this->needs as $need) { | |
foreach ($this->blocks as $innerIndex => $innerBlocks) { | |
if (in_array($need, $innerBlocks)) { | |
$temp = $blockIndex - $innerIndex; | |
$temp = $temp < 0 ? $temp * -1 : $temp; | |
$indexes[$blockIndex][$need] = $indexes[$blockIndex][$need] ?? $max; | |
$indexes[$blockIndex][$need] = $temp < $indexes[$blockIndex][$need] ? $temp : $indexes[$blockIndex][$need]; | |
} | |
} | |
} | |
$indexes[$blockIndex] = max($indexes[$blockIndex]); | |
} | |
return array_keys($indexes, min($indexes))[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment