diff options
author | HJ <thechairman@thechairman.info> | 2023-12-09 01:25:44 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2023-12-09 01:25:44 -0500 |
commit | 2f570466b75ca76636a692006bec59cbb3a038cf (patch) | |
tree | 880c0facb899178e25ea070e42d10d666d4e6ad1 /aoc2023-other | |
parent | b51dd4b3768bce0209733ef2562cb96e5330d3c6 (diff) | |
download | gleam_aoc-2f570466b75ca76636a692006bec59cbb3a038cf.tar.gz gleam_aoc-2f570466b75ca76636a692006bec59cbb3a038cf.zip |
day 9 complete
Diffstat (limited to 'aoc2023-other')
-rw-r--r-- | aoc2023-other/day-09/day-09.rkt | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/aoc2023-other/day-09/day-09.rkt b/aoc2023-other/day-09/day-09.rkt new file mode 100644 index 0000000..c632150 --- /dev/null +++ b/aoc2023-other/day-09/day-09.rkt @@ -0,0 +1,22 @@ +#lang racket + +(require advent-of-code + threading) + +(define histories + (for/list ([raw-history (in-lines (open-aoc-input (find-session) 2023 9 #:cache #true))]) + (~>> raw-history string-split (map string->number)))) + +(define (constant? xs) (= 1 (length (remove-duplicates xs)))) + +(define/match (derivative xs) + [((list a b)) (list (- b a))] + [((list* a b _)) (cons (- b a) (derivative (rest xs)))]) + +(define (extrapolate xs) (if (constant? xs) (car xs) (+ (last xs) (extrapolate (derivative xs))))) + +;; part 1 +(~>> histories (map extrapolate) (apply +)) + +;; part 2 +(~>> histories (map (λ~> reverse extrapolate)) (apply +)) |