$addr . $bin[$i], $addresses); break; case '1': $addresses = array_map(fn ($addr) => $addr . '1', $addresses); break; case 'X': $new_addresses = []; foreach ($addresses as $address) { $new_addresses[] = $address . '0'; $new_addresses[] = $address . '1'; } $addresses = $new_addresses; break; } } return array_map('bindec', $addresses); } function apply_mask(string $mask, int $num): int { $bin = str_pad(decbin($num), 36, '0', STR_PAD_LEFT); for ($i = 0; $i < strlen($bin); $i++) if ($mask[$i] != 'X') $bin[$i] = $mask[$i]; return bindec($bin); } function parse_instructions(array $instructions): array { $parsed_instructions = []; foreach ($instructions as $instruction) { [$inst, $arg] = array_map('trim', explode(' = ', $instruction)); if ($inst === 'mask') $parsed_instructions[] = [$inst, $arg]; else $parsed_instructions[] = ['mem', substr($inst, 4, strlen($inst) - 5), $arg]; } return $parsed_instructions; }