Beispiel #1
0
function step($grid)
{
    $new = $grid;
    for ($j = 0; $j < 100; $j++) {
        for ($k = 0; $k < 100; $k++) {
            $on = countOn($grid, $j, $k);
            if ($grid[$j][$k] === 0 && $on === 3) {
                $new[$j][$k] = 1;
            } else {
                if ($grid[$j][$k] === 1 && $on != 3 && $on != 2) {
                    $new[$j][$k] = 0;
                }
            }
        }
    }
    return $new;
}
Beispiel #2
0
$file = file('input', FILE_IGNORE_NEW_LINES);
$zeroToThousand = range(0, 999);
$lights = array_fill_keys($zeroToThousand, array_fill_keys($zeroToThousand, 0));
foreach ($file as $cmd) {
    preg_match('/([a-z ]{4,9})([0-9]{1,3}\\,[0-9]{1,3}) through ([0-9]{1,3}\\,[0-9]{1,3})/', $cmd, $matches);
    array_shift($matches);
    list($command, $startCoords, $endCoords) = $matches;
    $command = trim($command);
    $startCoords = explode(',', $startCoords);
    $endCoords = explode(',', $endCoords);
    $lightsToSet = getCoordsBetweenInRectangle($startCoords, $endCoords);
    foreach ($lightsToSet as $light) {
        $lights[$light[0]][$light[1]] = whatSetTo($command, $light);
    }
}
echo countOn($lights) . PHP_EOL;
function getCoordsBetweenInRectangle($startCoords, $endCoords)
{
    $coords = [];
    for ($row = $startCoords[0]; $row <= $endCoords[0]; $row++) {
        for ($column = $startCoords[1]; $column <= $endCoords[1]; $column++) {
            $coords[] = [$row, $column];
        }
    }
    return $coords;
}
function whatSetTo($command, $coords)
{
    global $lights;
    switch ($command) {
        case 'turn on':