14th day both parts

This commit is contained in:
kplaczek
2021-12-14 23:14:43 +01:00
parent 6d58a73344
commit e3494e234a
3 changed files with 67 additions and 1 deletions

22
14/part1.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$template = array_shift($input);
$pairs = [];
foreach ($input as $item) {
$line = explode(' -> ', $item);
$pairs[$line[0]] = $line[1];
}
$newTemplate = $template;
for ($j = 0; $j < 10; $j++) {
$tmpTemplate = $newTemplate;
for ($i = 0; $i < strlen($tmpTemplate) - 1; $i++) {
$window = substr($tmpTemplate, $i, 2);
$newTemplate = substr_replace($newTemplate, $pairs[$window], $i + 1 + $i, 0);
}
}
$characters = array_count_values(str_split(($newTemplate)));
sort($characters);
echo array_pop($characters) - array_shift($characters); //2549