day23: a bit of cleanup
This commit is contained in:
parent
3dca21452a
commit
5f0fb357bd
31
day23.py
31
day23.py
@ -16,39 +16,32 @@ def buildDoubleLinkedListDict(cup_list):
|
||||
cups = {}
|
||||
left_cup = None
|
||||
first_cup = None
|
||||
last_cup = None
|
||||
for cup in cup_list:
|
||||
thisCup = Cup(cup, left_cup, None)
|
||||
if first_cup is None:
|
||||
first_cup = thisCup
|
||||
else:
|
||||
last_cup.right = thisCup
|
||||
left_cup.right = thisCup
|
||||
left_cup = thisCup
|
||||
last_cup = thisCup
|
||||
cups[cup] = thisCup
|
||||
|
||||
first_cup.left = last_cup
|
||||
last_cup.right = first_cup
|
||||
first_cup.left = thisCup
|
||||
thisCup.right = first_cup
|
||||
|
||||
return cups, first_cup
|
||||
|
||||
|
||||
def play(cups, current_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
|
||||
first_after_pickup.left = current_cup
|
||||
|
||||
pickup_cups = [first_pickup.label, first_pickup.right.label, first_pickup.right.right.label]
|
||||
|
||||
current_label = current_cup.label - 1 if current_cup.label > 1 else max(cups.keys())
|
||||
while current_label in pickup_cups:
|
||||
current_label -= 1
|
||||
if current_label == 0:
|
||||
current_label = max(cups.keys())
|
||||
|
||||
desination_cup = cups[current_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]:
|
||||
destination_label = destination_label - 1 if destination_label > 1 else max_label
|
||||
|
||||
desination_cup = cups[destination_label]
|
||||
desination_right = desination_cup.right
|
||||
desination_cup.right = first_pickup
|
||||
first_pickup.left = desination_cup
|
||||
@ -62,8 +55,8 @@ def part1(test_mode=False):
|
||||
my_input = aoclib.getInputAs2DArray(day=DAY, return_type=int, test=test_mode)
|
||||
cups, current_cup = buildDoubleLinkedListDict(my_input[0])
|
||||
|
||||
for round in range(100):
|
||||
cups, current_cup = play(cups, current_cup)
|
||||
for _ in range(100):
|
||||
cups, current_cup = play(cups, current_cup, 9)
|
||||
|
||||
answer = ""
|
||||
add_cup = cups[1]
|
||||
@ -78,7 +71,7 @@ def part2(test_mode=False):
|
||||
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)))
|
||||
|
||||
for round in range(10000000):
|
||||
cups, current_cup = play(cups, current_cup)
|
||||
for _ in range(10000000):
|
||||
cups, current_cup = play(cups, current_cup, 1000000)
|
||||
|
||||
return cups[1].right.label * cups[1].right.right.label
|
||||
|
||||
Loading…
Reference in New Issue
Block a user