|
| 1 | +<?php |
| 2 | + |
| 3 | +$start = microtime(true); |
| 4 | + |
| 5 | + |
| 6 | +#$lines = file('example.txt', FILE_IGNORE_NEW_LINES); |
| 7 | +$lines = file('input.txt', FILE_IGNORE_NEW_LINES); |
| 8 | + |
| 9 | + |
| 10 | +$map = []; |
| 11 | +foreach ($lines as $line) { |
| 12 | + $map[] = str_split($line); |
| 13 | +} |
| 14 | + |
| 15 | +$variants = [ |
| 16 | + [ |
| 17 | + ['find' => 'M', 'col' => -1, 'row' => -1], |
| 18 | + ['find' => 'M', 'col' => -1, 'row' => 1], |
| 19 | + ['find' => 'S', 'col' => 1, 'row' => -1], |
| 20 | + ['find' => 'S', 'col' => 1, 'row' => 1], |
| 21 | + ], |
| 22 | + [ |
| 23 | + ['find' => 'M', 'col' => -1, 'row' => -1], |
| 24 | + ['find' => 'S', 'col' => -1, 'row' => 1], |
| 25 | + ['find' => 'M', 'col' => 1, 'row' => -1], |
| 26 | + ['find' => 'S', 'col' => 1, 'row' => 1], |
| 27 | + ], |
| 28 | + [ |
| 29 | + ['find' => 'S', 'col' => -1, 'row' => -1], |
| 30 | + ['find' => 'S', 'col' => -1, 'row' => 1], |
| 31 | + ['find' => 'M', 'col' => 1, 'row' => -1], |
| 32 | + ['find' => 'M', 'col' => 1, 'row' => 1], |
| 33 | + ], |
| 34 | + [ |
| 35 | + ['find' => 'S', 'col' => -1, 'row' => -1], |
| 36 | + ['find' => 'M', 'col' => -1, 'row' => 1], |
| 37 | + ['find' => 'S', 'col' => 1, 'row' => -1], |
| 38 | + ['find' => 'M', 'col' => 1, 'row' => 1], |
| 39 | + ], |
| 40 | +]; |
| 41 | + |
| 42 | + |
| 43 | +$count = 0; |
| 44 | +foreach ($map as $rowNum => $row) { |
| 45 | + foreach ($row as $colNum => $char) { |
| 46 | + if ('A' !== $char) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + foreach ($variants as $variant) { |
| 50 | + foreach ($variant as $target) { |
| 51 | + $searchCol = $colNum + $target['col']; |
| 52 | + $searchRow = $rowNum + $target['row']; |
| 53 | + if (!isset($map[$searchRow][$searchCol]) || $map[$searchRow][$searchCol] !== $target['find']) { |
| 54 | + continue 2; |
| 55 | + } |
| 56 | + } |
| 57 | + ++$count; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +echo $count, "\n"; |
| 65 | + |
| 66 | +echo microtime(true) - $start; |
| 67 | +echo "\n"; |
0 commit comments