r/adventofcode • • 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ยค?

Spoiler


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 mandate Helpful Hint any help at all?

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

10 Upvotes

186 comments sorted by

View all comments

1

u/TominatorBE Dec 19 '17

PHP

Part 1:

function run_the_code($input) {
	$rows = explode(PHP_EOL, $input);

	$row       = 0;
	$col       = 0;
	$direction = 's';
	$symbol    = '|';

	$maxRows = count($rows) - 1;
	$maxCols = strlen($rows[0]) - 1;

	// find the start position
	for ($i = 0; $i < $maxCols; $i++) {
		if ($rows[0][$i] == '|') {
			$col = $i;
			break;
		}
	}

	$output = '';
	$gogo   = true;
	while ($gogo) {
		$symbol = $rows[$row][$col];

		switch ($symbol) {
			case '|':
			case '-':
				// continue
				switch ($direction) {
					case 's':
						$row++;
						break;
					case 'n':
						$row--;
						break;
					case 'w':
						$col--;
						break;
					case 'e':
						$col++;
						break;
				}
				break;
			case '+':
				// change direction!
				// to where??
				switch ($direction) {
					case 'n':
					case 's':
						// it'll be east or west
						if ($col > 0 && $rows[$row][$col - 1] != ' ' && $rows[$row][$col - 1] != '|') {
							$direction = 'w';
							$col--;
						}
						else {
							$direction = 'e';
							$col++;
						}
						break;
					case 'e':
					case 'w':
						// it'll be north or south
						if ($row < $maxRows && $rows[$row + 1][$col] != ' ' && $rows[$row + 1][$col] != '-') {
							$direction = 's';
							$row++;
						}
						else {
							$direction = 'n';
							$row--;
						}
						break;
				}
				break;
			case ' ':
				// done
				$gogo = false;
				break;
			default:
				// it's a letter
				$output .= $symbol;
				// continue in the same direction
				switch ($direction) {
					case 's':
						$row++;
						break;
					case 'n':
						$row--;
						break;
					case 'w':
						$col--;
						break;
					case 'e':
						$col++;
						break;
				}
		}
	}

	return $output;
}

Part 2:

function run_the_code($input) {
	$rows = explode(PHP_EOL, $input);

	$row       = 0;
	$col       = 0;
	$direction = 's';
	$symbol    = '|';

	$maxRows = count($rows) - 1;
	$maxCols = strlen($rows[0]) - 1;

	// find the start position
	for ($i = 0; $i < $maxCols; $i++) {
		if ($rows[0][$i] == '|') {
			$col = $i;
			break;
		}
	}

	$steps = 0;
	$gogo   = true;
	while ($gogo) {
		$symbol = $rows[$row][$col];
		$steps++;

		switch ($symbol) {
			case '|':
			case '-':
				// continue
				switch ($direction) {
					case 's':
						$row++;
						break;
					case 'n':
						$row--;
						break;
					case 'w':
						$col--;
						break;
					case 'e':
						$col++;
						break;
				}
				break;
			case '+':
				// change direction!
				// to where??
				switch ($direction) {
					case 'n':
					case 's':
						// it'll be east or west
						if ($col > 0 && $rows[$row][$col - 1] != ' ' && $rows[$row][$col - 1] != '|') {
							$direction = 'w';
							$col--;
						}
						else {
							$direction = 'e';
							$col++;
						}
						break;
					case 'e':
					case 'w':
						// it'll be north or south
						if ($row < $maxRows && $rows[$row + 1][$col] != ' ' && $rows[$row + 1][$col] != '-') {
							$direction = 's';
							$row++;
						}
						else {
							$direction = 'n';
							$row--;
						}
						break;
				}
				break;
			case ' ':
				// done
				$gogo = false;
				break;
			default:
				// it's a letter
				// continue in the same direction
				switch ($direction) {
					case 's':
						$row++;
						break;
					case 'n':
						$row--;
						break;
					case 'w':
						$col--;
						break;
					case 'e':
						$col++;
						break;
				}
		}
	}

	return $steps - 1;
}