Example #1
0
                $command = LightSwitchCommand::COMMAND_TOGGLE;
                break;
            case 'turn on':
                $command = LightSwitchCommand::COMMAND_ON;
                break;
            case 'turn off':
            default:
                $command = LightSwitchCommand::COMMAND_OFF;
        }
        return new LightSwitchCommand(Coordinate::fromString($matches[2]), Coordinate::fromString($matches[3]), $command);
    }
}
$parser = new CommandParser();
$lightGrid = new LightGrid(new OnOffLightStrategy());
foreach (file('6.txt') as $instruction) {
    echo 'excecuting ' . $instruction . PHP_EOL;
    $lightGrid->toggleLights($parser->parseCommand($instruction));
}
echo 'Part 1 answer: ' . $lightGrid->getNumLights(true);
$lightGrid = new LightGrid(new BrightnessLightStrategy());
foreach (file('6.txt') as $instruction) {
    echo 'excecuting ' . $instruction . PHP_EOL;
    $lightGrid->toggleLights($parser->parseCommand($instruction));
}
$totalBrightness = 0;
foreach ($lightGrid->getLights() as $x => $arr) {
    foreach ($arr as $y => $brightness) {
        $totalBrightness += $brightness;
    }
}
echo 'Part 2 answer: ' . $totalBrightness;