|
8 | 8 |
|
9 | 9 | class Day16 extends DayBehaviour implements DayInterface
|
10 | 10 | {
|
11 |
| - public function solvePart1(): int|string|null |
| 11 | + public function solvePart1(): ?int |
12 | 12 | {
|
13 |
| - // TODO: Implement solvePart1() method. |
14 |
| - return null; |
| 13 | + /*$this->input = array_map(static fn(string $l) => $l . "\n", explode("\n",<<<INPUT |
| 14 | + class: 1-3 or 5-7 |
| 15 | + row: 6-11 or 33-44 |
| 16 | + seat: 13-40 or 45-50 |
| 17 | +
|
| 18 | + your ticket: |
| 19 | + 7,1,14 |
| 20 | +
|
| 21 | + nearby tickets: |
| 22 | + 7,3,47 |
| 23 | + 40,4,50 |
| 24 | + 55,2,20 |
| 25 | + 38,6,12 |
| 26 | + INPUT));*/ |
| 27 | + |
| 28 | + [$rules,, $nearby] = $this->getTicketDataFromInput($this->input); |
| 29 | + |
| 30 | + $invalid = []; |
| 31 | + foreach ($nearby as $ticket) { |
| 32 | + foreach ($ticket as $n) { |
| 33 | + $foundValidRule = false; |
| 34 | + $ticketInvalid = []; |
| 35 | + foreach ($rules as [$a, $b]) { |
| 36 | + if (($n < $a[0] || $n > $a[1]) && ($n < $b[0] || $n > $b[1])) { |
| 37 | + $ticketInvalid[] = $n; |
| 38 | + } else { |
| 39 | + $foundValidRule = true; |
| 40 | + } |
| 41 | + } |
| 42 | + if (!$foundValidRule) { |
| 43 | + /** @noinspection SlowArrayOperationsInLoopInspection */ |
| 44 | + $invalid = array_merge($invalid, array_unique($ticketInvalid)); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return (int) array_sum($invalid); |
15 | 50 | }
|
16 | 51 |
|
17 |
| - public function solvePart2(): int|string|null |
| 52 | + public function solvePart2(): ?int |
18 | 53 | {
|
19 | 54 | // TODO: Implement solvePart2() method.
|
20 | 55 | return null;
|
21 | 56 | }
|
| 57 | + |
| 58 | + protected function getTicketDataFromInput(array $input): array |
| 59 | + { |
| 60 | + $ticket = [ |
| 61 | + [], // rules |
| 62 | + [], // my |
| 63 | + [], // nearby |
| 64 | + ]; |
| 65 | + $inputPositions = array_keys($ticket); |
| 66 | + // start the pointer at rules |
| 67 | + $p = array_shift($inputPositions); |
| 68 | + // loop over each input, when we encounter a blank new line, update our pointer ($p) to the next position |
| 69 | + foreach ($input as $line) { |
| 70 | + if (PHP_EOL === $line) { |
| 71 | + $p = array_shift($inputPositions); |
| 72 | + } |
| 73 | + $line = trim($line); |
| 74 | + |
| 75 | + // parse a rule e.g.: departure location: 29-458 or 484-956 |
| 76 | + if (0 === $p) { |
| 77 | + [$ruleName, $numbers] = explode(':', $line, 2); |
| 78 | + if (1 <= preg_match_all('/(\d+)/', $numbers, $matches)) { |
| 79 | + $ticket[$p][$ruleName] = array_map(static fn (array $a) => array_map('intval', $a), array_chunk($matches[1], 2)); |
| 80 | + } |
| 81 | + } elseif (str_contains($line, ',')) { // parse the comma separated list of numbers |
| 82 | + $nums = explode(',', $line); |
| 83 | + if (!empty($nums)) { |
| 84 | + $ticket[$p][] = array_map('intval', $nums); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return $ticket; |
| 90 | + } |
22 | 91 | }
|
0 commit comments