function solve($r, $lines, $limit = 0) {
$count = 0;
for ($i = 0; $i < count($lines); $i++) {
$cmd = explode(" ", $lines[$i]);
if (!--$limit) return $r;
match ($cmd[0]) {
"set" => $r[$cmd[1]] = $r[$cmd[2]] ?? $cmd[2],
"sub" => $r[$cmd[1]] -= $r[$cmd[2]] ?? $cmd[2],
"mul" => $r[$cmd[1]] *= $r[$cmd[2]] ?? $cmd[2],
"jnz" => $i = ($r[$cmd[1]] ?? $cmd[1]) != 0 ? $i + ($r[$cmd[2]] ?? $cmd[2]) - 1 : $i,
};
if ($cmd[0] == "mul") $count++;
}
return $count;
}
// ==================================================
// > PART 1
// ==================================================
$r = array_fill_keys(range("a", "h"), 0);
$solution_1 = solve($r, explode("\n", $input->string));
// ==================================================
// > PART 2
// ==================================================
$r["a"] = 1;
// Go to 100 iterations to have init values
$r = solve($r, explode("\n", $input->string), 100);
// Run optimized version of the instructions :
// -> all the prime numbers between b and c, incremented by 17
for ($x = $r["b"]; $x <= $r["c"]; $x += 17) {
if (!gmp_prob_prime($x)) $r["h"]++;
}
$solution_2 = $r["h"];