r/adventofcode • • Dec 16 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 16 Solutions -🎄-

--- Day 16: Chronal Classification ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 16

Transcript:

The secret technique to beat today's puzzles is ___.


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 at 00:39:03!

16 Upvotes

139 comments sorted by

View all comments

1

u/blfuentes Dec 16 '18

Well, one fun today. Yesterday is still pending ... My solution is quite extense because I implemented some classes and enums for the opcodes.

https://github.com/blfuentes/AdventOfCode_2018/tree/master/day16

Typescript

"happy" with the algorithm I created to resolve the opcodes.

function operatorsToResolve(){
	return operatorDictionary.filter(_o => _o.length > 0).length > 0;
}

let operatorsSolution: Array<OperatorType> = [];

function resolveOpCodes() {
	let tmpOperator = operatorDictionary.find(_o => _o.length == 1);
	let initialOperator = -1;
	let index = -1;
	if (tmpOperator != undefined) {
		index = operatorDictionary.indexOf(tmpOperator);
		initialOperator = tmpOperator[0];
		tmpOperator.pop();
		operatorsSolution[index] = initialOperator;
	}
	while (operatorsToResolve()) {
		// find next 
		for (let operator of operatorDictionary) {
			index = operator.indexOf(initialOperator);
			if (index != -1) {
				operator.splice(index, 1);
			}          
		}
		tmpOperator = operatorDictionary.find(_o => _o.length == 1);
		if (tmpOperator != undefined) {
			index = operatorDictionary.indexOf(tmpOperator);
			initialOperator = tmpOperator[0];
			tmpOperator.pop();
			operatorsSolution[index] = initialOperator;
		}
	}
}