From 86d891d546a4c3e8e38221d57eaf4c8758965b05 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 3 Dec 2024 14:59:53 -0500 Subject: [PATCH] Complete 2024 day1 --- 2024/day1/index.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/2024/day1/index.php b/2024/day1/index.php index 25643ac..97cc9dc 100644 --- a/2024/day1/index.php +++ b/2024/day1/index.php @@ -51,9 +51,22 @@ function diffSum (array $list1, array $list2): int return $sum; } +function similarityScore (array $list1, array $list2): int +{ + $sum = 0; + foreach ($list1 as $search) + { + $rightList = array_filter($list2, fn (int $item) => $item === $search); + $sum += ($search * count($rightList)); + } + + return $sum; +} + [$list1, $list2] = parseLists(); $sumOfDifference = diffSum($list1, $list2); +$similarityScore = similarityScore($list1, $list2); echo "Part 1: Total distance: $sumOfDifference\n"; - +echo "Part 2: Similarity score: $similarityScore\n"; \ No newline at end of file