From e90d6186c536544f27496917df254cc35720f2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 18 Dec 2020 09:56:50 +0100 Subject: [PATCH] :sparkles: Finit le jour 18 --- day_18/part_1.php | 53 +++++++++++++++++++++++++++++++++++++++++++ day_18/part_2.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 day_18/part_1.php create mode 100644 day_18/part_2.php diff --git a/day_18/part_1.php b/day_18/part_1.php new file mode 100644 index 0000000..af48451 --- /dev/null +++ b/day_18/part_1.php @@ -0,0 +1,53 @@ + $operation) { + $operation = evaluateParentheses($operation); + $operation = evaluateOperation($operation); + $results[] = (int) $operation; + echo sprintf('Line %u : %u%s', $i+1, $operation, "\n"); +} + +echo array_sum($results); +/** + * @param string $operation + * + * @return false|float|int + * @throws \Exception + */ +function evaluateOperation(string $operation) +{ + while (!is_numeric($operation)) { + $operation = preg_replace_callback( + '/^(\d+) (\+|\*) (\d+)/', + static function ($matches) { + switch ($matches[2]) { + case '+': + return (int)$matches[1] + (int)$matches[3]; + case '*': + return (int)$matches[1] * (int)$matches[3]; + default: + throw new \Exception('Unknown Operation ' . var_export($matches, true)); + } + }, + $operation + ); + } + + return $operation; +} + +function evaluateParentheses($operation) +{ + $result = preg_replace_callback('/\(([^\)\(]+)\)/', static function ($matches) { + return evaluateOperation($matches[1]); + }, $operation); + + if ($result === $operation || $result === null) { + return $operation; + } + + return evaluateParentheses($result); +} diff --git a/day_18/part_2.php b/day_18/part_2.php new file mode 100644 index 0000000..fa029ad --- /dev/null +++ b/day_18/part_2.php @@ -0,0 +1,57 @@ + $operation) { + $operation = evaluateParentheses($operation); + $operation = evaluateOperation($operation); + $results[] = (int) $operation; + echo sprintf('Line %u : %u%s', $i+1, $operation, "\n"); +} + +echo array_sum($results); +/** + * @param string $operation + * + * @return false|float|int + * @throws \Exception + */ +function evaluateOperation(string $operation) +{ + while (!is_numeric($operation)) { + $original_operation = $operation; + $operation = preg_replace_callback( + '/(\d+) \+ (\d+)/', + static function ($matches) { + return (int)$matches[1] + (int)$matches[2]; + }, + $operation + ); + + if ($original_operation === $operation) { + $operation = preg_replace_callback( + '/(\d+) \* (\d+)/', + static function ($matches) { + return (int)$matches[1] * (int)$matches[2]; + }, + $operation + ); + } + } + + return $operation; +} + +function evaluateParentheses($operation) +{ + $result = preg_replace_callback('/\(([^\)\(]+)\)/', static function ($matches) { + return evaluateOperation($matches[1]); + }, $operation); + + if ($result === $operation || $result === null) { + return $operation; + } + + return evaluateParentheses($result); +}