29 lines
645 B
PHP
29 lines
645 B
PHP
|
#!/usr/bin/env php
|
||
|
<?php declare(strict_types=1);
|
||
|
|
||
|
$inputStr = file_get_contents('input.txt');
|
||
|
$input = str_split($inputStr);
|
||
|
$actions = array_map(fn (string $item): int => $item === '(' ? 1 : -1, $input);
|
||
|
|
||
|
echo "Part 1: final floor:";
|
||
|
echo array_reduce($actions, fn (int $a, int $b): int => $a + $b, 0) . "\n";
|
||
|
|
||
|
function find_sum_index(array $actions, int $target = -1): int
|
||
|
{
|
||
|
$current = 0;
|
||
|
|
||
|
foreach ($actions as $idx => $val)
|
||
|
{
|
||
|
$current += $val;
|
||
|
|
||
|
if ($current === $target)
|
||
|
{
|
||
|
return $idx + 1;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
echo "Part 2: first step in basement:";
|
||
|
echo find_sum_index($actions, -1) . "\n";
|