diff options
Diffstat (limited to '2020/day-02/day-02.rkt')
-rw-r--r-- | 2020/day-02/day-02.rkt | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/2020/day-02/day-02.rkt b/2020/day-02/day-02.rkt new file mode 100644 index 0000000..3296d32 --- /dev/null +++ b/2020/day-02/day-02.rkt @@ -0,0 +1,45 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading) + +(struct policy (least most char pwd) #:transparent) + +(define (make-policy least-str most-str char-str pwd) + (policy (string->number least-str) + (string->number most-str) + (string-ref char-str 0) + pwd)) + +(define policies + (for/list ([l (in-lines (open-day 02 2020))]) + (~>> l + (regexp-match #px"(\\d+)-(\\d+) (\\w): (.*)") + rest + (apply make-policy)))) + +;; part 1 +(define (valid-policy? p) + (~>> p + policy-pwd + string->list + (count (curry char=? (policy-char p))) + ((λ (n) (and (>= n (policy-least p)) + (<= n (policy-most p))))))) + +(for/sum ([p (in-list policies)] + #:when (valid-policy? p)) + 1) + +;; part 2 +(define (valid-revised-policy? p) + (~>> p + policy-pwd + string->list + ((λ (lst) (list (list-ref lst (sub1 (policy-most p))) + (list-ref lst (sub1 (policy-least p)))))) + (count (curry char=? (policy-char p))) + (= 1))) + +(for/sum ([p (in-list policies)] + #:when (valid-revised-policy? p)) + 1)
\ No newline at end of file |