Skip to content

Instantly share code, notes, and snippets.

@dvanhorn
Created December 5, 2020 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvanhorn/b257ba3821e00fb8beb827158526ce2d to your computer and use it in GitHub Desktop.
Save dvanhorn/b257ba3821e00fb8beb827158526ce2d to your computer and use it in GitHub Desktop.
Advent of Code, Day 4, Part II
#lang racket
(define in "...")
(define rules
(list
(cons "byr" (λ (x) (let ((y (string->number x))) (and y (<= 1920 y 2002)))))
(cons "iyr" (λ (x) (let ((y (string->number x))) (and y (<= 2010 y 2020)))))
(cons "eyr" (λ (x) (let ((y (string->number x))) (and y (<= 2020 y 2030)))))
(cons "hgt" (λ (x) (let ((y (string-length x)))
(and (<= 3 y)
(let ((z (string->number (substring x 0 (- y 2)))))
(case (substring x (- y 2))
(("cm") (and z (<= 150 z 193)))
(("in") (and z (<= 59 z 76)))
(else #f)))))))
(cons "hcl" (λ (x) (and (= (string-length x) 7)
(regexp-match #rx"#[0-9a-f]*" x))))
(cons "ecl" (λ (x) (case x
(("amb" "blu" "brn" "gry" "grn" "hzl" "oth") #t)
(else #f))))
(cons "pid" (λ (x) (and (= (string-length x) 9) (regexp-match #rx"[0-9]*" x))))))
(define (valid? h)
(andmap (λ (k+p)
(match k+p
[(cons k p)
(and (hash-has-key? h k)
(p (hash-ref h k)))]))
rules))
(count (λ (x) (valid?
(make-hash
(map (λ (p) (match (regexp-split #rx":" p)
[(list x y) (cons x y)]))
(regexp-split #px"\\s" x)))))
(regexp-split #rx"\n\n" in))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment