Skip to content

Instantly share code, notes, and snippets.

@mspreij
Created December 3, 2018 18:34
Show Gist options
  • Save mspreij/6248c9685510b7a769e30d52105c7628 to your computer and use it in GitHub Desktop.
Save mspreij/6248c9685510b7a769e30d52105c7628 to your computer and use it in GitHub Desktop.
$input = explode("\n", file_get_contents('3.txt'));
$count = count($input);
echo $count. " inputs\n";
foreach ($input as $i => $line) {
preg_match('/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/', $line, $m);
$rects[$m[1]] = ['x'=>$m[2], 'y'=>$m[3], 'x2'=>$m[2]+$m[4], 'y2'=>$m[3]+$m[5]];
}
$filled = [];
$unsets = array_fill(1, 1309, 1);
for ($i=1; $i <= $count-1; $i++) {
$rect1 = $rects[$i];
for ($j=$i+1; $j <= $count; $j++) {
$rect2 = $rects[$j];
if ($rect1['x']<$rect2['x2'] and $rect1['x2']>$rect2['x'] and $rect1['y']<$rect2['y2'] and $rect1['y2']>$rect2['y']) {
$overlap = [
'x' => max($rect1['x'], $rect2['x']),
'x2' => min($rect1['x2'], $rect2['x2']),
'y' => max($rect1['y'], $rect2['y']),
'y2' => min($rect1['y2'], $rect2['y2']),
];
for ($ox=$overlap['x']; $ox < $overlap['x2']; $ox++) {
for ($oy=$overlap['y']; $oy < $overlap['y2']; $oy++) {
$filled[$ox.'_'.$oy] = 1;
}
}
unset($unsets[$i], $unsets[$j]);
}
}
}
echo count($filled);
printDebug($unsets);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment