Answer ⏱ 2ms
Part 1 :
20=2-02-0---02=22=21
Part 2 :
⭐
function SNAFU($number)
{
$nums = "=-012";
return $number->chars->reverse()->reduce(fn ($sum, $char, $i) =>
$sum + (strpos( $nums, $char) - 2) * pow(5, $i)
, 0, true);
}
function UFANS($number)
{
$nums = "=-012";
$chars = scalar(base_convert($number, 10, 5))->chars();
// Offset 3 & 4 to 1= & 1-
while ($chars->hasValue(3) || $chars->hasValue(4)) {
foreach ($chars as $i=>$char) {
if ($char == 3) {
$chars[$i - 1] = ($chars[$i - 1] ?? 0) + 1;
$chars[$i] = -2;
}
if ($char == 4) {
$chars[$i - 1] = ($chars[$i - 1] ?? 0) + 1;
$chars[$i] = -1;
}
}
}
return $chars->mapAssoc(function ($i, $a) use ($chars, $nums) {
return [($i + $chars->count()) => $nums[$a + 2]];
})->sortKeys()->values()->join("");
}
$solution_1 = UFANS($input->lines->map("SNAFU")->sum());
$solution_2 = "⭐";