Complete 2024 day1

This commit is contained in:
Timothy Warren 2024-12-03 14:59:53 -05:00
parent ffe19ab049
commit 86d891d546

View File

@ -51,9 +51,22 @@ function diffSum (array $list1, array $list2): int
return $sum; 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(); [$list1, $list2] = parseLists();
$sumOfDifference = diffSum($list1, $list2); $sumOfDifference = diffSum($list1, $list2);
$similarityScore = similarityScore($list1, $list2);
echo "Part 1: Total distance: $sumOfDifference\n"; echo "Part 1: Total distance: $sumOfDifference\n";
echo "Part 2: Similarity score: $similarityScore\n";