diff options
author | HJ <thechairman@thechairman.info> | 2024-02-03 15:42:02 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2024-02-03 15:42:02 -0500 |
commit | 89e359060388c44d87cd70dd8102c37db58b0384 (patch) | |
tree | e4c3588f542c7b7691f915ceb6d4497e0304635d | |
parent | 0c869b2782aeecb92dff232b46a499a3821f9f2c (diff) | |
download | gleam_aoc-89e359060388c44d87cd70dd8102c37db58b0384.tar.gz gleam_aoc-89e359060388c44d87cd70dd8102c37db58b0384.zip |
2018 day 2 complete
-rw-r--r-- | aoc2018/day-02/day-02.rkt | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/aoc2018/day-02/day-02.rkt b/aoc2018/day-02/day-02.rkt new file mode 100644 index 0000000..38155fb --- /dev/null +++ b/aoc2018/day-02/day-02.rkt @@ -0,0 +1,27 @@ +#lang racket + +(require advent-of-code + threading) + +(define ids (port->lines (open-aoc-input (find-session) 2018 2 #:cache #true))) + +;; part 1 +(define (make-baskets str) + (for/fold ([baskets (hash)]) ([ch (in-string str)]) + (hash-update baskets ch add1 0))) + +(define (has-count n ht) + (member n (hash-values ht))) + +(for/fold ([two 0] [three 0] #:result (* two three)) ([id (in-list ids)]) + (define baskets (make-baskets id)) + (values (if (has-count 2 baskets) (add1 two) two) (if (has-count 3 baskets) (add1 three) three))) + +;; part 2 +(define (string-difference str1 str2) + (for/sum ([ch1 (in-string str1)] [ch2 (in-string str2)]) (if (equal? ch1 ch2) 0 1))) + +(for*/first ([id1 (in-list ids)] [id2 (in-list ids)] #:when (= 1 (string-difference id1 id2))) + (~>> (for/list ([ch1 (in-string id1)] [ch2 (in-string id2)] #:when (equal? ch1 ch2)) + ch1) + (apply string))) |