function solve($lengths, $iterations = 1) {
$list = range(0, 255);
$pos = 0;
$skip = 0;
for ($i = 0; $i < $iterations; $i++) foreach ($lengths as $length) {
$segment = array_reverse(array_slice(array_merge($list, $list), $pos, $length));
$looped = $pos + $length - count($list);
if ($looped <= 0) {
$list = array_merge(
array_slice($list, 0, $pos),
$segment,
array_slice($list, $pos + $length)
);
} else {
$list = array_merge(
array_slice($segment, -$looped),
array_slice($list, $looped, $pos - $looped),
array_slice($segment, 0, -$looped)
);
}
$pos = ($pos + $length + $skip) % count($list);
$skip++;
}
return $list;
}
function get_hash($string) {
$lengths = scalar($string)->chars()->map(fn ($char) => ord($char))->merge([17, 31, 73, 47, 23]);
$list = solve($lengths, 64);
return set($list)->chunk(16)
// Reduce each chunk to a single number by XORing all numbers
->map(fn ($chunk) => $chunk->reduce(fn ($carry, $item) => $carry ^ $item, $chunk->shift()))
// Convert each number to a hex string of length 2
->map(fn ($num) => str_pad(dechex($num), 2, "0", STR_PAD_LEFT))->join("");
}
// ==================================================
// > PART 1
// ==================================================
$list = solve($input->numbers());
$solution_1 = $list[0] * $list[1];
// ==================================================
// > PART 2
// ==================================================
$solution_2 = get_hash($input->string);