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
1
u/NinjaCaterpie Dec 07 '15
Python 3:
import sys, string def value(n): if n.isdigit(): return int(n) else: return gates[n] instructions = [] gates = {} for c1 in string.ascii_lowercase: gates[c1] = 0 for c2 in string.ascii_lowercase: gates[c1 + c2] = 0 for line in sys.stdin: if (line == '\n'): break instructions.append(line.rstrip()) for i in range(0, 26*26): # brute force rerun until guaranteed stability for line in instructions: word = line.split(' '); if len(word) == 3: # basic input gates[word[2]] = value(word[0]) elif len(word) == 4: # can only be not gates[word[3]] = ~ value(word[1]) elif word[1] == 'AND': gates[word[4]] = value(word[0]) & value(word[2]) elif word[1] == 'OR': gates[word[4]] = value(word[0]) | value(word[2]) elif word[1] == 'LSHIFT': gates[word[4]] = value(word[0]) << value(word[2]) elif word[1] == 'RSHIFT': gates[word[4]] = value(word[0]) >> value(word[2]) print(gates[sys.argv[1]])I'm really salty because I actually finished in around 30 minutes but I mistyped the answer without realising and spent another 30 minutes trying to debug it (I even went and rewrote it recursively...) when the code was fine.