|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Year2023\Day12; |
| 4 | + |
| 5 | +$start = microtime(true); |
| 6 | + |
| 7 | +#$lines = file('example.txt', FILE_IGNORE_NEW_LINES); |
| 8 | +$lines = file('example2.txt', FILE_IGNORE_NEW_LINES); |
| 9 | +#$lines = file('input.txt', FILE_IGNORE_NEW_LINES); |
| 10 | + |
| 11 | +class Arrangement { |
| 12 | + |
| 13 | + private string $springs; |
| 14 | + private array $pattern; |
| 15 | + |
| 16 | + |
| 17 | + public function __construct(string $raw) |
| 18 | + { |
| 19 | + [$rawSprings, $rawPattern] = explode(" ", $raw); |
| 20 | + $this->springs = implode("?", array_fill(0, 5, $rawSprings)); |
| 21 | + $rawPattern = implode(",", array_fill(0, 5, $rawPattern)); |
| 22 | + $this->pattern = explode(",", $rawPattern); |
| 23 | + array_walk($this->pattern, static function (&$value) {$value = (int)$value;}); |
| 24 | + } |
| 25 | + |
| 26 | + public function getCountOfValidPermutations(): int |
| 27 | + { |
| 28 | + return $this->permutateAndCount($this->springs, $this->pattern); |
| 29 | + } |
| 30 | + |
| 31 | + private function permutateAndCount(string $springs, array $pattern): int |
| 32 | + { |
| 33 | + #empty string AND pattern means we are at the end and have found a match |
| 34 | + if ($springs === '') { |
| 35 | + if ($pattern === []) { |
| 36 | + return 1; |
| 37 | + } |
| 38 | + return 0; // some leftover pattern -> can't fulfill |
| 39 | + } |
| 40 | + |
| 41 | + # if there is no pattern left, and also no springs, return a match |
| 42 | + if ($pattern === []) { |
| 43 | + if (str_contains($springs, "#")) { |
| 44 | + return 0; // still some left, no match. |
| 45 | + } |
| 46 | + return 1; |
| 47 | + } |
| 48 | + |
| 49 | + # early exit if the remainder cannot work |
| 50 | + # if the sum of the pattern plus the minimum number of separators is longer than the remainder of the string |
| 51 | + # return 0, no match |
| 52 | + if ((array_sum($pattern) + count($pattern)-1) > strlen($springs)) { |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + # now, if there is only one pattern left, the string is the same length AND does not contain a separator, we found a match |
| 57 | + # the wildcard does not matter, it can only work when it's a #, so we can skip permutations. |
| 58 | + if (count($pattern) === 1 && $pattern[0] === strlen($springs) && !str_contains($springs, '.')) { |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + $count = 0; |
| 63 | + # if the next one is not a broken fountain - or is a wildcard - we move on with just a shorter string |
| 64 | + if ($springs[0] !== '#') { |
| 65 | + $count += $this->permutateAndCount(substr($springs, 1), $pattern); |
| 66 | + } |
| 67 | + |
| 68 | + # if there is a block exactly the size of the next pattern that does not contain a separator, we recurse on |
| 69 | + # (but not count, we're not at the end yet!) |
| 70 | + $nextPattern = array_shift($pattern); |
| 71 | + $sub = substr($springs, 0, $nextPattern); |
| 72 | + $next = ''; |
| 73 | + if (strlen($springs) > $nextPattern) { |
| 74 | + $next = $springs[$nextPattern]; // 0 indexed ftw! |
| 75 | + } |
| 76 | + |
| 77 | + if ($next !== '#' && !str_contains($sub, '.')) { |
| 78 | + $count += $this->permutateAndCount(substr($springs, $nextPattern + 1), $pattern); |
| 79 | + } |
| 80 | + |
| 81 | + return $count; |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +$sum = 0; |
| 87 | +foreach ($lines as $line) { |
| 88 | + $arrangement = new Arrangement($line); |
| 89 | + echo $line, ": ", ($count = $arrangement->getCountOfValidPermutations()), "\n"; |
| 90 | + $sum += $count; |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +echo "\n", $sum, "\n"; |
| 95 | + |
| 96 | +echo microtime(true) - $start; |
| 97 | +echo "\n"; |
| 98 | + |
0 commit comments