Answer ⏱ 3624ms
Part 1 :
4225
Part 2 :
⭐
// Parse begin state
preg_match("/Begin in state (\w)/", $input->string, $state);
$state = $state[1];
// Parse steps
preg_match("/checksum after (\d+) steps/", $input->string, $steps);
$steps = (int) $steps[1];
// Parse states
$states = $input->lines->filter()->slice(2)->chunk(9)->mapAssoc(fn ($i, $state) => [
$state[0][strlen($state[0]) - 2] =>
$state->slice(1)->chunk(4)->mapAssoc(fn ($i, $chunk) => [
$chunk[0][strlen($chunk[0]) - 2] => [
$chunk[1][strlen($chunk[1]) - 2], // Value
substr($chunk[2], -6, -1) == "right" ? 1 : -1, // Direction
$chunk[3][strlen($chunk[3]) - 2] // State
]
])
]);
$tape = [];
$pos = 0;
for ($i = 0; $i < $steps; $i++) {
$value = $tape[$pos] ?? 0; // Get value : 1 or 0
$tape[$pos] = $states[$state][$value][0]; // Set value
$pos += $states[$state][$value][1]; // Move pointer
$state = $states[$state][$value][2]; // Change state
}
$solution_1 = array_sum($tape);
$solution_2 = "⭐";