diff options
Diffstat (limited to '2020')
-rw-r--r-- | 2020/day-04/day-04.rkt | 49 | ||||
-rw-r--r-- | 2020/day-05/day-05.rkt | 44 |
2 files changed, 93 insertions, 0 deletions
diff --git a/2020/day-04/day-04.rkt b/2020/day-04/day-04.rkt new file mode 100644 index 0000000..3c7db89 --- /dev/null +++ b/2020/day-04/day-04.rkt @@ -0,0 +1,49 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading) + +(define passports + (~> (open-day 4 2020) + (port->string) + (string-split "\n\n") + (map (λ~> (string-replace "\n" " ")) _))) + +;; part 1 +(define required-fields + (list "byr:" + "iyr:" + "eyr:" + "hgt:" + "hcl:" + "ecl:" + "pid:")) + +(define (valid-passport? p) + (andmap (λ (s) (string-contains? p s)) required-fields)) + +(count valid-passport? passports) + +;; part 2 +(define passport-fields + (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 year (string->number (hash-ref p "byr"))) + (and (<= year 1920) + (>= year 2002))) + +(define (valid-iyr p) + (define year (string->number (hash-ref p "iyr"))) + (and (<= year 2010) + (>= year 2020))) + +(define (valid-eyr p) + (define year (string->number (hash-ref p "iyr"))) + (and (<= year 2020) + (>= year 2030)))
\ No newline at end of file diff --git a/2020/day-05/day-05.rkt b/2020/day-05/day-05.rkt new file mode 100644 index 0000000..93c54a2 --- /dev/null +++ b/2020/day-05/day-05.rkt @@ -0,0 +1,44 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading + algorithms) + +(define tickets + (for/list + ([l (in-lines (open-day 5 2020))]) + (~>> l + (regexp-match #px"(.{7})(.{3})" ) + rest))) + +(define (find-place str min-p max-p l r) + (if (string=? "" str) + min-p + (let ([p-range (/ (add1 (- max-p min-p)) 2)] + [c (substring str 0 1)]) + (cond + [(string=? c l) + (find-place (substring str 1) min-p (- max-p p-range) l r)] + [(string=? c r) + (find-place (substring str 1) (+ min-p p-range) max-p l r)])))) + +(define (find-row str) (find-place str 0 127 "F" "B")) +(define (find-col str) (find-place str 0 7 "L" "R")) + +(define (ticket-id t) + (let ([row (find-row (first t))] + [col (find-col (second t))]) + (+ col (* 8 row)))) + +;; part 1 +(define occupied-seats + (~>> (for/list ([t (in-list tickets)]) (ticket-id t)))) + +(apply max occupied-seats) + +;; part 2 +(set-first + (set-subtract (list->set (inclusive-range (apply min occupied-seats) + (apply max occupied-seats))) + (list->set occupied-seats))) + + |