Skip to content

Instantly share code, notes, and snippets.

@maartenpaauw
Created December 1, 2024 13:24
Show Gist options
  • Save maartenpaauw/c514e0416af4d4c574e8db2f9c272352 to your computer and use it in GitHub Desktop.
Save maartenpaauw/c514e0416af4d4c574e8db2f9c272352 to your computer and use it in GitHub Desktop.
Advent of Code 2024 - Day 1
<?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);
<?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