advent-of-code/2015/day1/run.php

29 lines
645 B
PHP
Raw Permalink Normal View History

2022-12-15 12:03:31 -05:00
#!/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";