Example #1
0
                    $grid[$i][$j] = false;
                    break;
                case 'toggle':
                    $grid[$i][$j] = !$grid[$i][$j];
                    break;
            }
        }
    }
}
echo 'Part one: ' . countGrid($grid);
echo '
';
// Part two
$grid = initGrid2();
foreach ($instructions as $instruction) {
    $parsed = parseInstruction($instruction);
    for ($i = $parsed['x']['start']; $i <= $parsed['x']['end']; $i++) {
        for ($j = $parsed['y']['start']; $j <= $parsed['y']['end']; $j++) {
            switch ($parsed['action']) {
                case 'toggle':
                    $grid[$i][$j]++;
                    // no break!
                // no break!
                case 'on':
                    $grid[$i][$j]++;
                    break;
                case 'off':
                    $grid[$i][$j] > 0 ? $grid[$i][$j]-- : 0;
                    break;
            }
        }
        $type = 'toggle';
        $range = [$front[1], $sections[1]];
    } else {
        $type = $front[1];
        $range = [$front[2], $sections[1]];
    }
    return ['type' => $type, 'range' => $range];
}
$input = file_get_contents('input.txt');
$limit = 999;
$lights = new SplFixedArray($limit + 1);
for ($i = 0; $i <= $limit; $i++) {
    $lights[$i] = \SplFixedArray::fromArray(array_fill(0, $limit + 1, 0));
}
foreach (explode("\n", $input) as $instruction) {
    $instruction = parseInstruction($instruction);
    $start = explode(',', $instruction['range'][0]);
    $end = explode(',', $instruction['range'][1]);
    for ($j = $start[0]; $j <= $end[0]; $j++) {
        for ($k = $start[1]; $k <= $end[1]; $k++) {
            switch ($instruction['type']) {
                case 'on':
                    $lights[$j][$k] = 1;
                    break;
                case 'off':
                    $lights[$j][$k] = 0;
                    break;
                case 'toggle':
                    $lights[$j][$k] = $lights[$j][$k] ? 0 : 1;
                    break;
            }