-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.php
44 lines (34 loc) · 1.46 KB
/
05.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
$f = fopen(__DIR__ . '/05-input.txt', 'r');
$ignoredUnits = ['_'];
$currentlyIgnoreUnitIndex = 0;
$reactedPolymerForEachIgnoredUnit = [];
$minLength = PHP_INT_MAX;
while(isset($ignoredUnits[$currentlyIgnoreUnitIndex])) {
$currentlyIgnoredUnit = $ignoredUnits[$currentlyIgnoreUnitIndex];
$reactedPolymer = '';
rewind($f);
while (!feof($f) && ($nextUnit = fread($f, 1)) !== false) {
$nextUnitLowerCase = strtolower($nextUnit);
$lastUnit = substr($reactedPolymer, -1);
if ($lastUnit !== $nextUnit && strtolower($lastUnit) === $nextUnitLowerCase) {
$reactedPolymer = substr($reactedPolymer, 0, -1);
}
elseif ($nextUnitLowerCase !== $currentlyIgnoredUnit) {
$reactedPolymer .= $nextUnit;
}
if (!in_array($nextUnitLowerCase, $ignoredUnits, true)) {
$ignoredUnits[] = $nextUnitLowerCase;
}
}
$reactedPolymerForEachIgnoredUnit[$currentlyIgnoredUnit] = $reactedPolymer;
if (strlen($reactedPolymer) < $minLength) {
$minLength = strlen($reactedPolymer);
}
// Repeat with the next unit on the list of ignore
$currentlyIgnoreUnitIndex++;
}
fclose($f);
echo 'Reacted polymer length is ' . strlen($reactedPolymerForEachIgnoredUnit['_']) . PHP_EOL;
echo 'Smallest polymer that can be obtained by ignoring one unit type is ' . $minLength . PHP_EOL;
file_put_contents(__DIR__ . '/05-input-reacted.txt', $reactedPolymerForEachIgnoredUnit['_']);