Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
r/Dailyprogrammer Challenge #200 Intermediate
<?php
/*
* r/Dailyprogammer Challenge #200 Intermediate
* coded by Michael Repper
* query [ a * t ] gridoodle [ d * o * t ] com
*/
function print_results($tile)
{
echo $tile[2][0] . "x" . $tile[2][1] . " tile of character '" . $tile[0] . "' located at (" . $tile[1][0] . ',' . $tile[1][1] . ")<br />";
}
function count_tiles($tile_size)
{
$file_name = 'tile_' . $tile_size[0] . '_' . $tile_size[1] . '.txt';
$this_file = file_get_contents($file_name);
$this_grid = array_map('str_split', explode("\n", $this_file));
$corners = [];
for ($i = 0; $i < count($this_grid); $i++)
{
for ($j = 0; $j < count($this_grid[$i]); $j++)
{
$this_char = $this_grid[$i][$j];
if ($this_char !== '.')
{
if(($j - 1 < 0 || $this_grid[$i][$j - 1] === '.') && ($i - 1 < 0 || $this_grid[$i - 1][$j] === '.'))
{
$corners['top_left'][$this_char] = [$j, $i];
}
if (($j + 1 > $tile_size[0] - 1 || $this_grid[$i][$j + 1] === '.') && ($i + 1 > $tile_size[1] - 1 || $this_grid[$i + 1][$j] === '.'))
{
if (array_key_exists($this_char, $corners['top_left']))
{
$top_left = $corners['top_left'][$this_char];
$size = [$j - $top_left[0] + 1, $i - $top_left[1] + 1];
print_results([$this_char, $top_left, $size]);
unset($corners['top_left'][$this_char]);
}
}
}
}
}
}
$grid_sizes = [ [74, 30], [4, 4], [10, 10] ];
foreach ($grid_sizes as $grid)
{
count_tiles($grid);
echo "<br />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.