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