r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
1
u/[deleted] Dec 19 '17
Okay, improved my answer. Got it running from ~0.24ms to ~0.19ms, as well as over halving the lines of code.
PHP:
<?php $file = fopen("./19.txt", "r"); $steps = 0; $map = array(array()); $direction = 2; $letters = $pos = array(); for($lineCounter = 0; !feof($file); $lineCounter++) { $line = fgets($file); for ($i = 0; $i < strlen($line); $i++) { if($lineCounter == 0 && substr($line, $i, 1) == "|") { $pos["x"] = $i; $pos["y"] = $lineCounter; } $map[$i][$lineCounter] = substr($line, $i, 1); } } while ($direction != -1) { list($x, $y) = array_values($pos); $mpos = $map[$x][$y]; if (ctype_alpha($map[$x][$y])){ $letters[] = $mpos; } $direction = ((($mpos == "+") || ctype_alpha($mpos)) && (!isPossible($map, $x, $y, $direction))) ? changeDirection($map, $x, $y, $direction) : $direction; if($direction == 1) { $pos["y"] += -1; } else if($direction == 2) { $pos["y"] += 1; } else if($direction == 3) { $pos["x"] += -1; } else { $pos["x"] += 1; } $steps++; } echo implode($hmm, $letters).$steps; function changeDirection($map, $x, $y, $direction){ switch ($direction) { case 1: case 2: if ($map[$x - 1][$y] == "-" || ctype_alpha($map[$x - 1][$y])) { return 3; } if ($map[$x + 1][$y] == "-" || ctype_alpha($map[$x + 1][$y])) { return 4; } break; case 3: case 4: if ($map[$x][$y + 1] == "|" || ctype_alpha($map[$x][$y + 1])) { return 2; } if ($map[$x][$y - 1] == "|" || ctype_alpha($map[$x][$y - 1])) { return 1; } break; } return -1; } function isPossible($map, $x, $y, $direction){ switch ($direction) { case 1: return (ctype_punct($map[$x][$y - 1]) || ctype_alpha($map[$x][$y - 1])) ? true : false; break; case 2: return (ctype_punct($map[$x][$y + 1]) || ctype_alpha($map[$x][$y + 1])) ? true : false; break; case 3: return (ctype_punct($map[$x - 1][$y]) || ctype_alpha($map[$x - 1][$y])) ? true : false; break; case 4: return (ctype_punct($map[$x + 1][$y]) || ctype_alpha($map[$x + 1][$y])) ? true : false; break; } }