Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $input = str_split(file_get_contents('input.txt'));
- $x = 100;
- $y = 100;
- $ymax = $y * 2;
- $current = $previous = $x + $y * $ymax;
- $dists = array_fill(0, $current * 2, 0);
- $pos = array();
- $directions = array('N' => -$ymax, 'S' => $ymax, 'W' => -1, 'E' => 1);
- array_shift($input); //Remove ^
- array_pop($input); //Remove $
- foreach ($input as $i) {
- switch ($i) {
- case '|':
- $p = end($pos);
- $current = $p;
- break;
- case '(':
- $pos[] = $current;
- break;
- case ')':
- $p = array_pop($pos);
- $current = $p;
- break;
- default:
- $dir = $directions[$i];
- $current += $dir;
- $dc = $dists[$current];
- $dp = $dists[$previous];
- if ($dc === 0) {
- $dists[$current] = $dp + 1;
- } else {
- $dists[$current] = $dc > $dp ? $dp + 1 : $dc;
- }
- }
- $previous = $current;
- }
- printf('Part 1: %d', max($dists));
- echo PHP_EOL;
- printf('Part 2: %d', count(array_filter($dists, function ($n) {
- return $n >= 1000;
- })));
- echo PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment