diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-11-28 22:50:22 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-11-28 22:50:22 -0500 |
commit | c871d9cbe4cc6d3063665a428de4b205cef29041 (patch) | |
tree | 7a1ad52aaea5a551ce57e546e53fb044a69b9af3 /2020 | |
parent | feccf3f6f0a806b3317d1f399e3e8b42945c4f09 (diff) | |
download | gleam_aoc-c871d9cbe4cc6d3063665a428de4b205cef29041.tar.gz gleam_aoc-c871d9cbe4cc6d3063665a428de4b205cef29041.zip |
2020: day 4, 6, 7
Diffstat (limited to '2020')
-rw-r--r-- | 2020/day-04/day-04.rkt | 47 | ||||
-rw-r--r-- | 2020/day-06/day-06.rkt | 22 | ||||
-rw-r--r-- | 2020/day-07/day-07.rkt | 44 |
3 files changed, 106 insertions, 7 deletions
diff --git a/2020/day-04/day-04.rkt b/2020/day-04/day-04.rkt index f9eac4a..54d50f8 100644 --- a/2020/day-04/day-04.rkt +++ b/2020/day-04/day-04.rkt @@ -18,14 +18,47 @@ (for/list ([p (in-list passports)] #:when (valid-passport? p)) (~> p string-split (map (curryr string-split ":") _) flatten (apply hash _)))) -(define (valid-byr p) +(define (between x low high) + (and (x . >= . low) (x . <= . high))) + +(define (valid-byr? p) (define year (string->number (hash-ref p "byr"))) - (and (<= year 1920) (>= year 2002))) + (between year 1920 2002)) -(define (valid-iyr p) +(define (valid-iyr? p) (define year (string->number (hash-ref p "iyr"))) - (and (<= year 2010) (>= year 2020))) + (between year 2010 2020)) -(define (valid-eyr p) - (define year (string->number (hash-ref p "iyr"))) - (and (<= year 2020) (>= year 2030))) +(define (valid-eyr? p) + (define year (string->number (hash-ref p "eyr"))) + (between year 2020 2030)) + +(define (valid-hgt? p) + (define height (hash-ref p "hgt")) + (cond + [(string-suffix? height "cm") + (let ([h (string->number (string-trim height "cm"))]) (between h 150 193))] + [(string-suffix? height "in") + (let ([h (string->number (string-trim height "in"))]) (between h 59 76))] + [else #false])) + +(define (valid-hcl? p) + (define color (hash-ref p "hcl")) + (regexp-match #px"^#[0-9a-f]{6}$" color)) + +(define (valid-ecl? p) + (member (hash-ref p "ecl") (list "amb" "blu" "brn" "gry" "grn" "hzl" "oth"))) + +(define (valid-pid? p) + (regexp-match #px"^\\d{9}$" (hash-ref p "pid"))) + +(define (valid-stricter-passport? p) + (and (valid-byr? p) + (valid-iyr? p) + (valid-eyr? p) + (valid-hgt? p) + (valid-hcl? p) + (valid-ecl? p) + (valid-pid? p))) + +(count valid-stricter-passport? passport-fields) diff --git a/2020/day-06/day-06.rkt b/2020/day-06/day-06.rkt new file mode 100644 index 0000000..b0e2af9 --- /dev/null +++ b/2020/day-06/day-06.rkt @@ -0,0 +1,22 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading) + +(define responses (~> (open-day 6 2020) (port->string) (string-split "\n\n"))) + +;; part 1 +(define (response-count-total rs) + (for/sum ([r (in-list rs)]) (~> r (string-replace _ "\n" "") string->list list->set set-count))) + +(response-count-total responses) + +;; part 2 +(define (response-consensus-total rs) + (for/sum ([r (in-list rs)]) + (~> r + (string-split _ "\n") + (map (λ~> string->list list->set) _) + (apply set-intersect _) + set-count))) + +(response-consensus-total responses)
\ No newline at end of file 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|)) |