Finit le jour 3

This commit is contained in:
Clement Desmidt 2022-12-06 09:47:09 +01:00
parent dafbffbcf8
commit 1e84b70b7a
2 changed files with 32 additions and 0 deletions

15
day_3/part_1.php Normal file
View File

@ -0,0 +1,15 @@
<?php
$rucksacks = explode("\n", file_get_contents('./input.txt'));
$lettersToNumbers = array_merge(range('a', 'z'), range('A', 'Z'));
echo array_sum(
array_map(
static function($rucksack) use ($lettersToNumbers) {
[$f, $s] = array_map('str_split', str_split($rucksack, strlen($rucksack)/2));
return array_search(current(array_intersect($f, $s)), $lettersToNumbers, true) + 1;
},
$rucksacks
)
);

17
day_3/part_2.php Normal file
View File

@ -0,0 +1,17 @@
<?php
$rucksacks = explode("\n", file_get_contents('./input.txt'));
$lettersToNumbers = array_merge(range('a', 'z'), range('A', 'Z'));
$groups = array_chunk($rucksacks, 3);
echo array_sum(
array_map(
static function($rucksacks) use ($lettersToNumbers) {
[$f, $s, $t] = array_map('str_split', $rucksacks);
return array_search(current(array_intersect($f, $s, $t)), $lettersToNumbers, true) + 1;
},
$groups
)
);