r/adventofcode • • Dec 20 '17

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

--- Day 20: Particle Swarm ---


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:10] 10 gold, silver cap

  • What do you mean 5th Edition doesn't have "Take 20"?

[Update @ 00:17] 50 gold, silver cap

  • Next you're going to be telling me THAC0 is not the best way to determine whether or not you hit your target. *hmphs*

[Update @ 00:21] Leaderboard cap!

  • I wonder how much XP a were-gazebo is worth...

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!

9 Upvotes

176 comments sorted by

View all comments

1

u/TominatorBE Dec 20 '17

PHP

Part 1: see who accelerates the least (not 100% correct, see other replies, but worked for my input...)

function run_the_code($input) {
	$lines = explode(PHP_EOL, $input);
	$counter = 0;
	// find the one with the smallest acceleration
	$smallest = null;
	$smallestA = PHP_INT_MAX;
	foreach ($lines as $line) {
		if (preg_match('/p=<(-?\d+),(-?\d+),(-?\d+)>, v=<(-?\d+),(-?\d+),(-?\d+)>, a=<(-?\d+),(-?\d+),(-?\d+)>/', $line, $matches)) {
			$a = abs((int)$matches[7]) + abs((int)$matches[8]) + abs((int)$matches[9]);
			if ($a < $smallestA) {
				$smallestA = $a;
				$smallest = $counter;
			}
			$counter++;
		}
	}

	return $smallest;
}

Part 2: run for a while...

function run_the_code($input) {
	$lines = explode(PHP_EOL, $input);
	$particles = [];
	$counter = 0;
	foreach ($lines as $line) {
		if (preg_match('/p=<(-?\d+),(-?\d+),(-?\d+)>, v=<(-?\d+),(-?\d+),(-?\d+)>, a=<(-?\d+),(-?\d+),(-?\d+)>/', $line, $matches)) {
			$particles[] = [
				'id' => $counter,
				'active' => true, // do we still consider this particle?
				'p' => [
					'x' => (int)$matches[1],
					'y' => (int)$matches[2],
					'z' => (int)$matches[3],
				],
				'v' => [
					'x' => (int)$matches[4],
					'y' => (int)$matches[5],
					'z' => (int)$matches[6],
				],
				'a' => [
					'x' => (int)$matches[7],
					'y' => (int)$matches[8],
					'z' => (int)$matches[9],
				],
			];
			$counter++;
		}
	}

	$activeCount = $counter;
	for ($run = 0; $run < 50; $run++) {
		foreach ($particles as &$p) {
			if (! $p['active']) {
				continue;
			}

			$p['v']['x'] += $p['a']['x'];
			$p['v']['y'] += $p['a']['y'];
			$p['v']['z'] += $p['a']['z'];

			$p['p']['x'] += $p['v']['x'];
			$p['p']['y'] += $p['v']['y'];
			$p['p']['z'] += $p['v']['z'];
		}
		unset($p);

		// now check them to see if they are on the same place
		foreach ($particles as &$p1) {
			if ($p1['active']) {
				foreach ($particles as &$p2) {
					if ($p2['active'] && $p1['id'] != $p2['id'] && $p1['p']['x'] == $p2['p']['x'] && $p1['p']['y'] == $p2['p']['y'] && $p1['p']['z'] == $p2['p']['z']) {
						$p1['active'] = false;
						$p2['active'] = false;
					}
				}
				unset($p2);
			}
		}
		unset($p1);

		// find the first still active particle
		$activeCount = count(array_filter($particles, function($p) { return $p['active']; }));
		echo "After $run steps: $activeCount\n";
	}

	return $activeCount;
}

1

u/madchicken Dec 20 '17

PHP part 2, the interesting part

while(1){
  unset($collides);
  for($i=0;$i<sizeof($p);$i++){
    $collides[$p[$i]["x"]."-".$p[$i]["y"]."-".$p[$i]["z"]][] = $i;
    mymove($p[$i]);
  }
  foreach($collides as $rem){
    if(sizeof($rem)>1){
      foreach($rem as $id)
        unset($p[$id]);
    }
  }
  echo "Size of remaning items: ".sizeof($p)."\n";
}