72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
import aoclib
|
|
|
|
DAY = 23
|
|
TEST_SOLUTION_PART1 = "67384529"
|
|
TEST_SOLUTION_PART2 = 149245887792
|
|
|
|
|
|
class Cup:
|
|
def __init__(self, label, right=None):
|
|
self.label = label
|
|
self.right = right
|
|
|
|
|
|
def buildLinkedListDict(cup_list):
|
|
cups = {}
|
|
left_cup = None
|
|
first_cup = None
|
|
for cup in cup_list:
|
|
thisCup = Cup(cup, None)
|
|
if first_cup is None:
|
|
first_cup = thisCup
|
|
else:
|
|
left_cup.right = thisCup
|
|
left_cup = thisCup
|
|
cups[cup] = thisCup
|
|
|
|
thisCup.right = first_cup
|
|
|
|
return cups, first_cup
|
|
|
|
|
|
def play(cups, current_cup, max_label):
|
|
first_pickup = current_cup.right
|
|
first_after_pickup = current_cup.right.right.right.right
|
|
current_cup.right = first_after_pickup
|
|
|
|
destination_label = current_cup.label - 1 if current_cup.label > 1 else max_label
|
|
while destination_label in [first_pickup.label, first_pickup.right.label, first_pickup.right.right.label]:
|
|
destination_label = destination_label - 1 if destination_label > 1 else max_label
|
|
|
|
desination_cup = cups[destination_label]
|
|
first_pickup.right.right.right = desination_cup.right
|
|
desination_cup.right = first_pickup
|
|
|
|
return cups, current_cup.right
|
|
|
|
|
|
def part1(test_mode=False):
|
|
my_input = aoclib.getInputAs2DArray(day=DAY, return_type=int, test=test_mode)
|
|
cups, current_cup = buildLinkedListDict(my_input[0])
|
|
|
|
for _ in range(100):
|
|
cups, current_cup = play(cups, current_cup, 9)
|
|
|
|
answer = ""
|
|
add_cup = cups[1]
|
|
while add_cup.right.label != 1:
|
|
answer += str(add_cup.right.label)
|
|
add_cup = add_cup.right
|
|
|
|
return answer
|
|
|
|
|
|
def part2(test_mode=False):
|
|
my_input = aoclib.getInputAs2DArray(day=DAY, return_type=int, test=test_mode)
|
|
cups, current_cup = buildLinkedListDict(my_input[0] + list(range(max(my_input[0]) + 1, 1000001)))
|
|
|
|
for _ in range(10000000):
|
|
cups, current_cup = play(cups, current_cup, 1000000)
|
|
|
|
return cups[1].right.label * cups[1].right.right.label
|