blob: f687db93fafc1047d0c14a487fc38b0c20bacc15 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#lang racket
(require advent-of-code
awesome-list
threading
racket/hash)
(define fish-data
(~> (open-aoc-input (find-session) 2021 6 #:cache (string->path "./cache"))
port->string
string-trim
(string-split ",")
(map string->number _)))
(define (simulate-fish time-period)
(for/fold ([state (frequencies fish-data)]
#:result (~> state
hash-values
(apply + _)))
([day (inclusive-range 1 time-period)])
(define day-older-fish
(for/hash ([(days pop) (in-hash state)])
(values (sub1 days) pop)))
(define breeding-fish
(hash-ref day-older-fish -1 0))
(hash-union (hash-remove day-older-fish -1)
(hash 8 breeding-fish 6 breeding-fish)
#:combine +)))
;; part 1
(simulate-fish 80)
;; part 2
(simulate-fish 256)
|