r/adventofcode • • Dec 07 '15

SOLUTION MEGATHREAD --- Day 7 Solutions ---

--- Day 7: Some Assembly Required ---

Post your solution as a comment. Structure your post like previous daily solution threads.

Also check out the sidebar - we added a nifty calendar to wrangle all the daily solution threads in one spot!

23 Upvotes

226 comments sorted by

View all comments

1

u/ThereOnceWasAMan Dec 07 '15

Solution with python 2. Uses back-propagation and is thus very fast (<2 ms runtime, for me):

import numpy as np

ops = {                                 
	"AND"   : lambda x,y: np.bitwise_and(x,y),      
	"OR"    : lambda x,y: np.bitwise_or(x,y),      
	"NOT"   : lambda x,y: np.invert(x),         
	"RSHIFT": lambda x,y: np.right_shift(x,y),     
	"LSHIFT": lambda x,y: np.left_shift(x,y),     
	None	: lambda x,y: x
}                                       

UIT = type(np.uint16(1))

def propagate(out, backlink):
	if type(out) == UIT:
		regs.update( {backlink: out} )
	
def procinput(x):
	if x is None: return None
	if not x.isdigit(): out = regs[x]() if not type(regs[x])==UIT else regs[x]
	else: out = np.uint16(x)
	return out


def linkfunc(backlink, fromreg1, fromreg2, operator):
	def func():
		out = ops[operator]( procinput(fromreg1), procinput(fromreg2) )
		propagate(out,backlink)
		return out
	return func

lines = [[ side.split() for side in line.strip().split("->")] for line in open("input7.dat","r").readlines()]

regs = {}
for line in lines:
	lside,rside = line
	toreg = rside[0]
	if   len(lside) == 1: fromreg1, fromreg2, operator = lside[0], None, None
	elif len(lside) == 2: fromreg1, fromreg2, operator = lside[1], None, lside[0]
	elif len(lside) == 3: fromreg1, fromreg2, operator = lside[0], lside[2], lside[1]
	regs.update( {toreg : linkfunc( toreg, fromreg1, fromreg2, operator)} )

print "Contents of register 'a' is: ", regs["a"]()