#!/usr/bin/env python3
#Advent of Code
#2020 Day 23, Part 1
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrunfromcollectionsimportdequedefsolve(puzzle_input,moves=100):classCupsGame:def__init__(self,initial_order):self.cups=deque([int(cup)forcupininitial_order])defplay(self,moves):formoveinrange(moves):self.turn()defturn(self):current_cup=self.cups[0]self.cups.rotate(-1)removed_cups=[self.cups.popleft()forcupinrange(3)]destination=self.find_next_destination(current_cup)self.cups.rotate(-1-self.cups.index(destination))self.cups.extend(removed_cups)self.cups.rotate(-1-self.cups.index(current_cup))deffind_next_destination(self,current_cup):candidate=current_cup-1whileTrue:ifcandidate<min(self.cups):candidate=max(self.cups)ifcandidateinself.cups:returncandidatecandidate-=1deforder_from_one(self):cups=list(self.cups)return"".join([str(cup)forcupincups[cups.index(1)+1:]])+"".join([str(cup)forcupincups[:cups.index(1)]])game=CupsGame(puzzle_input)game.play(moves)returngame.order_from_one()defmain():puzzle_input=util.read.as_string()order=solve(puzzle_input)print("The order of the cups is "+str(order)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve("389125467",10),"92658374")deftest_ex2(self):returnself.assertEqual(solve("389125467"),"67384529")if__name__=="__main__":run(main)
#!/usr/bin/env python3
#Advent of Code
#2020 Day 23, Part 2
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrundefsolve(puzzle_input,moves=10_000_000):classNode:def__init__(self,value):self.value=valueself.next=Noneself.prev=Nonedeflink(prev_node,next_node):prev_node.next=next_nodenext_node.prev=prev_nodeclassCupsGame:def__init__(self,initial_order):initial_order=[int(x)forxininitial_order]self.min=min(initial_order)self.max=1_000_000self.current_cup=initial_order[0]self.cups=[Noneforxinrange(1_000_002)]last_created=Nonefirst_created=Noneforcupin[int(x)forxininitial_order]+[xforxinrange(max(initial_order)+1,1_000_001)]:new=Node(cup)self.cups[cup]=newiffirst_created==None:first_created=newiflast_created!=None:link(last_created,new)last_created=newlink(last_created,first_created)defplay(self,moves):formoveinrange(moves):self.turn()defturn(self):removed_cups=self.cups[self.current_cup].nextlink(self.cups[self.current_cup],self.cups[self.current_cup].next.next.next.next)destination=self.find_next_destination(self.current_cup,[removed_cups.value,removed_cups.next.value,removed_cups.next.next.value])destination_node=self.cups[destination]link(removed_cups.next.next,destination_node.next)link(destination_node,removed_cups)self.current_cup=self.cups[self.current_cup].next.valuedeffind_next_destination(self,current_cup,removed_cups):candidate=current_cup-1whileTrue:ifcandidate<self.min:candidate=self.maxifcandidatenotinremoved_cups:returncandidatecandidate-=1deflabel_product(self):returnself.cups[1].next.value*self.cups[1].next.next.valuegame=CupsGame(puzzle_input)game.play(moves)returngame.label_product()defmain():puzzle_input=util.read.as_string()labels=solve(puzzle_input)print("The product of the labels is "+str(labels)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve("389125467"),149245887792)if__name__=="__main__":run(main)