r/adventofcode • • Dec 22 '17

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

--- Day 22: Sporifica Virus ---


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


  • [T-10 to launch] AoC ops, /r/nocontext edition:
  • <Endorphion> You may now make your waffle.
  • <Endorphion> ... on Mars.
  • [Update @ 00:17] 50 gold, silver cap
  • <Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy
  • <Topaz> that doesn't seem necessary
  • <Aneurysm9> what does "necessary" have to do with anything!
  • [Update @ 00:20] Leaderboard cap!
  • <Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE

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!

8 Upvotes

174 comments sorted by

View all comments

1

u/TominatorBE Dec 22 '17

PHP

Nothing really fancy, just doing the algo Call it with run_the_code(['map' => "...", 'bursts' => 10000000])

Part 1:

function run_the_code($input) {
	ini_set('memory_limit', -1);

	$bursts = $input['bursts'];
	$lines = explode(PHP_EOL, $input['map']);
	foreach ($lines as &$line) {
		$line = str_split($line);
	}
	unset($line);

	$map = [];
	$mapSize = 1000; // "infinite"
	for ($i = 0; $i < $mapSize; $i++) {
		$map[$i] = str_split(str_repeat('.', $mapSize));
	}

	// middle to put the input on
	$x = ($mapSize / 2) - ((count($lines) - 1) / 2);
	$y = ($mapSize / 2) - ((count($lines) - 1) / 2);
	for ($i = 0, $iMax = count($lines); $i < $iMax; $i++) {
		for ($j = 0, $jMax = count($lines[$i]); $j < $jMax; $j++) {
			$map[$x + $i][$y + $j] = $lines[$i][$j];
		}
	}

	// start position
	$x = $mapSize / 2;
	$y = $mapSize / 2;
	$d = 'n';

	$infected = 0;
	for ($burst = 0; $burst < $bursts; $burst++) {
		if ($map[$x][$y] == '#') {
			$map[$x][$y] = '.'; // clean

			switch ($d) {
				case 'n':
					$d = 'e';
					break;
				case 'e':
					$d = 's';
					break;
				case 's':
					$d = 'w';
					break;
				case 'w':
					$d = 'n';
					break;
			}
		}
		else {
			$map[$x][$y] = '#'; // infect
			$infected++;

			switch ($d) {
				case 'n':
					$d = 'w';
					break;
				case 'e':
					$d = 'n';
					break;
				case 's':
					$d = 'e';
					break;
				case 'w':
					$d = 's';
					break;
			}
		}
		switch ($d) {
			case 'n':
				$x--;
				break;
			case 'e':
				$y++;
				break;
			case 's':
				$x++;
				break;
			case 'w':
				$y--;
				break;
		}
		if ($x < 0 || $y < 0 || $x > $mapSize || $y > $mapSize) {
			die('map too small!');
		}
	}

	return $infected;
}

Part 2:

function run_the_code($input) {
	ini_set('memory_limit', -1);

	$bursts = $input['bursts'];
	$lines = explode(PHP_EOL, $input['map']);
	foreach ($lines as &$line) {
		$line = str_split($line);
	}
	unset($line);

	$map = [];
	$mapSize = 1000; // "infinite"
	for ($i = 0; $i < $mapSize; $i++) {
		$map[$i] = str_split(str_repeat('.', $mapSize));
	}

	// middle to put the input on
	$x = ($mapSize / 2) - ((count($lines) - 1) / 2);
	$y = ($mapSize / 2) - ((count($lines) - 1) / 2);
	for ($i = 0, $iMax = count($lines); $i < $iMax; $i++) {
		for ($j = 0, $jMax = count($lines[$i]); $j < $jMax; $j++) {
			$map[$x + $i][$y + $j] = $lines[$i][$j];
		}
	}

	// start position
	$x = $mapSize / 2;
	$y = $mapSize / 2;
	$d = 'n';

	$infected = 0;
	for ($burst = 0; $burst < $bursts; $burst++) {
		switch ($map[$x][$y]) {
			case '#': // infected
				// turn right
				switch ($d) {
					case 'n':
						$d = 'e';
						break;
					case 'e':
						$d = 's';
						break;
					case 's':
						$d = 'w';
						break;
					case 'w':
						$d = 'n';
						break;
				}

				// flag it
				$map[$x][$y] = 'F';
				break;
			case 'F': // flagged
				// flip direction
				switch ($d) {
					case 'n':
						$d = 's';
						break;
					case 'e':
						$d = 'w';
						break;
					case 's':
						$d = 'n';
						break;
					case 'w':
						$d = 'e';
						break;
				}

				// clean it
				$map[$x][$y] = '.';
				break;
			case 'W': // weakened
				// no turning

				// infect it
				$map[$x][$y] = '#';
				$infected++;
				break;
			case '.': // clean
				// turn left
				switch ($d) {
					case 'n':
						$d = 'w';
						break;
					case 'e':
						$d = 'n';
						break;
					case 's':
						$d = 'e';
						break;
					case 'w':
						$d = 's';
						break;
				}

				// weaken it
				$map[$x][$y] = 'W';
				break;
		}

		// move
		switch ($d) {
			case 'n':
				$x--;
				break;
			case 'e':
				$y++;
				break;
			case 's':
				$x++;
				break;
			case 'w':
				$y--;
				break;
		}
		if ($x < 0 || $y < 0 || $x > $mapSize || $y > $mapSize) {
			die('map too small!');
		}
	}

	return $infected;
}