✨ Finit le jour 1
This commit is contained in:
parent
e98ff431dc
commit
7c9d8ae1bd
20
day_1/part_1.php
Normal file
20
day_1/part_1.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$list_1 = [];
|
||||||
|
$list_2 = [];
|
||||||
|
|
||||||
|
$lines = array_filter(explode("\n", file_get_contents(__DIR__.'/input.txt')), static fn($line) =>!empty($line));
|
||||||
|
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
[$a, $b] = explode(" ", $line);
|
||||||
|
$list_1[] = $a;
|
||||||
|
$list_2[] = $b;
|
||||||
|
}
|
||||||
|
|
||||||
|
sort($list_1); sort($list_2);
|
||||||
|
$total = 0;
|
||||||
|
for ($i=0, $l=count($list_1); $i < $l; $i++) {
|
||||||
|
$total += abs($list_1[$i] - $list_2[$i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $total;
|
21
day_1/part_2.php
Normal file
21
day_1/part_2.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$list_1 = [];
|
||||||
|
$list_2 = [];
|
||||||
|
|
||||||
|
$lines = array_filter(explode("\n", file_get_contents(__DIR__.'/input.txt')), static fn($line) =>!empty($line));
|
||||||
|
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
[$a, $b] = explode(" ", $line);
|
||||||
|
$list_1[] = $a;
|
||||||
|
$list_2[] = $b;
|
||||||
|
}
|
||||||
|
|
||||||
|
$similarity_score = 0;
|
||||||
|
|
||||||
|
$count_list_2 = array_count_values($list_2);
|
||||||
|
foreach ($list_1 as $item) {
|
||||||
|
$similarity_score += array_key_exists($item, $count_list_2) ? $count_list_2[$item] * $item : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $similarity_score;
|
Loading…
Reference in New Issue
Block a user