Initial commit
This commit is contained in:
commit
f242d2b0df
420 changed files with 62521 additions and 0 deletions
117
2019/day22/day22.rkt
Normal file
117
2019/day22/day22.rkt
Normal file
|
@ -0,0 +1,117 @@
|
|||
#lang racket
|
||||
|
||||
(require racket/string
|
||||
math/number-theory)
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
(module+ main
|
||||
(displayln "Day 22"))
|
||||
|
||||
(define (read-instructions filename)
|
||||
(with-input-from-file filename
|
||||
(lambda ()
|
||||
(for/list ([line (in-lines)])
|
||||
(cond
|
||||
[(string-prefix? line "deal into new stack") 'deal]
|
||||
[(string-prefix? line "cut") `(cut ,(string->number (last (string-split line " "))))]
|
||||
[(string-prefix? line "deal with increment") `(increment ,(string->number (last (string-split line " "))))]
|
||||
[else 'other])))))
|
||||
|
||||
(define (deal deck)
|
||||
(reverse deck))
|
||||
|
||||
(define (cut deck n)
|
||||
(define split-fn (if (> n 0) split-at split-at-right))
|
||||
(define-values (a b) (split-fn deck (abs n)))
|
||||
(append b a))
|
||||
|
||||
(define (increment deck n)
|
||||
(for/list ([i (in-range (length deck))])
|
||||
(list-ref deck
|
||||
(remainder (* i (modular-inverse n (length deck))) (length deck)))))
|
||||
|
||||
(define (execute instruction deck)
|
||||
(match instruction
|
||||
['deal (deal deck)]
|
||||
[(list 'cut n) (cut deck n)]
|
||||
[(list 'increment n) (increment deck n)]))
|
||||
|
||||
(define (shuffle deck instructions)
|
||||
(for/fold ([d deck]) ([i instructions])
|
||||
(execute i d)))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (shuffle (range 10) (read-instructions "test1")) '(0 3 6 9 2 5 8 1 4 7))
|
||||
(check-equal? (shuffle (range 10) (read-instructions "test2")) '(3 0 7 4 1 8 5 2 9 6))
|
||||
(check-equal? (shuffle (range 10) (read-instructions "test3")) '(6 3 0 7 4 1 8 5 2 9))
|
||||
(check-equal? (shuffle (range 10) (read-instructions "test4")) '(9 2 5 8 1 4 7 0 3 6)))
|
||||
|
||||
(module+ main
|
||||
(displayln "Part 1:")
|
||||
(define instructions (read-instructions "input"))
|
||||
(define deck (range 10007))
|
||||
(time (index-of (shuffle deck instructions) 2019)))
|
||||
|
||||
(define (deal-card deck-size card)
|
||||
(- (sub1 deck-size) card))
|
||||
|
||||
(define (cut-card deck-size n card)
|
||||
(modulo (- card n) deck-size))
|
||||
|
||||
(define (increment-card deck-size n card)
|
||||
(modulo (* n card) deck-size))
|
||||
|
||||
(define (execute-card deck-size instruction card)
|
||||
(match instruction
|
||||
['deal (deal-card deck-size card)]
|
||||
[(list 'cut n) (cut-card deck-size n card)]
|
||||
[(list 'increment n) (increment-card deck-size n card)]))
|
||||
|
||||
(define (shuffle-card deck-size instructions card)
|
||||
(for/fold ([c card]) ([i instructions])
|
||||
(execute-card deck-size i c)))
|
||||
|
||||
(module+ main
|
||||
(displayln "Part 1 (better):")
|
||||
(time (shuffle-card 10007 instructions 2019)))
|
||||
|
||||
(define (deal-card-inv deck-size card)
|
||||
(- (sub1 deck-size) card))
|
||||
|
||||
(define (cut-card-inv deck-size n card)
|
||||
(modulo (+ card n) deck-size))
|
||||
|
||||
(define (increment-card-inv deck-size n card)
|
||||
(remainder (* card (modular-inverse n deck-size)) deck-size))
|
||||
|
||||
(define (execute-card-inv deck-size instruction card)
|
||||
(match instruction
|
||||
['deal (deal-card-inv deck-size card)]
|
||||
[(list 'cut n) (cut-card-inv deck-size n card)]
|
||||
[(list 'increment n) (increment-card-inv deck-size n card)]))
|
||||
|
||||
(define (shuffle-card-inv deck-size reversed-instructions card)
|
||||
(for/fold ([c card]) ([i reversed-instructions])
|
||||
(execute-card-inv deck-size i c)))
|
||||
|
||||
(module+ main
|
||||
(displayln "Part 2:")
|
||||
(define deck-size 119315717514047)
|
||||
(define iterations 101741582076661)
|
||||
|
||||
;; Operations are linear, we find the coefficients and apply them directly
|
||||
(define x 2020)
|
||||
(define y (shuffle-card-inv deck-size (reverse instructions) x))
|
||||
(define z (shuffle-card-inv deck-size (reverse instructions) y))
|
||||
|
||||
(define a (modulo (* (- z y) (modular-inverse (- y x) deck-size)) deck-size))
|
||||
(define b (modulo (- y (* a x)) deck-size))
|
||||
|
||||
(modulo (+ (* (modular-expt a iterations deck-size)
|
||||
x)
|
||||
(* (sub1 (modular-expt a iterations deck-size))
|
||||
(modular-inverse (sub1 a) deck-size)
|
||||
b))
|
||||
deck-size))
|
100
2019/day22/input
Normal file
100
2019/day22/input
Normal file
|
@ -0,0 +1,100 @@
|
|||
deal with increment 55
|
||||
cut -6984
|
||||
deal into new stack
|
||||
cut -2833
|
||||
deal with increment 75
|
||||
cut 2488
|
||||
deal with increment 54
|
||||
cut 9056
|
||||
deal with increment 52
|
||||
cut -2717
|
||||
deal with increment 4
|
||||
deal into new stack
|
||||
cut -852
|
||||
deal with increment 21
|
||||
cut -3041
|
||||
deal with increment 38
|
||||
cut -6871
|
||||
deal into new stack
|
||||
deal with increment 32
|
||||
cut 988
|
||||
deal with increment 29
|
||||
deal into new stack
|
||||
deal with increment 68
|
||||
cut 5695
|
||||
deal with increment 36
|
||||
cut -27
|
||||
deal with increment 33
|
||||
deal into new stack
|
||||
cut -1306
|
||||
deal with increment 30
|
||||
cut -4033
|
||||
deal with increment 28
|
||||
cut -442
|
||||
deal into new stack
|
||||
deal with increment 30
|
||||
cut -6295
|
||||
deal with increment 56
|
||||
cut -4065
|
||||
deal into new stack
|
||||
cut 5275
|
||||
deal with increment 64
|
||||
cut 9747
|
||||
deal into new stack
|
||||
deal with increment 63
|
||||
cut -3772
|
||||
deal with increment 61
|
||||
deal into new stack
|
||||
cut 1021
|
||||
deal with increment 73
|
||||
deal into new stack
|
||||
deal with increment 7
|
||||
cut -1232
|
||||
deal with increment 52
|
||||
cut -3439
|
||||
deal with increment 31
|
||||
cut 1128
|
||||
deal into new stack
|
||||
deal with increment 55
|
||||
deal into new stack
|
||||
deal with increment 39
|
||||
cut -3424
|
||||
deal with increment 11
|
||||
deal into new stack
|
||||
cut 4139
|
||||
deal with increment 15
|
||||
deal into new stack
|
||||
cut 5333
|
||||
deal with increment 16
|
||||
cut -6787
|
||||
deal with increment 39
|
||||
cut -5817
|
||||
deal into new stack
|
||||
deal with increment 62
|
||||
cut -2704
|
||||
deal with increment 64
|
||||
deal into new stack
|
||||
deal with increment 70
|
||||
cut 3436
|
||||
deal with increment 65
|
||||
cut -8686
|
||||
deal with increment 22
|
||||
cut -6190
|
||||
deal with increment 13
|
||||
cut -100
|
||||
deal into new stack
|
||||
cut -619
|
||||
deal into new stack
|
||||
cut 3079
|
||||
deal with increment 53
|
||||
cut 1725
|
||||
deal with increment 19
|
||||
cut 3440
|
||||
deal with increment 64
|
||||
cut 8578
|
||||
deal with increment 5
|
||||
cut 2341
|
||||
deal with increment 45
|
||||
cut 2217
|
||||
deal with increment 13
|
||||
deal into new stack
|
3
2019/day22/test1
Normal file
3
2019/day22/test1
Normal file
|
@ -0,0 +1,3 @@
|
|||
deal with increment 7
|
||||
deal into new stack
|
||||
deal into new stack
|
3
2019/day22/test2
Normal file
3
2019/day22/test2
Normal file
|
@ -0,0 +1,3 @@
|
|||
cut 6
|
||||
deal with increment 7
|
||||
deal into new stack
|
3
2019/day22/test3
Normal file
3
2019/day22/test3
Normal file
|
@ -0,0 +1,3 @@
|
|||
deal with increment 7
|
||||
deal with increment 9
|
||||
cut -2
|
10
2019/day22/test4
Normal file
10
2019/day22/test4
Normal file
|
@ -0,0 +1,10 @@
|
|||
deal into new stack
|
||||
cut -2
|
||||
deal with increment 7
|
||||
cut 8
|
||||
cut -4
|
||||
deal with increment 7
|
||||
cut 3
|
||||
deal with increment 9
|
||||
deal with increment 3
|
||||
cut -1
|
Loading…
Add table
Add a link
Reference in a new issue