Browse Source

[2018-12-02] Add second challange sample and solution

master
Elis Hirwing 3 years ago
parent
commit
998cc043f8
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 36
      day2/main.php

36
day2/main.php

@ -25,6 +25,27 @@ function puzzle1(array $boxIds) : int
return $counters[2] * $counters[3];
}
function puzzle2(array $boxIds) : string
{
foreach ($boxIds as $boxId) {
foreach ($boxIds as $boxId2) {
if ($boxId === $boxId2) {
continue;
}
if (levenshtein($boxId, $boxId2) === 1) {
for ($i = 0; $i < strlen($boxId); $i++) {
if ($boxId[$i] !== $boxId2[$i]) {
return substr_replace($boxId, '', $i, 1);
}
}
break 2;
}
}
}
}
echo 'First challange sample:'.PHP_EOL;
echo puzzle1([
'abcdef',
@ -38,3 +59,18 @@ echo puzzle1([
echo 'First challange solution:'.PHP_EOL;
echo puzzle1(explode(PHP_EOL, trim(file_get_contents(dirname(__FILE__).'/input.txt')))).PHP_EOL.PHP_EOL;
echo 'Second challange sample:'.PHP_EOL;
echo puzzle2([
'abcde',
'fghij',
'klmno',
'pqrst',
'fguij',
'axcye',
'wvxyz',
]).PHP_EOL; // fgij
echo 'Second challange solution:'.PHP_EOL;
echo puzzle2(explode(PHP_EOL, trim(file_get_contents(dirname(__FILE__).'/input.txt')))).PHP_EOL.PHP_EOL;

Loading…
Cancel
Save