#!/usr/bin/env python3
#Advent of Code
#2016 Day 10, Part 1
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrunbotList={}outList={}defsolve(puzzle_input):classBot:def__init__(self,name,output=False):self.inventory=[]self.name=nameifnotoutput:globalbotListbotList[self.getName()]=selfelse:globaloutListoutList[self.getName()]=selfdefaccepting_instructions(self):returnlen(self.inventory)==2defgetName(self):returnself.namedefgive(self,other,value):ifvalue=="high":other.take(self.inventory.pop())else:other.take(self.inventory.pop(0))deftake(self,value):self.inventory.append(int(value))self.inventory.sort()ifself.inventory==[17,61]:print("The bot that compares value-61 and value-17 microchips is "+self.getName()+".")defgetBotById(id):ifidinbotList:returnbotList[id]else:returnBot(id)defgetOutputById(id):ifidinoutList:returnoutList[id]else:returnBot(id,True)whilelen(puzzle_input)!=0:i=0whilei<len(puzzle_input):j=puzzle_input[i]ifj.split("")[0]=="value":getBotById(j.split("")[-1]).take(j.split("")[1])puzzle_input.pop(i)i=-1elifj.split("")[0]=="bot":ifgetBotById(j.split("")[1]).accepting_instructions():low,high=None,Noneifj.split("")[5]=="bot":low=getBotById(j.split("")[6])else:low=getOutputById(j.split("")[6])ifj.split("")[-2]=="bot":high=getBotById(j.split("")[-1])else:high=getOutputById(j.split("")[-1])ifnotlow.accepting_instructions()andnothigh.accepting_instructions():getBotById(j.split("")[1]).give(low,"low")getBotById(j.split("")[1]).give(high,"high")puzzle_input.pop(i)i=-1i+=1defmain():puzzle_input=util.read.as_lines()solve(puzzle_input)classAOC_Tests(unittest.TestCase):@unittest.skip("No test cases")deftest_ex1(self):passif__name__=="__main__":run(main)
#!/usr/bin/env python3
#Advent of Code
#2016 Day 10, Part 2
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrunbotList={}outList={}defsolve(puzzle_input):classBot:def__init__(self,name,output=False):self.inventory=[]self.name=nameifnotoutput:globalbotListbotList[self.getName()]=selfelse:globaloutListoutList[self.getName()]=selfdefaccepting_instructions(self):returnlen(self.inventory)==2defgetName(self):returnself.namedefgive(self,other,value):ifvalue=="high":other.take(self.inventory.pop())else:other.take(self.inventory.pop(0))deftake(self,value):self.inventory.append(int(value))self.inventory.sort()defgetBotById(id):ifidinbotList:returnbotList[id]else:returnBot(id)defgetOutputById(id):ifidinoutList:returnoutList[id]else:returnBot(id,True)whilelen(puzzle_input)!=0:i=0whilei<len(puzzle_input):j=puzzle_input[i]ifj.split("")[0]=="value":getBotById(j.split("")[-1]).take(j.split("")[1])puzzle_input.pop(i)i=-1elifj.split("")[0]=="bot":ifgetBotById(j.split("")[1]).accepting_instructions():low,high=None,Noneifj.split("")[5]=="bot":low=getBotById(j.split("")[6])else:low=getOutputById(j.split("")[6])ifj.split("")[-2]=="bot":high=getBotById(j.split("")[-1])else:high=getOutputById(j.split("")[-1])ifnotlow.accepting_instructions()andnothigh.accepting_instructions():getBotById(j.split("")[1]).give(low,"low")getBotById(j.split("")[1]).give(high,"high")puzzle_input.pop(i)i=-1i+=1returngetOutputById("0").inventory[0]*getOutputById("1").inventory[0]*getOutputById("2").inventory[0]defmain():puzzle_input=util.read.as_lines()mult_values=solve(puzzle_input)print("The multiplied values of the microchips in the output bins equal "+str(mult_values)+".")classAOC_Tests(unittest.TestCase):@unittest.skip("No test cases")deftest_ex1(self):passif__name__=="__main__":run(main)