Skip to content

Instantly share code, notes, and snippets.

@jeromefitzpatrick
Last active March 12, 2021 15:08
Show Gist options
  • Save jeromefitzpatrick/ce6686f92e87288a293ccd2ddef4c84d to your computer and use it in GitHub Desktop.
Save jeromefitzpatrick/ce6686f92e87288a293ccd2ddef4c84d to your computer and use it in GitHub Desktop.
<?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