After a bit of a hiatus for Christmas and family, I’m going to try to finish off the last couple problems before I have to go back to work on January 4th. Well at least the first part of each…

Part 1

I’ll start with the naive solution, which should suffice to get us through 100 moves. First parse the string into an array of Ints:

    ns = [parse(Int,x) for x in ins]

Each round I’ll construct the next ns such that the current cup is the first element in the list, this should simplify finding the destination cup, since I’ll just have to check elements [5:end] ([1] is current, [2:4] are picked up), which I’ll refer to as the remaining cups. So to decide which cup is the destination I get the maximum of all remaining cups that are less than the current, and if there are none then get the maximum of all remaining cups:

        ds = filter(x->x<ns[1], @view ns[5:end])
        d = isempty(ds) ? maximum(@view ns[5:end]) : maximum(ds)

I then find the index of the destination (probably room for improvement here: I never actually need the value of the destination cup), and construct the new cup array from four parts:

  1. The remaining cups up to and including the destination cup.
  2. The picked up cups.
  3. The remaining cup after the destination cup.
  4. The current cup.
        id = findfirst(x->x==d, ns)
        ns = [ns[5:id]; ns[2:4]; ns[id+1:end]; ns[1]]

After all the rounds I just need to re-assemble the list starting from after the one cup and join it into a string:

    i1 = findfirst(x->x==1, ns)
    join([ns[i1+1:end];ns[1:i1-1]])

Part 2

to be continued…


Next: Advent of Code 2020 Day 24
/Posts
/Photos
#adventofcode (25)
#apl (5)
#baking (3)
#biggreenegg (9)
#bike (1)
#chia (5)
#data-visualization (8)
#development (48)
#devops (4)
#docker (1)
#electronics (7)
#elixir (1)
#engineering (9)
#food (13)
#golang (1)
#home-improvement (3)
#julia (19)
#keto (1)
#keyboards (4)
#lisp (8)
#meta (1)
#monitoring (10)
#photos (13)
#races (2)
#racket (1)
#recipes (6)
#run (2)
#rust (1)
#swim (1)
#woodworking (12)