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

3

u/tangentialThinker Dec 16 '18

C++, 53/34. Pretty interesting Part 2 today. Did some pretty lazy copy-pasting from Part 1 to run the program, but hey it was plenty fast enough.

#include <bits/stdc++.h>
using namespace std;

int main(){
	string s;
	char junk;
	vector<int> bef(4);
	vector<int> af(4);
	vector<int> op(4);

	int ans = 0;

	set<int> full;
	for(int i = 0; i < 16; i++) {
		full.insert(i);
	}
	vector<set<int>> poss(16, full);

	while(cin >> s) {
		// manually added SPLIT to input between the two parts
		if(s == "SPLIT") break;
		for(int i = 0; i < 4; i++) {
			cin >> junk >> bef[i];
		}
		cin >> junk;
		for(int i = 0; i < 4; i++) {
			cin >> op[i];
		}
		cin >> s;
		for(int i = 0; i < 4; i++) {
			cin >> junk >> af[i];
		}
		cin >> junk;
		int opCode = op[0];
		int A = op[1], B = op[2], C = op[3];
		// compute all expected final register states
		vector<vector<int>> afExpect;
		for(int i = 0; i < 16; i++) {
			afExpect.push_back(bef);
		}
		afExpect[0][C] = bef[A] + bef[B];
		afExpect[1][C] = bef[A] + B;

		afExpect[2][C] = bef[A] * bef[B];
		afExpect[3][C] = bef[A] * B;

		afExpect[4][C] = bef[A] & bef[B];
		afExpect[5][C] = bef[A] & B;

		afExpect[6][C] = bef[A] | bef[B];
		afExpect[7][C] = bef[A] | B;

		afExpect[8][C] = bef[A];
		afExpect[9][C] = A;

		afExpect[10][C] = A > bef[B];
		afExpect[11][C] = bef[A] > B;
		afExpect[12][C] = bef[A] > bef[B];

		afExpect[13][C] = A == bef[B];
		afExpect[14][C] = bef[A] == B;
		afExpect[15][C] = bef[A] == bef[B];
		int cnt = 0;
		for(int cur = 0; cur < 16; cur++) {
			bool match = true;
			for(int i = 0; i < 4; i++) {
				if(afExpect[cur][i] != af[i]) match = false;
			}
			// rule out impossible number-operation combos
			if(!match) poss[opCode].erase(cur);
		}
		ans++;
	}

	while(true) {
		int numOnes = 0;
		for(int i = 0; i < 16; i++) {
			// For any opcode with only one possibility,
			// we can remove the possibility from other opcodes.
			if(poss[i].size() == 1) {
				numOnes++;
				for(int j = 0; j < 16; j++) {
					if(i == j) continue;
					poss[j].erase(*poss[i].begin());
				}
			}
		}
		if(numOnes == 16) break;
	}

	int opCode, A, B, C;
	vector<int> reg(4, 0);
	while(cin >> opCode) {
		cin >> A >> B >> C;
		int op = *poss[opCode].begin();

		// Lazy copy-paste, but it's fast enough
		vector<vector<int>> afExpect;
		for(int i = 0; i < 16; i++) {
			afExpect.push_back(reg);
		}
		afExpect[0][C] = reg[A] + reg[B];
		afExpect[1][C] = reg[A] + B;
		
		afExpect[2][C] = reg[A] * reg[B];
		afExpect[3][C] = reg[A] * B;

		afExpect[4][C] = reg[A] & reg[B];
		afExpect[5][C] = reg[A] & B;

		afExpect[6][C] = reg[A] | reg[B];
		afExpect[7][C] = reg[A] | B;

		afExpect[8][C] = reg[A];
		afExpect[9][C] = A;

		afExpect[10][C] = A > reg[B];
		afExpect[11][C] = reg[A] > B;
		afExpect[12][C] = reg[A] > reg[B];

		afExpect[13][C] = A == reg[B];
		afExpect[14][C] = reg[A] == B;
		afExpect[15][C] = reg[A] == reg[B];

		reg = afExpect[op];
	}

	for(auto cur : reg) cout << cur << " " ;

	return 0;
}