From 5625c662c99bbca796da5ba6e5c48127322fb680 Mon Sep 17 00:00:00 2001 From: HJ Date: Sat, 11 Dec 2021 00:03:22 -0500 Subject: 2020 day 4 part 1 --- 2020/day-04/day-04.rkt | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2020/day-04/day-04.rkt (limited to '2020/day-04/day-04.rkt') 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 -- cgit v1.2.3