d22 p2 (stuck)

master
Matthew Butterick 4 years ago
parent 70fbb19f3e
commit e6e748b779

@ -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)