aboutsummaryrefslogtreecommitdiff
path: root/aoc2020/day-04/day-04.rkt
blob: 54d50f8af790a0785b07394a69f35a419a673661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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)