alexandria-ocasio-cortez axiom-of-choice area-of-concern american-orthodox-church almost-optimal-coset solutions in the year of our lord 2021, I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

1 year ago
#lang racket
(require "lib/common.rkt"
1 year ago
fancy-app
math/array
math/matrix)
(define transition-matrix
(matrix [[0 1 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0]
[0 0 0 1 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 1 0 0]
[1 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 1]
[1 0 0 0 0 0 0 0 0]]))
(define (generate-initial-vector fish)
(define vec (make-vector 9 0))
1 year ago
(for ([f (in-list fish)])
1 year ago
(vector-set! vec f (add1 (vector-ref vec f))))
vec)
1 year ago
1 year ago
(define (simulate fish days)
(define V
(matrix* (matrix-expt transition-matrix days)
(->col-matrix (generate-initial-vector fish))))
(for/sum ([v (in-array V)]) v))
1 year ago
1 year ago
(define (day6a fish) (simulate fish 80))
(define (day6b fish) (simulate fish 256))
1 year ago
(module+ main
(call-with-input-file "data/day6.txt"
(λ (prt)
(define fish
(~> (port->string prt)
string-split
first
(string-split ",")
(map string->number _)))
(answer 6 1 (day6a fish))
(answer 6 2 (day6b fish)))))
(module+ test
(require rackunit)
(define test-fish '(3 4 3 1 2))
(check-equal? (day6a test-fish) 5934)
(check-equal? (day6b test-fish) 26984457539))