From 7c9d8ae1bd3bbe5a0fd91f77b9064bf6ab431ae3 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Mon, 2 Dec 2024 09:47:25 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Finit=20le=20jour=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day_1/part_1.php | 20 ++++++++++++++++++++ day_1/part_2.php | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 day_1/part_1.php create mode 100644 day_1/part_2.php diff --git a/day_1/part_1.php b/day_1/part_1.php new file mode 100644 index 0000000..98886b1 --- /dev/null +++ b/day_1/part_1.php @@ -0,0 +1,20 @@ +!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; \ No newline at end of file diff --git a/day_1/part_2.php b/day_1/part_2.php new file mode 100644 index 0000000..ae7c6b4 --- /dev/null +++ b/day_1/part_2.php @@ -0,0 +1,21 @@ +!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; \ No newline at end of file