From 527bc4762f9c66c9244f0ac1fbee6357478ac9ef Mon Sep 17 00:00:00 2001 From: HJ Date: Thu, 9 Dec 2021 18:05:04 -0500 Subject: putting all my solutions together in 1 repository --- 2021/day-06/day-06.rkt | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2021/day-06/day-06.rkt (limited to '2021/day-06/day-06.rkt') diff --git a/2021/day-06/day-06.rkt b/2021/day-06/day-06.rkt new file mode 100644 index 0000000..f687db9 --- /dev/null +++ b/2021/day-06/day-06.rkt @@ -0,0 +1,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) \ No newline at end of file -- cgit v1.2.3