Add day 1 '24

This commit is contained in:
Tristan 2024-12-01 11:04:30 +01:00
parent 0a54b66d2d
commit d3f2406b23
Signed by: trizz
SSH Key Fingerprint: SHA256:Xsd2dTN+ZC3OZWfvuKIDMQ/1lWicuINSEPgRQw/CJT8
3 changed files with 1069 additions and 0 deletions

61
data/Y24/day1/Day1.php Normal file
View File

@ -0,0 +1,61 @@
<?php
namespace trizz\AdventOfCode\Y24;
use trizz\AdventOfCode\Solution;
final class Day1 extends Solution
{
public static null|int|string $part1ExampleResult = 11;
public static null|int|string $part1Result = 1882714;
public static null|int|string $part2ExampleResult = 31;
public static null|int|string $part2Result = null;
public function part1(array $data): int
{
[$listLeft, $listRight] = $this->getList($data);
$score = 0;
foreach ($listLeft as $index => $left) {
$right = $listRight[$index];
$score += abs($left - $right);
}
return $score;
}
public function part2(array $data): int
{
$score = 0;
[$listLeft, $listRight] = $this->getList($data);
foreach ($listLeft as $index => $left) {
$right = count(array_filter($listRight, fn ($x) => $x === $left));
if ($right > 0) {
$score += $left * $right;
}
}
return $score;
}
/**
* @param array<int,string> $data
*
* @return array<int,int[]>
*/
private function getList(array $data): array
{
$listLeft = [];
$listRight = [];
foreach ($data as $x) {
[$a1, $_, $_, $b1] = explode(' ', $x);
$listLeft[] = (int) $a1;
$listRight[] = (int) $b1;
}
sort($listLeft);
sort($listRight);
return [$listLeft, $listRight];
}
}

1001
data/Y24/day1/data.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
3 4
4 3
2 5
1 3
3 9
3 3