14th day both parts
This commit is contained in:
22
14/part1.php
Normal file
22
14/part1.php
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user