From 52b8935d620b9fdd870215df27e986f313b5d4ff Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 5 Dec 2022 20:38:39 +0100 Subject: [PATCH] 2022 Day 5 part 1 --- 2022/day5.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2022/day5.php 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