Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
7b6c3bfa2a
22
day23.py
22
day23.py
@ -6,18 +6,17 @@ TEST_SOLUTION_PART2 = 149245887792
|
|||||||
|
|
||||||
|
|
||||||
class Cup:
|
class Cup:
|
||||||
def __init__(self, label, left=None, right=None):
|
def __init__(self, label, right=None):
|
||||||
self.label = label
|
self.label = label
|
||||||
self.left = left
|
|
||||||
self.right = right
|
self.right = right
|
||||||
|
|
||||||
|
|
||||||
def buildDoubleLinkedListDict(cup_list):
|
def buildLinkedListDict(cup_list):
|
||||||
cups = {}
|
cups = {}
|
||||||
left_cup = None
|
left_cup = None
|
||||||
first_cup = None
|
first_cup = None
|
||||||
for cup in cup_list:
|
for cup in cup_list:
|
||||||
thisCup = Cup(cup, left_cup, None)
|
thisCup = Cup(cup, None)
|
||||||
if first_cup is None:
|
if first_cup is None:
|
||||||
first_cup = thisCup
|
first_cup = thisCup
|
||||||
else:
|
else:
|
||||||
@ -25,7 +24,6 @@ def buildDoubleLinkedListDict(cup_list):
|
|||||||
left_cup = thisCup
|
left_cup = thisCup
|
||||||
cups[cup] = thisCup
|
cups[cup] = thisCup
|
||||||
|
|
||||||
first_cup.left = thisCup
|
|
||||||
thisCup.right = first_cup
|
thisCup.right = first_cup
|
||||||
|
|
||||||
return cups, first_cup
|
return cups, first_cup
|
||||||
@ -35,25 +33,21 @@ def play(cups, current_cup, max_label):
|
|||||||
first_pickup = current_cup.right
|
first_pickup = current_cup.right
|
||||||
first_after_pickup = current_cup.right.right.right.right
|
first_after_pickup = current_cup.right.right.right.right
|
||||||
current_cup.right = first_after_pickup
|
current_cup.right = first_after_pickup
|
||||||
first_after_pickup.left = current_cup
|
|
||||||
|
|
||||||
destination_label = current_cup.label - 1 if current_cup.label > 1 else max_label
|
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]:
|
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
|
destination_label = destination_label - 1 if destination_label > 1 else max_label
|
||||||
|
|
||||||
desination_cup = cups[destination_label]
|
desination_cup = cups[destination_label]
|
||||||
desination_right = desination_cup.right
|
first_pickup.right.right.right = desination_cup.right
|
||||||
desination_cup.right = first_pickup
|
desination_cup.right = first_pickup
|
||||||
first_pickup.left = desination_cup
|
|
||||||
desination_right.left = first_pickup.right.right
|
|
||||||
first_pickup.right.right.right = desination_right
|
|
||||||
|
|
||||||
return cups, current_cup.right
|
return cups, current_cup.right
|
||||||
|
|
||||||
|
|
||||||
def part1(test_mode=False):
|
def part1(test_mode=False):
|
||||||
my_input = aoclib.getInputAs2DArray(day=DAY, return_type=int, test=test_mode)
|
my_input = aoclib.getInputAs2DArray(day=DAY, return_type=int, test=test_mode)
|
||||||
cups, current_cup = buildDoubleLinkedListDict(my_input[0])
|
cups, current_cup = buildLinkedListDict(my_input[0])
|
||||||
|
|
||||||
for _ in range(100):
|
for _ in range(100):
|
||||||
cups, current_cup = play(cups, current_cup, 9)
|
cups, current_cup = play(cups, current_cup, 9)
|
||||||
@ -69,9 +63,9 @@ def part1(test_mode=False):
|
|||||||
|
|
||||||
def part2(test_mode=False):
|
def part2(test_mode=False):
|
||||||
my_input = aoclib.getInputAs2DArray(day=DAY, return_type=int, test=test_mode)
|
my_input = aoclib.getInputAs2DArray(day=DAY, return_type=int, test=test_mode)
|
||||||
cups, current_cup = buildDoubleLinkedListDict(my_input[0] + list(range(max(my_input[0]) + 1, 1000001)))
|
cups, current_cup = buildLinkedListDict(my_input[0] + list(range(max(my_input[0]) + 1, 1_000_001)))
|
||||||
|
|
||||||
for _ in range(10000000):
|
for _ in range(10_000_000):
|
||||||
cups, current_cup = play(cups, current_cup, 1000000)
|
cups, current_cup = play(cups, current_cup, 1_000_000)
|
||||||
|
|
||||||
return cups[1].right.label * cups[1].right.right.label
|
return cups[1].right.label * cups[1].right.right.label
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user