From ddfa50a6afbaba3ff195e6a179f620ae197ed397 Mon Sep 17 00:00:00 2001 From: Tristan Date: Thu, 2 Dec 2021 09:24:32 +0100 Subject: [PATCH] Use match instead of switch --- src/Day2.php | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Day2.php b/src/Day2.php index 50c778a..ac0fde2 100644 --- a/src/Day2.php +++ b/src/Day2.php @@ -25,22 +25,11 @@ class Day2 extends AbstractCommand */ [$direction, $distance] = explode(' ', $current); - switch ($direction) { - case 'forward': - $horizontal += $distance; - - break; - - case 'down': - $depth += $distance; - - break; - - case 'up': - $depth -= $distance; - - break; - } + match ($direction) { + 'forward' => $horizontal += $distance, + 'down' => $depth += $distance, + 'up' => $depth -= $distance, + }; } return $depth * $horizontal; @@ -63,6 +52,7 @@ class Day2 extends AbstractCommand */ [$direction, $distance] = explode(' ', $current); + // Can't use 'match' here because of the multiple expressions for 'forward'. switch ($direction) { case 'forward': $horizontal += $distance;