<?php
$sInput = 'turn on 0,0 through 999,999';
$aLights = array();
for($x = 0; $x < 1000; $x++) {
for($y = 0; $y < 1000; $y++) {
$aLights[$x . 'x' . $y] = false;
}
}
$aInput = explode("\r\n", $sInput);
foreach($aInput as $sInput) {
preg_match('#(?<action>turn (?:on|off)|toggle) (?<from_x>[0-9]{1,3}),(?<from_y>[0-9]{1,3}) through (?<to_x>[0-9]{1,3}),(?<to_y>[0-9]{1,3})#', $sInput, $aMatch);
for($x = $aMatch['from_x']; $x <= $aMatch['to_x']; $x++) {
for($y = $aMatch['from_y']; $y <= $aMatch['to_y']; $y++) {
switch($aMatch['action']) {
case 'turn on':
$aLights[$x . 'x' . $y] = true;
break;
case 'turn off':
$aLights[$x . 'x' . $y] = false;
break;
case 'toggle':
$aLights[$x . 'x' . $y] = ($aLights[$x . 'x' . $y] ? false : true);
break;
}
}
}
}
echo count(array_filter($aLights));
preferences:
54.66 ms | 402 KiB | 5 Q