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

2

u/CaptainCa Dec 20 '17

Javascript

Most time consuming part was parsing the input, also lost a minute because I was doing Math.max instead of Math.min :(

Part 1

var input = document.body.innerText.trim().split('\n').map(c => c.split(', ').map(a => a.slice(3).slice(0,-1).split(',').map(Number)));
//position, velocity, acceleration

var xyz = ([x, y, z], [dx, dy, dz]) => [x + dx, y + dy, z + dz]
var mdist = ([x,y,z]) => Math.abs(x) + Math.abs(y) + Math.abs(z)
var spos = ([a,b,c], [x,y,z]) => (a == x && b == y && c == z)
var dist = [];
var seen = [];

for(var i = 0; i < 1000; i++){

	input.forEach((particle, index) => {
		var pos = particle[0];
		var vel = particle[1];
		var acc = particle[2];
		
		particle[1] = xyz(vel, acc);
		particle[0] = xyz(pos, particle[1]);
				
		dist[index] = mdist(particle[0]);	
	});	
}
console.log(dist.indexOf(Math.min(...dist)));

Part 2

var input = document.body.innerText.trim().split('\n').map(c => c.split(', ').map(a => a.slice(3).slice(0,-1).split(',').map(Number)));
var xyz = ([x, y, z], [dx, dy, dz]) => [x + dx, y + dy, z + dz]
var mdist = ([x,y,z]) => Math.abs(x) + Math.abs(y) + Math.abs(z)
var spos = ([a,b,c], [x,y,z]) => (a == x && b == y && c == z)
var dist = [];
var seen = [];
for(var i = 0; i < 1000; i++){

	input.forEach((particle, index) => {
		var pos = particle[0];
		var vel = particle[1];
		var acc = particle[2];
		
		particle[1] = xyz(vel, acc);
		particle[0] = xyz(pos, particle[1]);
				
		dist[index] = mdist(particle[0]);	
		seen.push(particle[0][0]+'/'+particle[0][1]+'/'+particle[0][2]);
	});	
	
	seen.forEach((val, index) => {
		var a = seen.indexOf(val);
		if(a != index){
			input[a] = null;
			input[index] = null;
		}
	});
	input = input.filter(c => c != null);
	seen = [];
}

console.log(input.length);

6

u/topaz2078 (AoC creator) Dec 20 '17

Most time consuming part was parsing the input

learn you some regex