diff --git a/2022/day5.php b/2022/day5.php new file mode 100644 index 0000000..497661e --- /dev/null +++ b/2022/day5.php @@ -0,0 +1,37 @@ + trim($str, '[] '), str_split($line, 4)); + $crates = array_combine($numbers, $items); + foreach ($crates as $stack => $crate) { + if ($crate !== '') { + array_unshift($stacks[$stack], $crate); + } + } +} + +foreach ($commands as $command) { + preg_match('/move (\d+) from (\d+) to (\d+)/', $command, $matches); + [$x, $count, $from, $to] = $matches; + for ($i = 0; $i < $count; $i++) { + array_push($stacks[$to], array_pop($stacks[$from])); + } +} + +$result = implode('', array_map('end', $stacks)); +echo 'Top crates: ' . $result; \ No newline at end of file