aboutsummaryrefslogtreecommitdiff
path: root/2020/day-07/day-07.rkt
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-11-28 22:50:22 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-11-28 22:50:22 -0500
commitc871d9cbe4cc6d3063665a428de4b205cef29041 (patch)
tree7a1ad52aaea5a551ce57e546e53fb044a69b9af3 /2020/day-07/day-07.rkt
parentfeccf3f6f0a806b3317d1f399e3e8b42945c4f09 (diff)
downloadgleam_aoc-c871d9cbe4cc6d3063665a428de4b205cef29041.tar.gz
gleam_aoc-c871d9cbe4cc6d3063665a428de4b205cef29041.zip
2020: day 4, 6, 7
Diffstat (limited to '2020/day-07/day-07.rkt')
-rw-r--r--2020/day-07/day-07.rkt44
1 files changed, 44 insertions, 0 deletions
diff --git a/2020/day-07/day-07.rkt b/2020/day-07/day-07.rkt
new file mode 100644
index 0000000..6ad63e5
--- /dev/null
+++ b/2020/day-07/day-07.rkt
@@ -0,0 +1,44 @@
+#lang racket
+(require "../../jj-aoc.rkt"
+ threading
+ rebellion/collection/entry
+ rebellion/collection/multidict)
+
+(define raw-rules (~> (open-day 7 2020) (port->string) (string-split "\n")))
+
+(define (split-rule r)
+ (match-define (list head tail) (string-split (string-trim r #px" bags?.") " bags contain "))
+ (~>> tail
+ (regexp-split #px"( bags?,\\s)" _)
+ (map (λ~> (regexp-match* #px"(\\d+) (\\w+ \\w+)" _ #:match-select rest)))
+ append*
+ (map (λ~> (match _
+ [(list n c) (list (string->symbol c) (string->number n))]
+ ['() '()])))
+ (cons (string->symbol head) _)))
+
+(define rules-multidict
+ (for*/multidict ([ln (in-list raw-rules)] #:do [(match-define (list* from tos) (split-rule ln))]
+ [to (in-list tos)])
+ (entry from to)))
+
+;; part 1
+
+(define (bags-that-eventually-contain target)
+ (for/fold ([holders (set)]) ([rule (in-multidict-entries rules-multidict)])
+ (match rule
+ [(entry outside (list (== target) _))
+ (set-union (set-add holders outside) (bags-that-eventually-contain outside))]
+ [_ holders])))
+
+(time (set-count (bags-that-eventually-contain '|shiny gold|)))
+
+;; part 2
+
+(define (bags-that-are-contained-by target)
+ (for/sum ([holding (in-multidict-entries rules-multidict)])
+ (match holding
+ [(entry (== target) (list held n)) (+ n (* n (bags-that-are-contained-by held)))]
+ [_ 0])))
+
+(time (bags-that-are-contained-by '|shiny gold|))