Skip to content

Commit d9e04bb

Browse files
committed
added 2020/23
1 parent 0c03b18 commit d9e04bb

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

2020/23/README.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 2020, Day 23: Crab Cups
2+
3+
The small crab challenges _you_ to a game! The crab is going to mix up some cups, and you have to predict where they'll end up.
4+
5+
## Part 1
6+
7+
The cups will be arranged in a circle and labeled _clockwise_ (your puzzle input). For example, if your labeling were `32415`, there would be five cups in the circle; going clockwise around the circle from the first cup, the cups would be labeled `3`, `2`, `4`, `1`, `5`, and then back to `3` again.
8+
9+
Before the crab starts, it will designate the first cup in your list as the _current cup_. The crab is then going to do _100 moves_.
10+
11+
Each _move_, the crab does the following actions:
12+
13+
* The crab picks up the _three cups_ that are immediately _clockwise_ of the _current cup_. They are removed from the circle; cup spacing is adjusted as necessary to maintain the circle.
14+
* The crab selects a _destination cup_: the cup with a _label_ equal to the _current cup's_ label minus one. If this would select one of the cups that was just picked up, the crab will keep subtracting one until it finds a cup that wasn't just picked up. If at any point in this process the value goes below the lowest value on any cup's label, it _wraps around_ to the highest value on any cup's label instead.
15+
* The crab places the cups it just picked up so that they are _immediately clockwise_ of the destination cup. They keep the same order as when they were picked up.
16+
* The crab selects a new _current cup_: the cup which is immediately clockwise of the current cup.
17+
18+
For example, suppose your cup labeling were `389125467`. If the crab were to do merely 10 moves, the following changes would occur:
19+
20+
-- move 1 --
21+
cups: (3) 8 9 1 2 5 4 6 7
22+
pick up: 8, 9, 1
23+
destination: 2
24+
25+
-- move 2 --
26+
cups: 3 (2) 8 9 1 5 4 6 7
27+
pick up: 8, 9, 1
28+
destination: 7
29+
30+
-- move 3 --
31+
cups: 3 2 (5) 4 6 7 8 9 1
32+
pick up: 4, 6, 7
33+
destination: 3
34+
35+
-- move 4 --
36+
cups: 7 2 5 (8) 9 1 3 4 6
37+
pick up: 9, 1, 3
38+
destination: 7
39+
40+
-- move 5 --
41+
cups: 3 2 5 8 (4) 6 7 9 1
42+
pick up: 6, 7, 9
43+
destination: 3
44+
45+
-- move 6 --
46+
cups: 9 2 5 8 4 (1) 3 6 7
47+
pick up: 3, 6, 7
48+
destination: 9
49+
50+
-- move 7 --
51+
cups: 7 2 5 8 4 1 (9) 3 6
52+
pick up: 3, 6, 7
53+
destination: 8
54+
55+
-- move 8 --
56+
cups: 8 3 6 7 4 1 9 (2) 5
57+
pick up: 5, 8, 3
58+
destination: 1
59+
60+
-- move 9 --
61+
cups: 7 4 1 5 8 3 9 2 (6)
62+
pick up: 7, 4, 1
63+
destination: 5
64+
65+
-- move 10 --
66+
cups: (5) 7 4 1 8 3 9 2 6
67+
pick up: 7, 4, 1
68+
destination: 3
69+
70+
-- final --
71+
cups: 5 (8) 3 7 4 1 9 2 6
72+
73+
74+
In the above example, the cups' values are the labels as they appear moving clockwise around the circle; the _current cup_ is marked with `( )`.
75+
76+
After the crab is done, what order will the cups be in? Starting _after the cup labeled `1`_, collect the other cups' labels clockwise into a single string with no extra characters; each number except `1` should appear exactly once. In the above example, after 10 moves, the cups clockwise from `1` are labeled `9`, `2`, `6`, `5`, and so on, producing _`92658374`_. If the crab were to complete all 100 moves, the order after cup `1` would be _`67384529`_.
77+
78+
Using your labeling, simulate 100 moves. _What are the labels on the cups after cup `1`?_
79+
80+
Your puzzle input was `463528179`.
81+
82+
Your puzzle answer was `52937846`.
83+
84+
## Part 2
85+
86+
Due to what you can only assume is a mistranslation (you're not exactly fluent in Crab), you are quite surprised when the crab starts arranging _many_ cups in a circle on your raft - _one million_ (`1000000`) in total.
87+
88+
Your labeling is still correct for the first few cups; after that, the remaining cups are just numbered in an increasing fashion starting from the number after the highest number in your list and proceeding one by one until one million is reached. (For example, if your labeling were `54321`, the cups would be numbered `5`, `4`, `3`, `2`, `1`, and then start counting up from `6` until one million is reached.) In this way, every number from one through one million is used exactly once.
89+
90+
After discovering where you made the mistake in translating Crab Numbers, you realize the small crab isn't going to do merely 100 moves; the crab is going to do _ten million_ (`10000000`) moves!
91+
92+
The crab is going to hide your _stars_ - one each - under the _two cups that will end up immediately clockwise of cup `1`_. You can have them if you predict what the labels on those cups will be when the crab is finished.
93+
94+
In the above example (`389125467`), this would be `934001` and then `159792`; multiplying these together produces _`149245887792`_.
95+
96+
Determine which two cups will end up immediately clockwise of cup `1`. _What do you get if you multiply their labels together?_
97+
98+
Your puzzle input was still `463528179`.
99+
100+
Your puzzle answer was `8456532414`.
101+
102+
103+
## Solution Notes
104+
105+
Part 1 is very straightforward, but part 2 blows apart completely. Forget about vectors (a.k.a. Python lists). Forget about actual linked lists in C, even! The only structure that makes runtime performance somewhat manageable is a mapping from a cup number to the next one in clockwise order. With that in place, the puzzle becomes relatively easy to solve -- but it's a highly non-obvious solution, making this one of the more infuriating puzzles.
106+
107+
The Python version is pretty slow, even with all tricks in place. C is 25 times faster, and compilation time doesn't even matter much.
108+
109+
* Part 1, Python: 197 bytes, <100 ms
110+
* Part 2, Python: 244 bytes, ~15 s
111+
* Part 2, C: 387 bytes, ~600 ms

2020/23/aoc2020_23_part1.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
N=map(int,"463528179")
2+
for t in range(100):
3+
c=N[:1];r=N[1:4];del N[:4];d=c[0]-1
4+
while not d in N:d=d-1if d>1else 9
5+
d=N.index(d)+1;N[d:d]=r;N+=c
6+
p=N.index(1);print''.join(map(str,N[p+1:]+N[:p]))

2020/23/aoc2020_23_part2.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#if 0
2+
cc $0;exec ./a.out
3+
#endif
4+
#include <stdio.h>
5+
char *I="463528179:";
6+
#define N 1000000
7+
int M[N+1],t,p,a,b,c,d,n;int main(){
8+
for(t=0;t<9;++t)M[I[t]-'0']=I[t+1]-'0';for(t=10;t<N;++t)M[t]=t+1;M[N]=p=I[0]-'0';
9+
for(t=10*N;t;--t){
10+
a=M[p];b=M[a];c=M[b];n=M[c];d=p;
11+
do d=(d>1)?(d-1):N;while (d==a||d==b||d==c);
12+
M[c]=M[d];M[d]=a;M[p]=n;p=n;}
13+
return!printf("%ld\n",(long)M[1]*(long)M[M[1]]);}

2020/23/aoc2020_23_part2.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
_,N=range,1000000
2+
A=map(int,"463528179")+_(10,N+1)
3+
M=[0]*(N+1);p=A[0]
4+
for i in _(N):M[A[i-1]]=A[i]
5+
for t in _(10*N):
6+
a=M[p];b=M[a];c=M[b];n=M[c];r={a,b,c,p};d=p
7+
while d in r:d=(d-1)if d>1else N
8+
M[c]=M[d];M[d]=a;M[p]=n;p=n
9+
print M[1]*M[M[1]]

2020/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
| [20](20) | ★★★★★ | ★★★★☆ | jigsaw puzzle solving and 2D pattern matching
2525
| [21](21) | ★★★☆☆ | ★★★★☆ | logic puzzle solver
2626
| [22](22) | ★★★☆☆ | ★★★☆☆ | game simulation
27+
| [23](23) | ★★☆☆☆ | ★★★★☆ | cyclic list operations

0 commit comments

Comments
 (0)