From e6e748b779098a9c80c07940fb78ff8de491e671 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 25 Dec 2019 19:24:14 -0800 Subject: [PATCH] d22 p2 (stuck) --- 2019/22.rkt | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/2019/22.rkt b/2019/22.rkt index 2160e07..e75b0b4 100644 --- a/2019/22.rkt +++ b/2019/22.rkt @@ -53,3 +53,37 @@ cut -1") ;; 1 (check-eq? (index-of (run (file->string "22.rktd") 10007) 2019) 4086) + + +;; 2 +#| +The idea is that the numbers are too large to shuffle the deck directly. +Unlike the previous question, this one asks what card is at a certain position. +So we want to go in reverse: just trace that position backwards through each operation +and figure out where we are at the start. +Also, we are likely going to discover a cycle in the transformations +(that is, card 2020 returns to position 2020 after a while) +and we can use this cycle length to reduce the number of times we have to execute the (un)shuffling. +("Likely" because otherwise this puzzle cannot be computed in reasonable time.) +|# + +(define (unstack-pos cardlen pos) (- (sub1 cardlen) pos)) + +(check-eq? (unstack-pos 10 0) 9) +(check-eq? (unstack-pos 10 9) 0) + +(define (uncut-pos cardlen pos n) + (define cut-pos (match n + [(? positive?) n] + [_ (- cardlen (abs n))])) + (cond + [(<= (- cardlen cut-pos) pos) (- pos (- cardlen cut-pos))] + [else (+ pos cut-pos)])) + +(check-eq? (uncut-pos 10 7 3) 0) +(check-eq? (uncut-pos 10 0 3) 3) +(check-eq? (uncut-pos 10 7 -4) 3) +(check-eq? (uncut-pos 10 0 -4) 6) + +(define (unincrement-pos cardlen pos inc) + 42) \ No newline at end of file