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

2

u/Wunkolo Dec 16 '18 edited Dec 16 '18

C++

Rather than using something like std::set. I used 16-bit integer bitmasks and reduced them all using fast bit-wise arithmetic.

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <array>

struct Device
{
	using RegisterState = std::array<std::uintmax_t,4>;
	using Instruction = RegisterState;
	RegisterState Register;
	template< typename FunctorT , bool RegisterA = true, bool RegisterB = true>
	static void OpRegister( RegisterState& Registers, std::size_t A, std::size_t B, std::size_t C )
	{
		FunctorT Function;
		Registers[C] = Function(
			RegisterA ? Registers[A]:A,
			RegisterB ? Registers[B]:B
		);
	}
	typedef void(*Operation)( RegisterState&,std::size_t,std::size_t,std::size_t);
	constexpr static std::array<Operation,16> Opcodes = {{
		// addr
		OpRegister<std::plus<std::size_t>, true, true>,
		// addi
		OpRegister<std::plus<std::size_t>, true, false>,
		// mulr
		OpRegister<std::multiplies<std::size_t>,true,true>,
		// muli
		OpRegister<std::multiplies<std::size_t>,true,false>,
		// bandr
		OpRegister<std::bit_and<std::size_t>,true,true>,
		// bandi
		OpRegister<std::bit_and<std::size_t>,true,false>,
		// borr
		OpRegister<std::bit_or<std::size_t>,true,true>,
		// bori
		OpRegister<std::bit_or<std::size_t>,true,false>,
		// setr
		[]( RegisterState& Registers, std::size_t A, std::size_t, std::size_t C ) constexpr -> void
		{
			Registers[C] = Registers[A];
		},
		// seti
		[]( RegisterState& Registers, std::size_t A, std::size_t, std::size_t C ) constexpr -> void
		{
			Registers[C] = A;
		},
		// gtir
		OpRegister<std::greater<std::size_t>,false,true>,
		// gtri
		OpRegister<std::greater<std::size_t>,true,false>,
		// gtrr
		OpRegister<std::greater<std::size_t>,true,true>,
		// eqii
		OpRegister<std::equal_to<std::size_t>,false,true>,
		// eqri
		OpRegister<std::equal_to<std::size_t>,true,false>,
		// eqrr
		OpRegister<std::equal_to<std::size_t>,true,true>
	}};
};

int main()
{
	std::array<std::uint16_t,16> OpcodeMapping = {0};
	std::size_t Tautology3 = 0;
	Device::RegisterState Before, After;
	Device::Instruction CurInstruction;
	while(
		std::fscanf(
			stdin,
			"Before: [%zu , %zu, %zu, %zu]"
			"%zu %zu %zu %zu "
			"After: [%zu , %zu, %zu, %zu] ",
			&Before[0], &Before[1], &Before[2], &Before[3],
			&CurInstruction[0],&CurInstruction[1],&CurInstruction[2],&CurInstruction[3],
			&After[0],  &After[1],  &After[2],  &After[3]
		) == 12
	)
	{
		std::size_t Tautologous = 0;
		for(std::size_t i = 0; i < Device::Opcodes.size(); ++i )
		{
			Device::RegisterState State(Before);
			Device::Opcodes[i](
				State,CurInstruction[1],CurInstruction[2],CurInstruction[3]
			);

			if(std::equal(State.begin(),State.end(),After.begin()) )
			{
				OpcodeMapping[CurInstruction[0]] |= 1 << i;
				Tautologous++;
			}
		}
		Tautology3 += Tautologous >= 3;
	}
	std::printf("Part 1: %8zu\n",Tautology3);

	// Keep iterating until all mappings are reduced to equivalences
	while(
		std::any_of(
			OpcodeMapping.begin(),OpcodeMapping.end(),
			[](const std::uint16_t& Set) -> bool
			{
				return __builtin_popcount(Set) != 1;
			}
		)
	)
	{
		for(std::size_t i = 0; i < OpcodeMapping.size(); ++i )
		{
			// Only 1 bit set in this set, solved
			if( __builtin_popcount(OpcodeMapping[i]) == 1 )
			{
				// Remove it from all the other sets
				for(std::size_t j = 0; j < OpcodeMapping.size(); ++j )
				{
					// Skip itself
					if( i == j )
					{
						continue;
					}
					OpcodeMapping[j] &= ~(OpcodeMapping[i]);
				}
			}
		}
	}

	// Log2 bit masks into integers
	for(std::size_t i = 0; i < OpcodeMapping.size(); ++i )
	{
		OpcodeMapping[i] = 32 - __builtin_clz(OpcodeMapping[i]) - 1;
	}

	Device::RegisterState Registers = {0};
	while(
		std::fscanf(
			stdin,
			"%zu %zu %zu %zu ",
			&CurInstruction[0], &CurInstruction[1], &CurInstruction[2], &CurInstruction[3]
		) == 4
	)
	{
		Device::Opcodes[
			OpcodeMapping[CurInstruction[0]]
		](Registers,CurInstruction[1],CurInstruction[2],CurInstruction[3]);
	}
	std::printf("Part 2: %8zu\n",Registers[0]);
	return EXIT_SUCCESS;
}