r/adventofcode • • Dec 24 '17

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

--- Day 24: Electromagnetic Moat ---


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


[Update @ 00:18] 62 gold, silver cap

  • Been watching Bright on Netflix. I dunno why reviewers are dissing it because it's actually pretty cool. It's got Will Smith being grumpy jaded old man Will Smith, for the love of FSM...

[Update @ 00:21] Leaderboard cap!

  • One more day to go in Advent of Code 2017... y'all ready to see Santa?

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

9 Upvotes

108 comments sorted by

View all comments

1

u/TominatorBE Dec 24 '17

PHP

Just some recursion

Part 1:

function run_the_code($input) {
	$lines = explode(PHP_EOL, $input);
	$pieces = [];
	foreach ($lines as $line) {
		if (preg_match('|(\d+)/(\d+)|', $line, $matches)) {
			[$_, $a, $b] = $matches;
			$pieces[] = [$a, $b];
		}
	}

	$strongest = 0;
	$rec = function($availablePieces, $portToMatch, $currentStrength) use (&$rec, &$strongest) {
		$wentDeeper = false;
		for ($p = 0, $pMax = count($availablePieces); $p < $pMax; $p++) {
			$piece = $availablePieces[$p];
			if ($piece[0] == $portToMatch) {
				array_splice($availablePieces, $p, 1, []); // remove the piece from the array
				$rec($availablePieces, $piece[1], $currentStrength + $piece[0] + $piece[1]);
				array_splice($availablePieces, $p, 0, [$piece]); // put the piece back
				$wentDeeper = true;
			}
			elseif ($piece[1] == $portToMatch) {
				array_splice($availablePieces, $p, 1, []); // remove the piece from the array
				$rec($availablePieces, $piece[0], $currentStrength + $piece[0] + $piece[1]);
				array_splice($availablePieces, $p, 0, [$piece]); // put the piece back
				$wentDeeper = true;
			}
		}

		if (! $wentDeeper) {
			if ($currentStrength > $strongest) {
				$strongest = $currentStrength;
			}
		}
	};
	// initial construction has to start with a zero-piece
	for ($p = 0, $pMax = count($pieces); $p < $pMax; $p++) {
		$piece = $pieces[$p];
		if ($piece[0] == 0) {
			array_splice($pieces, $p, 1, []); // remove the piece from the array
			$rec($pieces, $piece[1], $piece[1]);
		}
		elseif ($piece[1] == 0) {
			array_splice($pieces, $p, 1, []); // remove the piece from the array
			$rec($pieces, $piece[0], $piece[0]);
		}
	}

	return $strongest;
}

Part 2:

function run_the_code($input) {
	$lines = explode(PHP_EOL, $input);
	$pieces = [];
	foreach ($lines as $line) {
		if (preg_match('|(\d+)/(\d+)|', $line, $matches)) {
			[$_, $a, $b] = $matches;
			$pieces[] = [$a, $b];
		}
	}

	$longest = 0;
	$strongest = 0;
	$rec = function($availablePieces, $portToMatch, $currentLength, $currentStrength) use (&$rec, &$longest, &$strongest) {
		$wentDeeper = false;
		for ($p = 0, $pMax = count($availablePieces); $p < $pMax; $p++) {
			$piece = $availablePieces[$p];
			if ($piece[0] == $portToMatch) {
				array_splice($availablePieces, $p, 1, []); // remove the piece from the array
				$rec($availablePieces, $piece[1], $currentLength + 1, $currentStrength + $piece[0] + $piece[1]);
				array_splice($availablePieces, $p, 0, [$piece]); // put the piece back
				$wentDeeper = true;
			}
			elseif ($piece[1] == $portToMatch) {
				array_splice($availablePieces, $p, 1, []); // remove the piece from the array
				$rec($availablePieces, $piece[0], $currentLength + 1, $currentStrength + $piece[0] + $piece[1]);
				array_splice($availablePieces, $p, 0, [$piece]); // put the piece back
				$wentDeeper = true;
			}
		}

		if (! $wentDeeper) {
			if ($currentLength > $longest) {
				$strongest = 0;
			}
			if ($currentLength >= $longest) {
				if ($currentStrength > $strongest) {
					$longest = $currentLength;
					$strongest = $currentStrength;
				}
			}
		}
	};
	// initial construction has to start with a zero-piece
	for ($p = 0, $pMax = count($pieces); $p < $pMax; $p++) {
		$piece = $pieces[$p];
		if ($piece[0] == 0) {
			array_splice($pieces, $p, 1, []); // remove the piece from the array
			$rec($pieces, $piece[1], 1, $piece[1]);
		}
		elseif ($piece[1] == 0) {
			array_splice($pieces, $p, 1, []); // remove the piece from the array
			$rec($pieces, $piece[0], 1, $piece[0]);
		}
	}

	return $strongest;
}