2020aoc/day23.py

133 lines
3.4 KiB
Python

input_test = '389125467'
input = '476138259'
moves = 10000000
max_cups = 1000000
cups = [int(c) for c in input] + [i for i in range(10, max_cups+1)]
# part 1 structure
# max_cups = 9
# cups = [int(c) for c in input]
# part one, with poor performance
#
# def calc_destination(destination, cups):
# global max_cups
# while destination not in cups:
# destination -= 1
# if destination < 1:
# destination = max_cups
# return destination
#
#
# def calc_next_cup(current_cup, cups):
# new_current_cup_index = cups.index(current_cup)
# return (new_current_cup_index + 1) % len(cups)
#
#
# def play(cups):
# current_cup_index = 0
# for m in range(moves):
# if m % 1000 == 0:
# print(m)
# current_cup = cups[current_cup_index]
# if current_cup_index + 4 < len(cups):
# pick = cups[current_cup_index+1:current_cup_index+4]
# cups = cups[:current_cup_index+1] + cups[current_cup_index+4:]
# else:
# pick = cups[current_cup_index+1:] + cups[:current_cup_index+4-len(cups)]
# cups = cups[current_cup_index+4-len(cups):current_cup_index+1]
#
# destination = calc_destination(current_cup - 1, cups)
#
# dest_index = cups.index(destination)
#
# print("{}, {}, {}, {}".format(current_cup, current_cup_index, destination, pick))
#
# cups = cups[:dest_index+1] + pick + cups[dest_index+1:]
#
# current_cup_index = calc_next_cup(current_cup, cups)
# return cups
#
#
# end = play(cups)
#
# index_one = end.index(1)
#
# final = end[index_one+1:] + end[:index_one]
# print(''.join([str(i) for i in final]))
def get_cup_dict(cups):
cup_dict = {}
for i, c in enumerate(cups):
if i == 0:
cup_dict[c] = (cups[-1], cups[i + 1])
elif i == len(cups) - 1:
cup_dict[c] = (cups[i-1], cups[0])
else:
cup_dict[c] = (cups[i-1], cups[i+1])
return cup_dict
def calc_destination_simple(current_cup, not_available):
global max_cups
destination = current_cup-1
while destination in not_available or destination < 1:
if destination < 1:
destination = max_cups
continue
destination -= 1
return destination
def play2(cups):
cup_dict = get_cup_dict(cups)
current_cup = cups[0]
for m in range(moves):
if m % 1000 == 0:
print(m)
current_cup_next_prev, first = cup_dict[current_cup]
first_prev, second = cup_dict[first]
_, third = cup_dict[second]
third_prev, fourth = cup_dict[third]
_, fourth_next = cup_dict[fourth]
cup_dict[fourth] = (current_cup, fourth_next)
cup_dict[current_cup] = (current_cup_next_prev, fourth)
pick = {first, second, third}
destination = calc_destination_simple(current_cup, pick)
destination_prev, destination_next = cup_dict[destination]
cup_dict[destination] = (destination_prev, first)
cup_dict[first] = (destination, second)
cup_dict[third] = (second, destination_next)
_, current_cup = cup_dict[current_cup]
return cup_dict
end = play2(cups)
_, first_after_one = end[1]
_, second_after_one = end[first_after_one]
print(first_after_one)
print(second_after_one)
print(first_after_one * second_after_one)
# get result for part1
#
# _, current = end[1]
#
# res = ''
# while current != 1:
# res += str(current)
# _, current = end[current]
#
# print(res)