Complete 2024 day1
This commit is contained in:
parent
ffe19ab049
commit
86d891d546
@ -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";
|
Loading…
Reference in New Issue
Block a user