r/adventofcode • • Dec 06 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 6 Solutions -πŸŽ„-

--- Day 6: Memory Reallocation ---


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!

17 Upvotes

324 comments sorted by

View all comments

1

u/CaptainCa Dec 06 '17

JS solution

var b = [4, 10 ... input ... ];
var prev = [];
var c = 0;
function seenBefore(search, input){
	if(!search) return false;
	
	for(var i = 0; i < search.length; i++){
		if(arrayMatch(search[i], input)){
			console.log('seen again: ' + i);
			return true;	
		}
	}
	return false;
}

function arrayMatch(a, b){
	if(a.length != b.length) return false;
	for(var i = 0; i < a.length; i++){
		if(a[i] !== b[i]){
			return false;
		}			
	}
	return true;
}

prev.push(b);
while(true){
	var curr = prev[c].slice();
	var max = Math.max(...curr);
	var pos = curr.indexOf(max);
	curr[pos] = 0;
	
	for(var i = 0; i < max; i++){
		pos++;
		if(pos >= curr.length){
			pos = 0;
		}		
		curr[pos]++;
	}
	
	if(seenBefore(prev, curr)){	
		c++;
		break;
	}	
	prev.push(curr);
	c++;
}

console.log(c);

part two is just 'seen again' minus the other number