advent_of_code_2022/day_3/part_2.php
2022-12-06 09:47:09 +01:00

18 lines
469 B
PHP

<?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
)
);