r/backtickbot • • Dec 23 '20

https://np.reddit.com/r/adventofcode/comments/kimluc/2020_day_23_solutions/ggrtpix/

Python, 199/55

My part1 was a hacky mess involving repeated list slicing and .indexing, because I didn't care at all about performance. Part2 forced me to think about performance, so I did it with a doubly-linked list for fast insertions/removals (implemented as a 3-element list :p), using a dictionary to keep track of nodes as they move.

import itertools as it

pattern = [4, 5, 9, 6, 7, 2, 8, 1, 3]
# pattern = [3, 8, 9, 1, 2, 5, 4, 6, 7]

head = [pattern[0], None, None]
prev = head
for n in it.chain(pattern[1:], range(max(pattern)+1, 1000000+1)):
    node = [n, prev, None]
    prev[2] = node
    prev = node
head[1] = prev
prev[2] = head

nodes = {}
curr = head
for _ in range(1000000):
    nodes[curr[0]] = curr
    curr = curr[2]

for i in range(10000000):
    pickl = curr[2]
    pickr = pickl[2][2]
    picked = {pickl[0], pickl[2][0], pickr[0]}
    curr[2] = pickr[2]
    curr[2][1] = curr
    destv = (curr[0] - 1) % 1000000
    if destv == 0: destv = 1000000
    while destv in picked:
        destv = (destv - 1) % 1000000
        if destv == 0: destv = 1000000
    dest = nodes[destv]
    # i love linked lists 8)
    pickr[2] = dest[2]
    pickr[2][1] = pickr
    dest[2] = pickl
    pickl[1] = dest
    curr = curr[2]

print(nodes[1][2][0], nodes[1][2][2][0])
1 Upvotes

0 comments sorted by