I'm a bit behind on these, but catching up this weekend! Day 5 is my favorite so far. 😁
<?php $input = trim(file_get_contents('./input.txt')); function reduce_polymer($str) { $index = strlen($str); while ($index > 0) { $index--; if ($str[$index] == $str[$index + 1]) { continue; } if (strtolower($str[$index]) == strtolower($str[$index + 1])) { $str = substr_replace($str, '', $index, 2); } } return strlen($str); } echo 'Initial reduction: ' . reduce_polymer($input) . PHP_EOL; $counts = []; foreach (range('a', 'z') as $letter) { $copy = $input; $copy = str_ireplace($letter, '', $copy); $counts[$letter] = reduce_polymer($copy); } echo 'Shortest polymer length: ' . min($counts);
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm a bit behind on these, but catching up this weekend! Day 5 is my favorite so far. 😁