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