diff options
author | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
commit | 8ab65dc2da1742eb86ec636c50c7018385b68167 (patch) | |
tree | c4fd556aca9b867cfa1f2f174128c30857353884 /aoc2020/day-04 | |
parent | fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff) | |
download | gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip |
prep for 2023, renaming for consistency
Diffstat (limited to 'aoc2020/day-04')
-rw-r--r-- | aoc2020/day-04/day-04.rkt | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/aoc2020/day-04/day-04.rkt b/aoc2020/day-04/day-04.rkt new file mode 100644 index 0000000..54d50f8 --- /dev/null +++ b/aoc2020/day-04/day-04.rkt @@ -0,0 +1,64 @@ +#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 (between x low high) + (and (x . >= . low) (x . <= . high))) + +(define (valid-byr? p) + (define year (string->number (hash-ref p "byr"))) + (between year 1920 2002)) + +(define (valid-iyr? p) + (define year (string->number (hash-ref p "iyr"))) + (between year 2010 2020)) + +(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) |