aboutsummaryrefslogtreecommitdiff
path: root/aoc2018/day-02
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
committerJ.J <thechairman@thechairman.info>2024-05-30 21:49:58 -0400
commit231c2b688d1e6cf0846d46e883da30e042a9c6cf (patch)
tree98a6d3a461fe190b38b2cf33a708a1d01703fa70 /aoc2018/day-02
parentfe088aa5778dcdbaab4dd8d4a7395a91c444b45c (diff)
parenta2c2b728ec6051323ed937f54816089cd2ae9d20 (diff)
downloadgleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.tar.gz
gleam_aoc-231c2b688d1e6cf0846d46e883da30e042a9c6cf.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2018/day-02')
-rw-r--r--aoc2018/day-02/day-02.rkt27
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)))