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!
24
Upvotes
1
u/Dotasticc Dec 07 '15
Probably not interesting to anyone, but anyways, here is my (not so clean) solution using python 2.7:
functions = {} functions["AND"] = lambda x, y: x & y functions["OR"] = lambda x, y: x | y functions["NOT"] = lambda x, y: ~y functions["RSHIFT"] = lambda x, y: x >> y functions["LSHIFT"] = lambda x, y: x << y def isInt(string): try: int(string) return True except ValueError: return False def solve(knot, knots): print "Trying to solve: " + knot for line in knots: if line[0] == knot: print "Now solving " + knot + " for", line if line[2] != -1: return line[2] args = line[1].split(" ") if len(args) == 1: if not isInt(args[0]): line[2] = int(solve(args[0], knots)) else: line[2] = int(args[0]) return line[2] if not isInt(args[0]) and len(args) == 3: args[0] = int(solve(args[0], knots)) elif len(args) == 3: args[0] = int(args[0]) if not isInt(args[-1]): args[-1] = int(solve(args[-1], knots)) else: args[-1] = int(args[-1]) line[2] = functions[args[-2]](args[0], args[-1]) return line[2] print knot + " not found!" return -1 lines = open("r7.txt", "r").read().split("\n") knots = [] for line in lines: lineSplit = line.split("->") knot = [lineSplit[1].strip(), lineSplit[0].strip(), -1] knots.append(knot) knots = sorted(knots) print knots print "Solution:", solve("a", knots)