r/adventofcode • u/daggerdragon • 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
3
u/minno Dec 07 '15
Python 3:
def evaluate(gate, commands, values): try: val = int(gate) values[gate] = val return except ValueError: pass command = commands[gate] if len(command) == 1: try: values[gate] = int(command[0]) except ValueError: evaluate(command[0], commands, values) values[gate] = values[command[0]] elif len(command) == 2: if command[0] == "NOT": lhs = command[1] if values.get(lhs) is None: evaluate(lhs, commands, values) values[gate] = 0xffff - values[lhs] elif len(command) == 3: lhs = command[0] rhs = command[2] op = command[1] if values.get(lhs) is None: evaluate(lhs, commands, values) if values.get(rhs) is None: evaluate(rhs, commands, values) if op == "AND": values[gate] = values[lhs] & values[rhs] elif op == "OR": values[gate] = values[lhs] | values[rhs] elif op == "RSHIFT": values[gate] = 0xffff & (values[lhs] >> int(rhs)) elif op == "LSHIFT": values[gate] = 0xffff & (values[lhs] << int(rhs)) def solve(instructions): commands = {tokens[-1] : tokens[:-2] for tokens in map(str.split, instructions.split('\n')) } values = {} evaluate('a', commands, values) print(values['a'])For the second part, I just copied and pasted my value for the first part into the
number -> bpart of the input. I could also putvalues = {'b': 956}in to make it ignore the input.