Created
December 1, 2024 13:24
-
-
Save maartenpaauw/c514e0416af4d4c574e8db2f9c272352 to your computer and use it in GitHub Desktop.
Advent of Code 2024 - Day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
$input = <<<'EOD' | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
EOD; | |
$leftList = []; | |
$rightList = []; | |
$differences = []; | |
$rows = explode("\n", $input); | |
foreach ($rows as $row) { | |
[$left, $right] = explode(' ', $row); | |
$leftList[] = (int) $left; | |
$rightList[] = (int) $right; | |
} | |
sort($leftList); | |
sort($rightList); | |
foreach ($leftList as $index => $left) { | |
$differences[] = abs($left - $rightList[$index]); | |
} | |
return array_sum($differences); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
$input = <<<'EOD' | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
EOD; | |
$leftList = []; | |
$rightList = []; | |
$score = []; | |
$rows = explode("\n", $input); | |
foreach ($rows as $row) { | |
[$left, $right] = explode(' ', $row); | |
$leftList[] = (int) $left; | |
$rightList[] = (int) $right; | |
} | |
$count = array_count_values($rightList); | |
foreach ($leftList as $value) { | |
$score[] = \array_key_exists($value, $count) ? $count[$value] * $value : 0; | |
} | |
return array_sum($score); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment