diff --git a/2.rkt b/2.rkt new file mode 100644 index 0000000..501240c --- /dev/null +++ b/2.rkt @@ -0,0 +1,31 @@ +#lang racket + +(require "scripts/aoc.rkt") + +;; solution for day 2 + +(define (password-valid-part1? lmin lmax char password) + (<= lmin (count (curry char=? char) (string->list password)) lmax)) + +(define (password-valid-part2? a b char password) + (define pwd-chars (string->list password)) + (xor (char=? (string-ref password (sub1 a)) char) + (char=? (string-ref password (sub1 b)) char))) + +(define (calculate valid-proc input) + (for/sum ([line (in-list input)]) + (match line + [(pregexp #px"^([0-9]+)\\-([0-9]+) (.): (.*?)$" (list _ lmin lmax char password)) + (if (valid-proc (string->number lmin) (string->number lmax) + (string-ref char 0) password) 1 0)] + [_ (error "failed to parse" line)]))) + +(module+ main + (define input (file->lines "inputs/2")) + ;; part 1 + (answer 2 1 (calculate password-valid-part1? input)) + + ;; part 2 + (answer 2 2 (calculate password-valid-part2? input)) + + (displayln "meow"))