Skip to content

Instantly share code, notes, and snippets.

@rendyanthony
Created December 24, 2020 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rendyanthony/05863f6211eef180c795542796a92b42 to your computer and use it in GitHub Desktop.
Save rendyanthony/05863f6211eef180c795542796a92b42 to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day 23
def crab_cups(cycle, n=10):
cur_val = cycle[0]
max_val = max(cycle)
cycle_dict = dict(zip(cycle, cycle[1:]))
cycle_dict[cycle[-1]] = cycle[0]
def pick_up(n=3):
pick = [cycle_dict[cur_val]]
for _ in range(n-1):
pick.append(cycle_dict[pick[-1]])
return pick
for r in range(n):
pick = pick_up()
next_val = cur_val-1
while next_val <= 0 or next_val in pick:
next_val -= 1
if next_val <= 0:
next_val = max_val
cycle_dict[cur_val] = cycle_dict[pick[-1]]
cycle_dict[pick[-1]] = cycle_dict[next_val]
cycle_dict[next_val] = pick[0]
cur_val = cycle_dict[cur_val]
return cycle_dict
## Part 1
cycle = crab_cups(
[int(t) for t in list("123487596")], n=100)
result = [cycle[1]]
while result[-1] != 1:
result.append(cycle[result[-1]])
print("".join([str(c) for c in result[:-1]]))
## Part 2
puzzle_input = [int(t) for t in list("123487596")]+list(range(9+1, 1000000+1))
cycle = crab_cups(puzzle_input, n=10000000)
v1 = cycle[1]
v2 = cycle[v1]
print(v1, v2, v1*v2)
@rendyanthony
Copy link
Author

Here is an illustration on how the code works https://imgur.com/gallery/MRw6gMA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment