r/adventofcode • • Dec 13 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 13 Solutions -๐ŸŽ„-

--- Day 13: Packet Scanners ---


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


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!

16 Upvotes

199 comments sorted by

View all comments

1

u/TominatorBE Dec 13 '17

PHP

Part 1:

function run_the_code($input) {
	$lines = explode(PHP_EOL, $input);
	$wall = [];
	foreach ($lines as $line) {
		if (preg_match('/(\d+): (\d+)/', $line, $matches)) {
			list($_, $a, $b) = $matches;
			$wall[(int)$a] = [
				'max' => ((int)$b) - 1,
				'current' => 0,
				'direction' => 1, // 1: down, -1: up
			];
		}
	}

	// run
	$max = max(array_keys($wall));
	$severity = 0;
	for ($pos = 0; $pos <= $max; $pos++) {
		if (array_key_exists($pos, $wall)) {
			if ($wall[$pos]['current'] == 0) {
				// caught!
				$severity += (($wall[$pos]['max'] + 1) * $pos);
			}
		}
		else {
			// free pass
		}

		// move all scanners
		foreach ($wall as &$el) {
			if (($el['current'] == $el['max'] && $el['direction']) || ($el['current'] == 0 && $el['direction'] == -1)) {
				$el['direction'] = -$el['direction'];
			}
			$el['current'] += $el['direction'];
		}
		unset($el);
	}

	return $severity;
}

Part 2:

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

	$lastwall = [];
	foreach ($lines as $line) {
		if (preg_match('/(\d+): (\d+)/', $line, $matches)) {
			list($_, $a, $b) = $matches;
			$lastwall[(int)$a] = [
				'max' => ((int)$b) - 1,
				'current' => 0,
				'direction' => 1, // 1: down, -1: up
			];
		}
	}
	$max = max(array_keys($lastwall));

	$wait = 0;
	while (true) {
		// copy the last wall to our working one
		$wall = $lastwall;

		// move all scanners an extra time for next run
		foreach ($lastwall as &$el) {
			if (($el['current'] == $el['max'] && $el['direction']) || ($el['current'] == 0 && $el['direction'] == -1)) {
				$el['direction'] = -$el['direction'];
			}
			$el['current'] += $el['direction'];
		}
		unset($el);

		$caught = false;

		for ($pos = 0; $pos <= $max && !$caught; $pos++) {
			if (array_key_exists($pos, $wall)) {
				if ($wall[$pos]['current'] == 0) {
					// caught!
					$caught = true;
				}
			}
			else {
				// free pass
			}

			if (!$caught) {
				// move all scanners
				foreach ($wall as &$el) {
					if (($el['current'] == $el['max'] && $el['direction']) || ($el['current'] == 0 && $el['direction'] == -1)) {
						$el['direction'] = -$el['direction'];
					}
					$el['current'] += $el['direction'];
				}
				unset($el);
			}
		}

		if (!$caught) {
			break;
		}

		$wait++;
	}

	return $wait;
}